Publically declared variables are not passing values to another form - vb.net

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

Related

Get values of one form into another form in Visual Basic

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

Having problem in passing variables between two forms in vb.net

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

Passing strings between forms while both forms are are being shown on screen

Hello to stackoveflow community members.!
I am reading threads from stackoverflow from past 6 months and which was really helpful for me. I never get stuck to anything where i need to write my own question here.
Currently i am working on visual studio 2015 with window based .net application.
I am having multiple forms in my application. I am loading a form into another form through panel control.
Say i have a form1 which is main form and form2 which i have opened main form using panel control.
When i am opening my form1 as startup form the data transfer is taking place. But when i am using another form say form3 for login page as startup the data transfer is not working between form1 and form2.
Could some please help.!!
Please find example code as below.
enter image description here
Number of Forms - 3,
Form3 as Login Form,
Form1 as Main Form,
Form2 as Entry Form,
Code for Form3-==========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form1 As New Form1
form1.Show()
Me.Hide()
End Sub
Code for Form1-===========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = "Hello"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim f As Form
f = Form2
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
Me.Panel1.Controls.Add(f)
f.Dock = DockStyle.Fill
f.Show()
End Sub
Code for Form2-=========
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.RichTextBox1.Text = Me.TextBox1.Text
End Sub
As suggested by 'Preciousbetine', i have removed new instance for form1. Application is working as desired.
Thank you so Much!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Hide()
End Sub

Passing information from form 1 to form 2 is not displaying correctly

I am trying to pass the information from form 1 object to form 2 object. all I am getting is a blank screen on form 2.
here is what I have so far
on form 1 I have :
Private Sub btnTestResults_Click(sender As Object, e As EventArgs) Handles btnTestResults.Click
Dim obb As New frmElevatedResults
obb.val = lstHighCholesterol
obb.Show()
On form 2 I have:
Option Strict On
Public Class frmElevatedResults
Public Property val As Object
Private Sub frmElevatedResults_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This Load event displays the results of the elevated cholestoral levels above 200.
lstResults = CType(val, ListBox)
I have been at this for hours. Please if anyone can give me an idea of why the list box in form 2 is blank, I would appreciate it.
I assume here that lstHighCholesterol is not empty and is also a ListBox control. Your Form 1 code is already correct. Try this:
Public Class frmElevatedResults
Public val as New ListBox
Private Sub frmElevatedResults_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each itm in val.Items
lstResults.Items.Add(itm)
Next
End Sub
...
Try this solution :
On form2 add a constructor :
Public Property val As Object
Public Sub New(val As Object)
InitializeComponent() ' This call is required by the Windows Form Designer.
Me.val=val
End Sub
Private Sub frmElevatedResults_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This Load event displays the results of the elevated cholestoral levels above 200.
lstResults = CType(val, ListBox)
In form1 :
Private Sub btnTestResults_Click(sender As Object, e As EventArgs) Handles btnTestResults.Click
Dim obb As New frmElevatedResults(lstHighCholesterol)
obb.Show( )

How to load a form again without exiting the application?

I have 2 form. Form1 and Form2. In form1 there's a button that will go to form2 if it is click. If I click that button if form1, form2 will load and integer a will become 1. If I click the button in form2, integer a will become 0 and it will back to form1. Since im in form1, if I click again the button in form1 it will go to form2 but form2 will not load again. Is theres a way to load again the form? Heres my example:
Form1:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
End Class
Form2:
Public Class Form2
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Me.Hide()
Form1.Show()
End Sub
End Class
Thanks in advance!
Form2's Load method only gets called once because that is the normal life cycle for a form. The load event only gets called once before the form is loaded for the first time.
From Form.Load Event:
Occurs before a form is displayed for the first time.
The solution depends on your needs. If you need to keep the state of the form even when it is hidden then you want to use the VisibleChanged event.
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
MsgBox("Visible changed")
End If
End Sub
If you don't need to keep the state then you can discard Form 2 and recreate it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim Form2 = New Form2()
Form2.Show()
End Sub
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Form1.Show()
Me.Close()
End Sub
Just set a new instance of Form2 before you load it.
Dim Form2 As New Form2
Form2 .Show()