Passing Control Parameters between Forms

G

Guest

I have an app with form1 and form2. Form1 has a label control. Form2 has a checkbox and OK button. By default the label on form1 is hidden. When I lauch my app form1 is the default. I right click on the form and select "Add Labels" from the context menu. This takes me to form2 where I can then select the checkbox if I want to turn on the label back on form1. This works great. I then select the "OK" button on form2 and executed the following code

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Clic
Dim frm1 As New Form
frm1.Show(
Me.Close(
End Su

This returns me to form1 and sure enough, the label is now visible. My problem is this. SInce the label is visible, when I return to form2 again I want the checkbox to be checked. I can't seem to get this to happen. I've tried to check the visible property of the label on form1 in the form2.show event, the form2.gotfocus event, the form2.load event and a few others but the debugger always reports the status of the following code as false

Dim frm1 As New Form
If frm1.label1.Visible = True The
checkbox1.Checked = Tru
End I

If declared everything everywhere and am just lost. WHat am I missing here

Thank you
Joh
 
G

Guest

you are creating a new instance of Form1 which is not the instance that called this form Form2. since you've set th
visible property of the label to false in design mode, whenever you create a new instance of Form1, the visible propert
of your label is always going to be false. you basically need a handle to the calling instance of Form1. this is similar t
something i posted yesterday. here's what you can do

code for Form1

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Clic
Dim frm2 As New Form
frm2.init(Me
If Label1.Visible = True The
frm2.CheckBox1.CheckState = CheckState.Checke
Els
frm2.CheckBox1.CheckState = CheckState.Unchecke
End I
frm2.Show(
End Su

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseU
ContextMenu1.Show(Me, New Point(e.X, e.Y)
End Su

code for Form2

Private frmParent As Form

Friend Sub init(ByRef frm As Form1
frmParent = fr
End Su

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChange
If CheckBox1.CheckState = CheckState.Checked The
frmParent.Label1.Visible = Tru
Els
frmParent.Label1.Visible = Fals
End I
End Su


The init method (or whatever else you may want to call it) makes sure you have a handle to the calling form. you coul
have also defined a let property to do this. any way will work fine

hope this helps..
----- jcrouse wrote: ----

I have an app with form1 and form2. Form1 has a label control. Form2 has a checkbox and OK button. By default the label on form1 is hidden. When I lauch my app form1 is the default. I right click on the form and select "Add Labels" from the context menu. This takes me to form2 where I can then select the checkbox if I want to turn on the label back on form1. This works great. I then select the "OK" button on form2 and executed the following code

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Clic
Dim frm1 As New Form
frm1.Show(
Me.Close(
End Su

This returns me to form1 and sure enough, the label is now visible. My problem is this. SInce the label is visible, when I return to form2 again I want the checkbox to be checked. I can't seem to get this to happen. I've tried to check the visible property of the label on form1 in the form2.show event, the form2.gotfocus event, the form2.load event and a few others but the debugger always reports the status of the following code as false

Dim frm1 As New Form
If frm1.label1.Visible = True The
checkbox1.Checked = Tru
End I

If declared everything everywhere and am just lost. WHat am I missing here

Thank you
Joh
 
G

Guest

Thats just a way to make sure you have a reference to the calling form (Form1) in your Form2 so that you can chang
the visibility of the label on Form1 (which is still visible) based on the checkbox check state. to solve the problem that yo
mentioned, just look at the code under MenuItem1_Click (forget the init) on Form1. that should do what you are tryin
to accomplish - it worked for me absolutely fine

makes sense

----- jcrouse wrote: ----

Makes no sense what-so-ever. I don't understand the init

Thanks anyways
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top