• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Accessing parent form variables in C#

carl67lp

Supreme [H]ardness
Joined
Oct 17, 2001
Messages
4,554
I have a program in which an MDI parent spawns MDI child forms, as well as a dialogue in which the person types in their personal information. That information, in conjunction with data from the child forms, is used to transmit a request over the Web (see my other threads).

Currently I have the main form (the MDI parent) with private variables that match the personal info form. I've tried using "firstName = firstName.Text;" with no luck--I didn't expect it to work, frankly, since this would be looking on the personal info form. I've tried using "this.Parent.firstName" but that's not legal. I've also tried switching to public variables.

My suspicion is that I have some sort of design flaw, but I can't figure out how best to do this. It's making me feel more and more stupid as I go along. Perhaps I've been staring at this project far too long...
 
I assume there is some reason to store info there? I could see this being the case if there were some global setting that were used by and shared between all the child windows.

At any rate here is some code that does it. In this code, each child window has a textbox and two buttons. The parent has a single global string value. One button copies the local textbox contents to the parent, and the other displays the contents of parent's storage variable.

The code in the parent to create children using a menu item (notice the public var):
Code:
        public string globalValue = null;
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new Form2();
            f.MdiParent = this;
            f.Show();
        }


The two event handlers of the child window:
Code:
        private void button1_Click(object sender, EventArgs e)
        {
            ((Form1)MdiParent).globalValue = this.textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(((Form1)MdiParent).globalValue);
        }
 
[MS] said:
I assume there is some reason to store info there? I could see this being the case if there were some global setting that were used by and shared between all the child windows.

You hit the nail on its uppermost flat surface.

Each of the child windows represents an "update" to be sent. Each update (remember the previous thread? ;)) needs the person's name, phone, and so on to be transmitted with it. Thus, the concept of global settings, as it were.

As an aside (and I'll probably expand on this in the other thread), I'm likely going to give Web services another go, depending on what I can do in .NET CF, since my overall goal is a Pocket PC application--the desktop program was more of a proof-of-concept, testbed program.

Thanks!
 
Well, to get everything to work, I had to do this:

Code:
private void aboutYouItem_Click(object sender, System.EventArgs e)
     {
          AboutYou aboutYouForm = new AboutYou();
          aboutYouForm.TopMost = true;
          aboutYouForm.TopLevel = false; 
          aboutYouForm.Parent = this;
          aboutYouForm.Show();
     }

That, at least, showed the form. Then in that form's code, I did this:

Code:
private void buttonSave_Click(object sender, System.EventArgs e)
     {
     // First put the vars into the parent class
     ((EZUpdate)MdiParent).firstName = textFirstName.Text;
     ((EZUpdate)MdiParent).lastName = textLastName.Text;
     ((EZUpdate)MdiParent).phone = textPhone.Text;
     ((EZUpdate)MdiParent).accessId = textAccessId.Text;
     ((EZUpdate)MdiParent).unit = textUnit.Text;
     ((EZUpdate)MdiParent).email = textEmail.Text;
     // ... do other stuff with saving to a file
     }

However, when I hit that button, I get "Object reference not set to an instance of an object." It highlights the second assignment statement in the buttonSave_Click method above.

What gives? Google isn't being much help. Do I need to put the variables into the EZUpdate constructor as well, or are they fine simply as members of the EZUpdate class?
 
System.Windows.Forms.Form inherits System.Windows.Forms.Control, and thus has the "parent" property, but since a form is a top level control, I don't know that it's ever used for anything.

That said, look at your code for the parent form, and then look at the code I posted for the same task. You might see a difference. :)
 
I notice you set the Parent property of the AboutYou class. Is that supposed to be the MDI Child? If so, then it needs to have its MdiParent property set, not its Parent property.
 
[MS] said:
System.Windows.Forms.Form inherits System.Windows.Forms.Control, and thus has the "parent" property, but since a form is a top level control, I don't know that it's ever used for anything.

That said, look at your code for the parent form, and then look at the code I posted for the same task. You might see a difference. :)

D'oh, I see what you and Velox are talking about. Dammit. I'll go change that and see what works.
 
Yup, that worked. Now I just have to write some try/catch blocks to populate the About You form from saved data and provide a confirmation to the user when they actually hit the "Save" button. Should be fairly easy, actually.

Thanks for the help!
 
Back
Top