Is it possible to use a label? I'm planning to display a scoring system, like every correct answer the score will add 10pts.
-
Take a look https://www.youtube.com/watch?v=RAAxqTK1W-k – AbdulAziz Apr 24 '17 at 18:50
-
Pass it in the constructor for your form... – Trevor Apr 24 '17 at 19:00
-
1Please read [ask] and take the [tour] as this is poorly asked. – Ňɏssa Pøngjǣrdenlarp Apr 24 '17 at 21:00
3 Answers
Use an instance of a class within a form. The Form can call methods of the Class. The Class can raise events to the Form. Don't talk directly from a Class to a Form. Or from one Form to another.
Form1 with TextBox1:
Public Class Form1
Private myClass1 As Class1
Private myForm2 As Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myClass1 = New Class1()
myForm2 = New Form2(myClass1)
myForm2.Show()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
myClass1.SetText(TextBox1.Text)
End Sub
End Class
Form2 with Label1:
Public Class Form2
Private WithEvents myClass1 As Class1
Public Sub New(instance As Class1)
InitializeComponent()
myClass1 = instance
End Sub
Private Sub myClass1TextSet(value As String) Handles myClass1.TextSet
Me.Label1.Text = value
End Sub
End Class
Class1:
Public Class Class1
Private text As String = ""
Public Event TextSet(value As String)
Public Sub SetText(value As String)
Me.text = value
RaiseEvent TextSet(value)
End Sub
End Class
Form2.Label1 will update as you type in Form1.TextBox1. You may change it around as you need to fit your application, but try to keep this structure.
Form >> instance >> Class
Class >> events >> Form
The form instantiating the other form is for simplicity of this example. Larger scale projects could have a form loader factory responsible for making forms.

- 15,168
- 7
- 48
- 72
-
@Zaggler Nothing was **linked** when I posted this answer. **Related** questions (including one nominated as duplicate) do not apply because `every correct answer the score will add 10pts` may not work very well with the one-shot constructor solution. Which one did you have in mind anyway? – djv Apr 24 '17 at 20:12
In your Form2 add a public shared integer
Public Shared score As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Then in Form1 increase it by adding to it
Form2.score += 50

- 137
- 8
-
-
OP said he wanted to do a score system, now he has a score that can be accessed by both forms so he can display it in the second form or do whatever. – EuX0 Apr 24 '17 at 19:11
for example Add two forms to project in Form2 define Label1 control and in Form1
Public Class Form1
Private intScore As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = intScore.ToString
End Sub
End Class

- 187
- 3