Public Sub Change_Shape(colorShape As Color, Forme as Form) Forme.Shape_Com.BackColor = colorShape End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionest elle même appelé par un évènement receive_data levé par un event du port com....
j'ai bien saisi qu'en vb.net je ne peux pas accéder aux control de la form directement
Public Sub Recolorier() Accueil.pBox.BackColor = Color.Blue End Sub
Private Sub Accueil_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Recolorier() End Sub
FormAcq.Change_Shape(Color.Green)
CType(My.Application.OpenForms("FormAcq"), FormAcq).Change_Shape(Color.Green)
Delegate Sub UpdateTextBox(ByVal message As String)
Dim f As frmSerialInOut = My.Application.OpenForms("frmSerialInOut") f.Invoke(New UpdateTextBox( _ AddressOf f.UpdateMessage), New Object() {strInMessage})
Public Sub UpdateMessage(ByVal strInMessage As String) txtSerialIn.Text = strInMessage & vbCrLf & txtSerialIn.Text End Sub
VB.NET has a "default instance" feature for forms. This was created to help simulate VB 6 functionality with forms. The default instance means that instead of creating a new instance of the form and then acting on it, I can simply refer to the form by name, and a default instance will be created for me. These instances are thread specific, however. So, your serial port code (know it or not) is creating a second thread that retrieves/returns data. Normally, if you try to update your UI from a worker thread, a cross thread exception occurs. However, because VB.NET had default instances for forms, the secondary thread is creating another instance of your form in the background, updating the textbox on the NEW instance, and then releasing the second instance of the form for garbage collection when the worker thread is complete. This is why when you called "InvokeRequired" it returned false. It wasn't required on the SECOND instance of the form, because it was created in the SECONDARY thread -- i.e. it wasn't what you wanted to update.
By calling the form through the other method, it retreives the existing instance on the other thread. Of course, if you try to update the UI from that thread, you finally get the cross-thread exception we would normally expect to see. However, by calling Invoke on that instance, you can cause the code to execute on the UI's thread rather than from the worker thread.