Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make form2a inherit form2 controls and recognize state 1

Status
Not open for further replies.

lunaclover

Programmer
Joined
Jun 22, 2005
Messages
54
Okay - I have this form (form1) with lots of radiobuttons, comboboxes and textboxes. On this form, there is a button that loads form2 with

Dim frmAccessories As New Form2a
frmAccessories.Show()

So far so good. Then in the code for form2 I want it to recognize the state of the controls on form 1 and when form2 loads it show/hide its own controls appropriately. For example on form2_load(byval...) handles mybase.load
I want to be able to say

If form1.radiobutton1.checked = true then
chk1.enabled = true
chk2.enabled = false
.
.
end if

chk1 and chk2 are checkboxes on form2. radiobutton1 is on form1.

I tried saying 'dim form2 as new form1' so it would inherit all form1's controls, and it seems to work (i.e. there is no squiggely blue line underneath radiobutton1, and the intellisense works and brings up controls from form1, like radiobutton1, for example) but then when I run it with a break to see what it's doing, it says 'radiobutton1.checked = false' when really it is checked, and should say 'true' and proceed with the if statement.

Why does it not see that it is checked? I am loading form2 from a button on form1.

Is my dim statement wrong somehow? I've tried changing it and it gets mad at me and insults me. Any suggestions would REALLY be appreciated!

Thanks!
LunaClover
 
Check this FAQ: faq796-5773

What you are looking to do is not inheritance. From the sounds of it you are looking to access one form from another form. The easiest way to do this is to pass a reference of the first form to the second form. If you use form.showmodal you can get this by casting the form.parentform to the type of the parent form. If you are using form.show you will have to pass the reference in, either with a property, or parameter in the constructor.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi Rick, thanks for the post! I am new and don't get it. I've tried looking up what form.showmodal does and found examples of loading applications.. I looked in the index and searched for it under help.. it doesn't even come up.

Is there a certain statement I have to make so it can see the state of the controls on form1 from form2? It will recognize the control exists, just doesn't recognize its state.

Like I said, I have so many controls on form1, to pass all of them wouldn't be very .. I forgot the word, by brain is mush. But you know what I mean. Can you please elaborate on what your suggestion is?

Thanks,
LunaClover
 
Let's say you have 2 classes. form1 and form2. form1 displays some information, the user can click a button and form2 will open and show information about the thing the user was looking at on form1.

On form2 I would add a property:
Code:
private m_form1Ref as form1

Public Property ParentForm() as form1
  get
    return m_form1Ref
  end get
  set (value as form1)
    m_form1Ref = value
  end set
end property

Then in form1 where you are opening form2:
Code:
  ...
  dim frmDetail as new form2
  frmDetail.ParentForm = Me
  frmDetail.show

Then in the on_load of form2:
Code:
  MyVariable = me.parentform.somecontrol.text

you'll likely have to change the access level of the controls on form1 to public also. Open up the 'windows form designer generated code' region on form1. Anyplace you see Friend WithEvents change it to Public Friend WithEvents.

-Rick

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Just a thought but you could do something like this:

In Form1:
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim f2 As New Form2(Me)
    'either
    f2.Show()
    'or 
    'f2.ShowDialog

  End Sub

and in Form2:
Code:
  [b]Private frm1 As Form1[/b]

#Region " Windows Form Designer generated code "

  Public Sub New([b]ByVal f1 As Form1[/b])
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()
    [b]frm1 = f1[/b]

    'Add any initialization after the InitializeComponent() call

  End Sub

and

Code:
  Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    With frm1
      CheckBox1.Checked = .CheckBox1.Checked
      CheckBox2.Checked = .CheckBox2.Checked
      CheckBox3.Checked = .CheckBox3.Checked
    End With

  End Sub

both forms in this example have three checkboxes:
CheckBox1, CheckBox2 and CheckBox2


Hope this helps.
 
Yup, that's another option. You could also overload the .show method to accept the parent and impliment a singleton design. There are many ways to get access to the stuff from form1 on form2, that's why I recommended that FAQ (Entitled: "a Thousand ways to transfer data between forms")

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thank you guys so much! Rick, I tried yours and got hungup on the "frmDetail.ParentForm = Me", it said it was 'read-only'.. I changed them to public from friend but still had the same problem.. I'm sure it's me just being new so don't worry I know it's not your advice. You have helped me before and what a help you are. Thank you!

EarthandFire, what you said works perfectly for my project. I was so proud to see it work. (It made my co-programmer(ASP.net) have a crush on you.) : )

Thanks again!
LunaClover


 
Sorry about that luna, most likely the property we added (ParentForm) is having issues with the base property ParentForm. If you renamed it to MyParentForm, or something like that, it should work fine.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top