Hello again!

I have a form (Form1) with some textbox, call it textbox1. With a button event I open a second form and hide de Form1. In this Form2 I dont use the value, I just make other things.. then open a another form, Form3 here I want to use the value from Form1.

I read that turn on public textbox is wrong, then I try to use get set method:

In Form1:

public string getvalue0
       {
           get { return textBox1.Text.ToString(); }
        }

In Form3:

public string getvalue1
        {
           set { textBox1.Text = value; }
        }

I know that I need to use something like this:

Form3 form3 = new Form3();
form3.getvalue1 = getvalue0;

But how? If I use this in Form2:

Form3 form3 = new Form3();
form3.Show();
this.Close();

Thanks a lot.

Recommended Answers

All 11 Replies

In form1, you have defined a get property setter to retrieve the string value, which you can do from form3. However, if you want to set the value of the textbox control on form1 from form3, you need to create a setter set property too. The typical way to do this is to combine the properties as:

// in form1 class, define property (I called it TextBox1):
        public string TextBox1
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        // then, in form3 to set the value of form1.textBox1.Text:
        form1.TextBox1 = "some text";
        // or, to get the value in form3:
        string text = form1.TextBox1;

But how I call in Fom3 for form1?

If i try to type form1.Text** any reference appears to the public string in form1...

But how I call in Fom3 for form1?

If i try to type form1.Text** any reference appears to the public string in form1...

Not sure what you mean. Are you still casting your form3.MdiParent to get your form1 reference? eg:

// in form3...
    Form1 form1 = (Form1)form3.MdiParent;

Sorry DDoubleD,

To clarify this:

MainForm (parent form, load Form1):

Form1 form1 = new Form1();
form1.MdiParent = this;
form1.Show();

Form1 (child form) contains a textBox1 and button:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.MdiParent = Form1.ActiveForm;
            form2.Show();
            this.Hide();
        }

Form2 contains graphs and other stuff and a button that open a Form3:

private void button6_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.MdiParent = Form1.ActiveForm;
            form3.Show();
            this.Close();
        }

And now is in this Form3 that I want to get the value from Form1.textBox1...

Sorry for not explaining it well.

I don't see anything right away that would prevent you from accessing that property. Can you zip up and attach your project?

I make a similar project with only the minimum stuff.

attach proj.zip

Thanks a lot.

Sorry DDoubleD,

To clarify this:

MainForm (parent form, load Form1):

Form1 form1 = new Form1();
form1.MdiParent = this;
form1.Show();

Form1 (child form) contains a textBox1 and button:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.MdiParent = Form1.ActiveForm;
            form2.Show();
            this.Hide();
        }

Form2 contains graphs and other stuff and a button that open a Form3:

private void button6_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.MdiParent = Form1.ActiveForm;
            form3.Show();
            this.Close();
        }

And now is in this Form3 that I want to get the value from Form1.textBox1...

Sorry for not explaining it well.

An MDIParent container paradigm has only one MDI parent, but you are using the MdiParent property for each successive form, which leaves the MdiParent property reference "dangling" if you close one of those forms, and also defeats having a Parent control...

I'm not sure, but it looks like you are trying to create an MDI Form from scratch? Did you know that there is an "MDI Parent Form" option from the VS -> Add New Item -> Windows Forms dialog? If you use this option to create an MDIParent Form, it will generate a lot code in the parent form, and you will see a method that looks like this, which gets called when you open a new form/document:

private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

If you really want to stick with how you are doing it, I can provide you a solution, but I'd rather make sure I'm not helping you find your way down the wrong path--if you know what I mean.

Let me know...

An MDIParent container paradigm has only one MDI parent, but you are using the MdiParent property for each successive form, which leaves the MdiParent property reference "dangling" if you close one of those forms, and also defeats having a Parent control...

I'm not sure, but it looks like you are trying to create an MDI Form from scratch? Did you know that there is an "MDI Parent Form" option from the VS -> Add New Item -> Windows Forms dialog? If you use this option to create an MDIParent Form, it will generate a lot code in the parent form, and you will see a method that looks like this, which gets called when you open a new form/document:

private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

If you really want to stick with how you are doing it, I can provide you a solution, but I'd rather make sure I'm not helping you find your way down the wrong path--if you know what I mean.

Let me know...

Be sure to read the above consideration. However, I got bored and made modifications to allow you to access any open form, but it will only work with one instance of each of your forms (eg. not two Form2 objects existing at the same time, etc.). There are a g'zillion ways to achieve something like this, and I usually try something different every time it comes up.;) Unfortunately, I had already stripped out your MdiParent settings :(, but it achieves the behavior you were looking for.

If you decide to implement the MDI Parent (generated by VS) as previously described in my last post, you can just ignore this little modification I made; well, you might as well look at it anyway...

I followed your suggestion using MDIParent VS default and changed my idea of use a lot child form in sequence. I will perhaps use panels or tabs, then hide or visible to false.

Only a little question, using MDIParent VS default which is the best way to create the form options (like menustip>Options>Configuration, then display a form) to interact to the childform? Create a winform without being child, then use "get and set"?

Thanks!

I followed your suggestion using MDIParent VS default and changed my idea of use a lot child form in sequence. I will perhaps use panels or tabs, then hide or visible to false.

Only a little question, using MDIParent VS default which is the best way to create the form options (like menustip>Options>Configuration, then display a form) to interact to the childform? Create a winform without being child, then use "get and set"?

Thanks!

>>using MDIParent VS default which is the best way to create the form options (like menustip>Options>Configuration, then display a form) to interact to the childform?

The menu that it generates contains items that represent common menu features found in MDI applications. You can add/remove them to suit your own needs.

>>Create a winform without being child, then use "get and set"?

You may not want to use a MDI approach to what it is you are doing. Because of the way you were trying to implement the Form.MdiParent , I thought I should direct you to the "shell" Windows Form item type that you could add to your project that gives an idea of how to interact with a MDI Parent.

Here is a link to MSDN MDI programming...

It is not clear to me what the purpose of your application is, so I don't want to recommend one way over another. However, using the MdiParent property as you were attempting to do for each form without maintaining a single parent form is a little confusing though; and since you were wanting to access the textBox value from form2 (not form1) from form4, you sort of hit a brick wall trying to do it that way. What you could have done was traverse backwards from the form4.MdiParent-> as form3->MdiParent as form2, but that would be really messy way of doing it...

Thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.