How to pass Data from Parent Form to Child Form - vb.net

I have 2 forms, Form1 is a parent form and Form2 is a child form. Both of them are set to show at the same time.....Form1's mid-container is set to true and has a button, Form2 has a text-box...I want it that if I press the button in Form1 something will appear in the text-box in Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
IsMdiContainer = True
Dim inv As New Form2
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
I tried clicking the button but nothing happened, I also tried the other way around...putting a button in Form2 and a text-box in Form1 and it works...

you will have to move the inv variable out of the form_load scope
Public Class Form1
Private inv As New Form2 'here
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
IsMdiContainer = True
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
inv.TextBox1.Text = "Hello World"
End Sub
End Class

Say you have a TextBox1 control on Form2 and a button on Form1 and upon clicking button on Form1, "Hello World" will appear on TextBox1 on Form2, just do it like this...
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.TextBox1.Text="Hello World"
End Sub

Related

how to change the value of a label in childForm from Another form button

need a little help i cant seem to change the value/text of a label when im using this method to open another form.
i have 3 form (form1, form2, form3) form1 is my main form which has a panel which i want the form 2 to appear/show(which i manage to do). then in form 2 i have a label and a button(to open form 3 using showdialog which is working). now in form 3 i have a button that will change the text of the label in form2 but its not working here the code.
form1
Public Class Form1
Dim myForm As New Form
Public Sub openChildForm(childform As Form)
If Not IsDBNull(myForm) Then myForm.Close() ' this will close active form
myForm = childform ' store childform in myform
childform.TopLevel = False
childform.FormBorderStyle = FormBorderStyle.None ' remove border to be put in panelChildForm
childform.Dock = DockStyle.Fill ' make childform dockstyle to fill to remove excess space if there are
Panel1.Controls.Add(childform) ' add childform as a control of panel1
Panel1.Tag = childform
childform.BringToFront()
childform.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
openChildForm(New Form2)
End Sub
End Class
form2
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.ShowDialog()
End Sub
End Class
form3
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = "NEW FORM 2"
End Sub
End Class
this is what it looks like
form2
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Form3.ShowDialog()
Label1.Text = My.Forms.Form3.str
End Sub
End Class
form3
Public Class Form3
Public str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
str = "NEW FORM 2"
End Sub
End Class

How to hold reference to different forms with one variable

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"

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

VB .Net Pass value from loaded form

im currently made project with VB VS2012
i have 2 form,
form1 contains textbox and button, this button is to open form2.
in form2, i have 1 button. i want to set form1.textbot value when form2 button is clicked then close the form2.
form1 button to call form2
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
this is button form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Dim x As New form1
x.textbox.Text = "tes"
End Sub
i've tried ctype, directcast but its not working. by the way, this form1 is docked to a mainform. i tried to do the same but without form docked, it works, but when it docked, its not working.
Try if you are using default instances.
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Form1.textbox.Text = "tes"
Close()
End Sub
Did you really name the text box textbox? Descriptive names can be helpful.
What you're currently doing is creating a new instance of form1 with the line Dim x As New form1 - and that means you're not updating the original form1.
Since you are passing a reference to form1 to the Form2 dialog already, i.e. .ShowDialog(Me) then this would work:
'form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
'Form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
CType(Me.Owner, form1).textbox.Text = "tes"
End Sub
i've found my solution
here's code on form 1
Private t1 As String = String.Empty
Private t2As String = String.Empty
Dim f As New form1()
If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
t1 = f.t1.ToString
t2 = f.t2.ToString
textbox.Text = t1
textbox2.Text = t2
f.Close()
End If
and here's on form2
Public t1 As String
Public t2 As String
Me.DialogResult = Windows.Forms.DialogResult.OK
If Me.DialogResult = Windows.Forms.DialogResult.OK Then
t1 = dgdriver.SelectedRows(0).Cells(1).Value.ToString
t2 = dgdriver.SelectedRows(0).Cells(0).Value.ToString
Me.Close()
End If

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()