how to change the value of a label in childForm from Another form button - vb.net

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

Related

Controlling non-startup form from another class

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

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

How to check if a Form has focus?

I have two WinForms. Let's say MainForm and ChildForm.
What I'm trying to make is when the MainForm is activated the ChildForm should always be visible, and when the MainForm loses focus the ChildForm should be hidden except if it's the ChildForm which was activated.
Here is my code :
AddHandler Me.MainForm.Activated, Sub()
Me.ChildForm.Show()
End Sub
AddHandler Me.MainForm.Deactivate, Sub()
If Not Me.ChildForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
AddHandler Me.ChildForm.Deactivate, Sub()
If Not MainForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
The code doesn't work. Basically when I click on a certain form (on the child form for example), the property Me.ChildForm.Focused is not correct and then ChildForm is hidden while it should be visible.
Can anyone know how to achieve that please ?
Not sure if this is a good one, but the idea is using flags for MainForm and ChildForm active status. These flags are set when MainForm and ChildForm's Activated and Deactive events are fired. For simplicity, I'll use a module to store the flags and the instance of ChildForm. Then call SetChildVisible() method in MainForm, Form1 and Form2 Activated event. Set startup object to MainForm.
Module1
Module Module1
Public FChild As ChildForm
Public FMainActive As Boolean
Public FChildActive As Boolean
Public Sub SetChildVisible()
FChild.Visible = Not FChildActive And FMainActive
End Sub
End Module
MainForm
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f1 = New Form1
Dim f2 = New Form2
Dim child = New ChildForm
Module1.FChild = child
child.Show()
f1.Show()
f2.Show()
End Sub
Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FMainActive = True
Module1.SetChildVisible()
End Sub
Private Sub MainForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FMainActive = False
End Sub
End Class
ChildForm
Public Class ChildForm
Private Sub ChildForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FChildActive = True
End Sub
Private Sub ChildForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FChildActive = False
End Sub
End Class
Form1
Public Class Form1
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class
Form2
Public Class Form2
Private Sub Form2_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class

How to pass Data from Parent Form to Child Form

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