Controls in diferents forms

hi i have this problem:

private void Form5_Load(object sender, System.EventArgs e)
{
Form1  frm1 = new Form1();
string name = frm1.textBox1.Text;
MessageBox..Show(name);
}

on the Form5 i wrote that code.....ill explain u what the program do:

u start in Form1 and when u click in a button it ShowDialog the Form5 and in the Form5_Load  it should get the text in the textBox1 of Form1 and MessageBox the text it get.

the problem is that i allways get a blank messagebox... and i allready checked that the textBox1 have text in it.. i also made oublic the textBox1 so... what can i do



Answer this question

Controls in diferents forms

  • Llewellin

    You can do someting, like this in the load

    MessageBox.Show(((Form1)this.Owner).textBox1.Text);

    But now, I ask, why you need to do someting like this



  • Arun_Adonis

    thanks paul as i allready said is allready public... what i dont know is how to call the contol and get the content of it,,, hope u can help  me. thanks

  • ThE ViKinG

    hi, what does this do if i do this will i be able to use the control from other forms in the project

  • melbsurfer

    Hi,

    Sorry, I haven't read all of your post (i really must change this habbit). bistok's post is correct. But just to make it clearer, you should make your Form1 be the Owner of Form5 you can do this by setting the owner property of Form5 to your Form1. Confused well here's a code snippet that does that:

    // On your Form1, upon displaying form5
    private void button1_Click(object sender, System.EventArgs e) {
    Form5 frm5 = new Form5();
    frm5.Owner = this;
    }

    // just like what bistok posted you can access your form like this.
    private void Form5_Load(object sender, System.EventArgs e)
    {
    Form1 frm1 = (Form1) this.Owner;
    string name = frm1.textBox1.Text;
    MessageBox..Show(name);
    }

     

    cheers,

    Paul June A. Domag



  • GauravGarg

    yea man thanks but didnt woekd either i got this exception when loading the Form5:

    Object reference not set to an instance of an object

  • Gaetan

    ok, use this, when calling the ShowDialog method:

    Form5 f = new Form5();

    f.ShowDialog(this);



  • Jeff McKune

    Hi,

    You can change the declaration of your textbox from private into public. To do this, click the control in design vew and in the properties window search for the Modifier property and change it to public...

     

     

    cheers,

    Paul June A. Domag



  • The Edge also develops software

    Hi,

    No, this would just let you access the control on another form. This routine would give you rights to change the other forms button text to something that you want. It gives you control over the controls on the other form.

    BTW, are you an ilocano as in from ilocos, phil

    cheers,

    Paul June A. Domag



  • ASPdeveloper

    That don't work because this: Form1  frm1 = new Form1(); Creates a new instance of the Form1, and that instance don't have text in TextBox1.



  • MTarris

    do you read my post

  • Controls in diferents forms