|
mechman wrote: Form1.SetTextLvw(3, "Timer:" & lCnt)
From your Form_Load event, you make a call to Test.TestUpdate(). The problem is that this module doesn't have any refernce to an instance of Form1. Right now, your code won't even compile. Forms do not work like they did in VB6. There is no "global" instance of Form1 that everything can see.
This is a quick and dirty hack, mostly because Modules are an outdated concept, but here's a couple of changes that should get this to work:
Public Module Test
Public myForm As Form1
Pricate lCnt As Integer
Private oTimer As New Timers.Timer(180)
Public Sub TestUpdate()
If Not myForm Is Nothing Then
Throw New Exception("You must set the value of myForm before calling TestUpdate!")
Else
AddHandler oTimer.Elapsed, AddressOf sTimer
oTimer.Start()
End If
End Sub
Private Sub sTimer(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
lCnt += 1
myForm.SetTextLvw(3, "Timer:" & lCnt.ToString())
End Sub
End Module
Now, when you start your app, you have to tell this module how to get to Form1.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Test.myForm = Me
Test.TestUpdate()
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
My aplogogies, I was trying to consolidate code at the last minute to post my real question...
The point was the listview will not update via a thread using the InvokeRequired...The value is updated if you check it via code, but the UI isn't showing the values...This was a test app to duplicate the scenerio...I have seen this several other times with the listview...I have also seen, if the ListView is not prepopulated with columns and items at design time, the InvokeRequired will not see any items or columns via code...
|
|
|
|
|
Also, if a new thread is created with the Form's class, everything works correctly...
|
|
|
|
|
If this works, then I would suspect that your updating the control from the wrong thread...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
That is the problem, I know I am updating from the wrong thread, but the Invoke, InvokeRequired, etc...do not ackowledge it, so the code continues to update the data, but the UI is wrong. I can verify that it is the wrong thread by using my sample code...You can check to see if the Handle for the listview is created, and it will show it isn't...What I am seeing is very strange behavior that I don't know the workaround. I can't always gurantee my Form's class will update a control, why would you ever need class's, modules, etc. if that was the case? This is why I am posting...
|
|
|
|
|
OK. How about this...
This code assumes you have a form with a Button and a ListView on it...
Private Delegate Sub AddItemToListViewDelegate(ByVal item As String)
Private Delegate Sub ChangeSubItemDelegate(ByVal index As Integer, ByVal item As String)
Private t1 As Thread
Private t2 As Thread
Private Const MAXITEMS As Long = 20
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'AddItems()
ListView1.Items.Clear()
t1 = New Thread(New ThreadStart(AddressOf AddItems))
t1.Start()
Thread.Sleep(100)
t2 = New Thread(New ThreadStart(AddressOf RandomSubItems))
t2.Start()
End Sub
Private Sub AddItemToListView(ByVal item As String)
If ListView1.InvokeRequired Then
Dim d As New AddItemToListViewDelegate(AddressOf AddItemToListView)
Invoke(d, New String() {item})
Else
Dim newItem As New ListViewItem(item)
newItem.SubItems.Add("---")
ListView1.Items.Add(newItem)
Application.DoEvents()
End If
End Sub
Private Sub ChangeSubItem(ByVal index As Integer, ByVal item As String)
Try
If ListView1.InvokeRequired Then
Dim d As New ChangeSubItemDelegate(AddressOf ChangeSubItem)
Invoke(d, New Object() {index, item})
Else
If ListView1.Items.Count > index Then
ListView1.Items(index).SubItems(1).Text = item
Application.DoEvents()
End If
End If
Catch
End Try
End Sub
Private Sub AddItems()
Dim x As Long
While x < MAXITEMS
Try
AddItemToListView(String.Format("Item #{0}", x))
x = x + 1
Catch ex As ThreadAbortException
Exit While
End Try
End While
End Sub
Private Sub RandomSubItems()
Dim RNG As New Random
Dim x As Long
While True
Try
ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
Catch ex As ThreadAbortException
Exit While
End Try
' This little sleep has to be in there for some reason.
' The form won't update properly without it!
Thread.Sleep(1)
End While
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If t1 Is Nothing Then
Exit Sub
End If
t1.Abort()
t1.Join()
t2.Abort()
t2.Join()
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Now take the RandomSubItems and put it in a new Module or Class (outside of the Form's Class) and try it...the listview will not update via the UI...
|
|
|
|
|
OK. It works fine...
Form1 code:
Private Delegate Sub AddItemToListViewDelegate(ByVal item As String)
Private Delegate Sub ChangeSubItemDelegate(ByVal index As Integer, ByVal item As String)
Private t1 As Thread
Private t2 As Thread
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.myForm = Me
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListView1.Items.Clear()
t1 = New Thread(New ThreadStart(AddressOf AddItems))
t1.Start()
Thread.Sleep(100)
t2 = New Thread(New ThreadStart(AddressOf Module1.RandomSubItems))
t2.Start()
End Sub
Private Sub AddItemToListView(ByVal item As String)
If ListView1.InvokeRequired Then
Dim d As New AddItemToListViewDelegate(AddressOf AddItemToListView)
Invoke(d, New String() {item})
Else
Dim newItem As New ListViewItem(item)
newItem.SubItems.Add("---")
ListView1.Items.Add(newItem)
Application.DoEvents()
End If
End Sub
Public Sub ChangeSubItem(ByVal index As Integer, ByVal item As String)
Try
If ListView1.InvokeRequired Then
Dim d As New ChangeSubItemDelegate(AddressOf ChangeSubItem)
Invoke(d, New Object() {index, item})
Else
If ListView1.Items.Count > index Then
ListView1.Items(index).SubItems(1).Text = item
Application.DoEvents()
End If
End If
Catch
End Try
End Sub
Public Sub AddItems()
Dim x As Long
While x < MAXITEMS
Try
AddItemToListView(String.Format("Item #{0}", x))
x = x + 1
Catch ex As ThreadAbortException
Exit While
End Try
End While
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If t1 Is Nothing Then
Exit Sub
End If
t1.Abort()
t1.Join()
t2.Abort()
t2.Join()
End Sub
Module1 code:
Imports System.Threading
Module Module1
Public myForm As Form1
Public Const MAXITEMS As Long = 20
Public Sub RandomSubItems()
Dim RNG As New Random
Dim x As Long
While True
Try
myForm.ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
Catch ex As ThreadAbortException
Exit While
End Try
Thread.Sleep(1)
End While
End Sub
End Module
I never use Modules, ... ever. They're an outdated concept that's around just to be backwards-compatible with VB6 code.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks Dave for all your help...I was simply trying to duplicate my issue in a easy manor...The problem was, in the module or Class (what I really use) I reference the form itself and not the public variable you used in the module:
Public myForm As Form1
Change
myForm.ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
To
Form1.ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
and you see the issue...changing back to what you had corrects it...Thanks again...
Can you tell why it was needed, just curious?
|
|
|
|
|
Because in that Module, or any other class, Form1 doesn't exist. This is not like VB6 where the forms existed in every context. The is REAL Object Oriented Programming and you have to follow the rules this time.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Ok...how come you can call Form1 directly, cheating the threading model? Just trying to get the full understanding...
|
|
|
|
|
I don't call Form1 directly anywhere. What are you talking about?? A little snippet would help...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
If you pass a ListViewItem to the module, and reference the form and listview object (to update it), you can update the the ListViewItem..I guess I have answered my own question because it does the same as the myForm object, just using a different object (lower level)...
|
|
|
|
|
That works too. But the code for updating the UI should remain on the form responsible for that UI.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello everyone,
A quick question. Is there a way to make a grid on a regular form or a MDI form. I want to create a GUI that has a grid on it to help the user with object placement while the application is running.
Thank you,
Quecumber256
|
|
|
|
|
I want to catch the printer jobs , information like , printer name , pages printed , system name and save them in to a access table , How can i do that , can u help me out
Fahad Ahsan
|
|
|
|
|
|
|
Good Afternoon:
I'm trying to develop a data entry system. There could be upto 6 different
tables to enter data into. My current design has one table and it's corresponding fields dropped onto a form. My question is this:
How do I make this generic? I want the user to pick a product which will
load data onto the form (i.e form1) for processing. When completed, if they want to enter data for another product (from another table), they pick the product and the form loads (to form1 from above). How do I handle the
CustomDataSet, TableAdapter, BindingSource and BindingNavigator?
Any help greatly appreciated.
Mike
|
|
|
|
|
Hi, I'm new guy to Vb.net. I have one issue with Crystal Report. Currently I have made some changes in the crystal report (which uses xml generated dataset as connection). But whatever the changes I made in the report is not refecting during runtime. Can anyone help me in this?
The way I did changes in the crystal report (as follows).
1. I have included new element (which is an new column from the table) in the xml schema in the project
2. generated it as dataset and Save that
3. I have included the field from the Dataset into report.
4. Save the report and entire project.
5. Build project.
When i click the particular button trigger event it loads the correct report without my changes. Please help to figure out this issue (which is very important for client's business concern)
Also, during build process i'm getting error "import on word microsoft.interop.word" COM object is not found". Does is related to this...?
Please help where i'm making mistake.
Jagan S.
s.jagans@gmail.com
|
|
|
|
|
Hi
I have one issue with Crystal Report in VB.net. Currently I have made some changes in the crystal report (which existing one and uses xml generated dataset as connection [ado.net xsd]). I need to include a new column in the report, I made that changes in the report is not reflected during report load. Can anyone help me in this?
The way I did changes in the crystal report (as follows).
1. I have included new element (which is an new column from the table) in the xml schema in the project
2. generated it as dataset table and Save that
3. I have included the field from the Dataset into report.
4. Save the report and entire project.
5. Build project.
When i click the particular button trigger event it loads the reports, but new column which i have included was not displayed (stilling loading the old report) and my changes are not available. Please help to figure out this issue (which is very important for client's business concern)
Also, during build process i'm getting error "import on word microsoft.interop.word" COM object is not found". Does is related to this...?
Please help where i'm making mistake.
any got the idea about to the above said problem..I'm badly looking for some input.
Jagan S.
s.jagans@gmail.com
|
|
|
|
|
Hi everyone,
I have a listview that display data from my SQL query. Now, I'm trying to modify some data on the listview by showing the selected data into a new form.
After I save the modified data, how can I show the updated data on the listview without having to re-run the same query, cause if I re-run the query it will take some time to get the updated data?
Is there any way to do this?
Thank you very much.
|
|
|
|
|
I failed to create lan chat in vb.net plz help me
Harshad
|
|
|
|
|
look up this
http://www.a1vbcode.com/app-3263.asp
_____________________
Proud to be Albanian
_____________________
|
|
|
|
|
Hello all,
Need YOUR help. I want to show MSchart in Web Application. Is it possible?? I have added it using Add Remove window and even it can be seen on the Web page.
But the problem is ahead. I can't program it. I can't see its events on the code page & even can't program it in any other events. cant we use this control in Web page. If we can, then please tell me how.
Waiting for your reply,
|
|
|
|
|