Home > database >  How to control the label by class in vb.net
How to control the label by class in vb.net

Time:04-06

I can't change the label color by using class. enter image description here

Public Sub change_color()
    Form1.Label1.BackColor = Color.Red
End Sub

CodePudding user response:

First go to your class

Public Class My_class 

      Public Say As String  'if you make it Dim you can't get the value by sure to make it Public

      Public Function Do_somthing(ByVal Form As Form1)  ' Function
           Say = "Hi"
           Form.Label1.Text = "Boo!"
           Return True
      End Function

End Class

Second go to your Form

Public Class Form1
      Dim MyClass As New My_Class() ' Here to call ur class to make it easy

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          ' Here You Call the Function 
           MyClass.Do_somthing(Me) 
           ' Why (Me) because you want 'Your Class' get Full Access to Form Class
      End Sub

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
          ' Here You Call String value
           Label2.Text = MyClass.Say.ToString()
      End Sub


End Class
  • Related