Comment faire pour combiner ces deux appels, lorsque la méthode SetText doit avoir deux paramètres ?
Invoke(New SetText(AddressOf ProtectedWriteText), TxbPosX, "0") Invoke(New SetText(AddressOf ProtectedWriteText), LblConsX, "0")
Private Delegate Sub SetText(sender As Object, s As String) Private Sub UpdateText(obj As Object, txt As String) If obj.GetType() Is GetType(TextBox) Then Dim t As TextBox = CType(obj, TextBox) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else t.Text = txt End If ElseIf obj.GetType() Is GetType(Label) Then Dim t As Label = CType(obj, Label) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else t.Text = txt End If End If End Sub Private Sub ProtectedWriteText(objAs Object, txt As String) If TypeOf sender Is TextBox Then CType(sender, TextBox).Text = t ElseIf TypeOf sender Is Label Then CType(sender, Label).Text = t End If End Sub
Dim thread As New Thread( Sub() OperationEnCours = True Try LogTrace.WriteLine("Mise en position initiale de la machine") Carte.SetEnable(True) StatusBarLabel1.Text = "Initialisation de la pince..." Carte.Voie(3).Origine() UpdateText(TxbPosP, "0") UpdateText(LblConsP, TxbPosP.Text) StatusBarLabel1.Text = "Initialisation de l'altitude du plateau..." Carte.Voie(2).Origine() ' Plateau Z UpdateText(TxbPosZ, "0") StatusBarLabel1.Text = "Initialisation position plateau..." Carte.Voie(1).Origine() ' Plateau X UpdateText(TxbPosX, "0") ' <--- lui est bien rafraichi UpdateText(LblConsX, TxbPosX.Text) ' <--- Mais pas celui-là ' … etc, etc... Catch ex As Exception LogTrace.WriteLine("#" + GetCurrentMethod().Name + " : " + ex.Message) MessageBox.Show(ex.Message, "Prise d'origine machine", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try OperationEnCours = False End Sub ) thread.Start()
Petite remarque : je suis obligé de passer par un délégué pour des Label, des textbox, etc mais curieusement pas pour la barre d'état...
UpdateText(TxbPosX, "0") ' <--- lui est bien rafraichi UpdateText(LblConsX, TxbPosX.Text) ' <--- Mais pas celui-là
UpdateText(LblConsX, "Coucou")ça devrait marcher.
Private Sub UpdateText(obj As Object, txt As String) If obj.GetType() Is GetType(Control) Then Dim t As TextBox = CType(obj, Control) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else ProtectedWriteText(obj, txt) End If End If End Sub
UpdateText(TxbPosP, "0") UpdateText(LblConsP, TxbPosP.Text) ' <- cet accès à .Text fonctionne.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub UpdateText(obj As Object, txt As String) If obj.GetType() Is GetType(Control) Then Dim t As Control = CType(obj, Control) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else ProtectedWriteText(obj, txt) End If End If End Sub
C'est un ToolStripStatusLabel… donc il ne possède pas de méthode InvokeRequired
Private Sub UpdateText(obj As Object, txt As String) Dim o As Type = obj.GetType() If o Is GetType(Label) Or o Is GetType(TextBox) Or o Is GetType(cNumBox) Then ' cNumBox est une classe derivée de TextBox Dim t As Control = CType(obj, Control) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else t.Text = txt End If End If End Sub
Private Sub UpdateText(obj As Object, txt As String) Dim o As Type = obj.GetType() If o Is GetType(Label) Or o Is GetType(TextBox) Or o Is GetType(cNumBox) Then ' cNumBox est une classe derivée de TextBox Dim t As Control = CType(obj, Control) If t.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else t.Text = txt End If ElseIf o Is GetType(ToolStripStatusLabel) Then Dim t As ToolStripStatusLabel = CType(obj, ToolStripStatusLabel) If t.GetCurrentParent.InvokeRequired Then Invoke(New SetText(AddressOf ProtectedWriteText), obj, txt) Else t.Text = txt End If End If End Sub