This question already has answers here:
Passing variables between windows forms in VS 2010
(3 answers)
How to pass value from Form1 to Form2
(2 answers)
Closed 5 years ago.
Is it possible to use a label? I'm planning to display a scoring system, like every correct answer the score will add 10pts.
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.
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
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
Related
How do you control a form from another class if that form is not the "Startup form:"?
Scenario:
I have a simple application with 2 forms (Form1 & Form2) and a class (Class1). Form1 has two controls (TextBox1 and Button1). Form2 has one control (TextBox1).
Class1 code:
Public Class Class1
Public Sub ChangeTextOnForm1()
Form1.TextBox1.Text = "Success"
End Sub
Public Sub ChangeTextOnForm2()
Form2.TextBox1.Text = "Success"
End Sub
End Class
Form1 code:
Public Class Form1
Dim Class1 As New Class1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm1()
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.Show()
End Sub
End Class
Form2 code:
Public Class Form2
Dim Class1 As New Class1
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Class1.ChangeTextOnForm2()
End Sub
End Class
If Form1 is set as the Startup Form it calls Class1.ChangeTextOnForm1() on Load and the text in Form1.Textbox1 is changed.
If Form2 is set as the Startup Form it calls Class1.ChangeTextOnForm2() on Load and the text in Form2.TextBox1 is changed.
If Form1 is the Startup Form and I click on Button1, Form2 opens but the text in Form2.Textbox1 is not changed. How do you control a form from another class if the form is not the startup form?
The solution is to open the default instance of Form2 instead of a new instance.
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Form2.Show()
End Sub
I am trying to get an integer value of one form (Form1) inside another form (Form2). I have tried to access it via the below code but not getting it. Can someone please tell me what I am doing wrong.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FinalPoints As New Form1
Label1.Text = FinalPoints.Label1.Text
End Sub
End Class
The problem: As you are showing your Form2 (Form2_Load is executed), a new Form1 is created. This newly created Form1 has NOT executed the Form1_Load function yet!
You would need to show the newly created FinalPoints (Form1) with FinalPoints.Show() like that:
Dim FinalPoints As New Form1
FinalPoints.Show()
Label1.Text = FinalPoints.Label1.Text
to let the Form1_Load function execute, which is then setting your FinalPoints.Label1.Text. But that would just opens a new Form1.
Also you can just get the public Points variable inside the Form2_Load like that (you also do not have to create a new Form1):
Label1.Text = Form1.Points
Alternatively: Just use a public variable inside Form2 and assign your value to it, before you show the form.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points.ToString
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FinalPoints As New Form2
FinalPoints.StringFromForm1 = Label1.Text
FinalPoints.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Public Property StringFromForm1 As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = StringFromForm1
End Sub
End Class
This line: Dim FinalPoints As New Form1 creates a new instance of Form1, but what you want is to refer to an existing instance of Form1. There are different techniques you can try. For example, overload the Show method of Form2.
Something like this:
Form1: pass the value to Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
Me.Hide()
f.Show(Points)
End Sub
Form2: fetch the value from caller (Form1)
Public Class Form2
Public Overloads Sub Show(ByVal Points As Integer)
Me.Label1.Text = Points.ToString
MyBase.Show()
End Sub
End Class
I'm almost a beginner of vb.net and I have to do some codes. I want to call a button click in form 2 from another class, i.e., form 1 and the problem is that the variables which defined as pubic, their values don't pass between two forms. My code simply presented as follows:
Public class form2
Public Property ResponseTime1 As String
Public Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Responsetime2 = 20
End sub
End class
Pub class form1
Dim Resposetime as string
Dim z as new form2
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
z.button1_click(sender, e)
ResponseTime = form2.ResponseTime2
MsgBox(ResponseTime) '' show nothing????????????
End sub
End class
I would be appreciated if someone helps me
The first problem I see is that Responsetime2 is never declared. A few spelling errors are further messing up the code. Even if I stick a Dim in front of Responsetime2 = 20 it is still not visible to from1. Just because a method is public doesn't mean that vaiables inside the method are visible. Even if it was visible, `Responsetime2 is an Integer and you have declared Responsetime as a String.
It is not a good idea to call an event procedure from another class. Event procedures are meant to respond to an event and are private by default. Make the code a separate public method and call it from both form2.button1_Click and form1.
Public Class form25
Public Property ResponseTime2 As Integer
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
SetResponseTime2()
End Sub
Public Sub SetResponseTime2()
ResponseTime2 = 20
End Sub
End Class
Public Class form15
Dim Responsetime As Integer
Dim z As New form25
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
z.SetResponseTime2()
Responsetime = z.ResponseTime2
MsgBox(Responsetime.ToString)
End Sub
End Class
Hi dear I have no idea why you made this Public Property ResponseTime1 As String most probably you have a reason for it but i am going to show for you how i would do it
Form1 Code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
form2.show
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(Form2.ResponseTime)
End Sub
End Class
Form2 code
Public Class Form2
Public ResponseTime As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ResponseTime = "test"
End Sub
End Class
I tested this one and its working
Hope i could help to you
I would like to share an event to another form via a common class with a public shared event.
My code is setup like this:
Form1 is the main form where the button.click event is generated
Form2 is a subform displayed in Form1 in a panel control, where if the event is generated executes a sub.
To share data I use a commonData class, to share the only data i need, because I want to keep private function and variables in the single forms.
Can someone help me figure out what I'd like to do?
something like this, but working
Public Class commonData
Public Shared Event event1()
End class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.Click
RaiseEvent commonData.event1()
End Sub
End class
Public Class Form2
Private Sub eventFired(sender As Object, e As EventArgs) Handles commonData.event1
MsgBox("event")
End Sub
End class
There is many techniques to do that but this one (which is one of those) I hope might help you to have an idea how can have a shared Event between Forms or other classes.
First as you want here is the common module (you can use a class instead, is your choice; but <Extension()> go only in modules).
CommonData as a Class and ExtensionUtils as a Module for extensions:
Imports System.Runtime.CompilerServices
Public Class CommonData
Public Shared Event MyGlobalEvent(eventSender As Object, otherParams As String)
Shared Sub RaiseMyGlobalEvent(eventSender As Object, otherParams As String)
RaiseEvent MyGlobalEvent(eventSender, otherParams)
End Sub
End Class
Module ExtensionUtils
<Extension()>
Public Sub ButtonClick(ByVal buttonCaller As Button, eventArgs As String)
CommonData.RaiseMyGlobalEvent(buttonCaller, eventArgs)
End Sub
End Module
And here the implementation:
In this example I use a button named “Button1” in Form2 and when I click on it show a msgbox in Form1.
Form1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
AddHandler CommonData.MyGlobalEvent, Sub(objSender As Object, message As String)
MsgBox(message & vbCrLf & " But I'm telling you that from form " & Me.Name)
End Sub
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, Sub()
Button1.ButtonClick(("I'm clicked from form " & Me.Name))
End Sub
End Sub
End Class
I'm trying to pass form name and object as parameters of my public sub because I have 2 forms and they are almost identical to one another.
For example I have richtextbox1 in Form1 and I also have richtextbox1 in Form2, I would like to create a public sub something like this.
Public Sub Sub1(ByVal frm_name As form, Byval obj_name As object)
frm_name.obj_name.Lines.Length 'Get the length of lines of that richtextbox
End Sub
Then when I want to call it
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
It is not working the way I want it, is there anything I can do for it to work?
Think about it. You are saying that you're passing the names of things but you're not. You're not passing the name of a form; you're passing the form itself. You're not passing the name of an object; you're passing the object itself. What is the actual point of this? It's to get the number of lines in a RichTextBox, right? So, write a method that takes a RichTextBox as an argument and then call it passing a RichTextBox.
Public Sub Sub1(rtb As RichTextBox)
Dim lineCount = rtb.Lines.Length
'...
End Sub
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class