How to hold reference to different forms with one variable - vb.net

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"

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

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 one form to multiple forms VB.net

My Form1 looks like this to get the value of textbox to pass the data to multiple forms.:
Public Class Form1
Public username As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
username = TextBox1.Text
Form2.Show()
Me.Close()
End Sub
End Class
At the second form, I used this code to show the data of username which was entered from the Form1.:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "Hi " & Form1.username & "!"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
Me.Close()
End Sub
End Class
It worked perfectly. The data showed up in the second form. But in Form3, it only shows "Form1"
Form3 code:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Text = Form1.username
End Sub
What should I do to make the data of username to show up on mutiple forms?
Do not use default instances of forms, create constructors for forms which takes username as parameter and then create forms explicitly
Public Class Form3
Private _username As String
Public Sub New(username As String)
_username = username
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Text = _username
End Sub
End Class
Public Class Form2
Private _username As String
Public Sub New(username As String)
_username = username
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = $"Hi {_username}!"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim formThree As new Form3(_username)
formThree.Show()
Me.Close()
End Sub
End Class
Public Class Form1
Private _username As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim formThree As new Form3(TextBox1.Text)
formThree.Show()
Me.Close()
End Sub
End Class
With this approach you don't need to expose username as public property for Form1.

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