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
Related
I have a Form (named Form1) which has a fixed size and some controls.
I created another Form (named Form2), which is a copy of Form1 with only difference being a different fixed size.
I created a Form "SharedForm", which holds subs and functions used by both forms (so I don't have to write them for each of them).
My problem is: I don't know how to keep reference for either form (only one at a time, ever).
If I declare FormRef variable as Form, I get an error that "label1 is not a member of form".
(Otherwise if I declare as Form1 or Form2 it works fine, but of course only for one form)
SharedForm looks like the following:
Public Class SharedForm
Public Shared FormRef As Form 'problem is here
Private Sub SharedForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormRef = New Form2
FormRef.Show()
End Sub
Public Shared Sub Button1_Click(sender As Object, e As EventArgs)
FormRef.Label1.Text = "test"
End Sub
End Class
Form1 and Form2 are as so.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Just a class, not a Form.
Public Class EventCode
Public Shared Sub Button1Click(Sender As Form)
Dim frm As Form = Nothing
If Sender.Name = "Form1" Then
frm = DirectCast(Sender, Form1)
ElseIf Sender.Name = "Form2" Then
frm = DirectCast(Sender, Form2)
End If
frm.Controls("Label1").Text = "Hello World"
End Sub
End Class
In Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form1 shows "Hello World"
In Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form2 shows "Hello World"
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
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
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
i have multiple forms in my vb project. Form1 is a startup form. There is a variable, that is passing integer value to Form2. I added Form3 to the project and made it as startup form. Then i deleted it from the project and again made Form1 as startup form. But, since then the variable on Form1 is not passing its integer value to Form2. I messed up with my project. Can anybody help ???
IN FORM 1
Public i as integer
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
i = 1
dim nf as new form2
nf.showdialog(me)
end sub
IN FORM 2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
msgbox(form1.i)
end sub
But the msgbox prints value '0'
Ok Friends thanks for your help. But, i solved it myself.
IN FORM 1
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
dim nf as new form2
nf.f2int = 1
nf.showdialog(me)
end sub
IN FORM 2
Public Property f2int as Integer
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
msgbox(f2int)
end sub