How can I move a property from a control to another form already shown?

I have form1, form2 on-screen, both of them already shown.

When I press a button on form1, I want :

form2.txt2.text = form1.txt1.text

Note: form1, form2 are already on -screen, so something like:

form2 f2;

f2 = New form2();

f2.Show()

would produce a second form2 form, and I really don't want that ...

Please ....



Answer this question

How can I move a property from a control to another form already shown?

  • pogmothoin

    you have a couple of options from your example:

  • make the textbox public in form 2, so your caller can access it

  • better way would be to make a public property in form 2, so you can get and set the textbox value from the caller. Example:

    //form 2:

    //global

    public string SetTextBox1

    {

    get { return this.textbox1.Text;}

    set { this.textbox1.Text = value; }

    }

    ///form1:

    Form2 f2 = new Form2();

    f2.SetTextBox1 = this.txtBox1.Text;

    does this help



  • XNA Rockstar

    This is definitely solving my problem (keeping the instance global...).

    Millions of thanks!

    And Application.OpenForms opens new horizons for me. How can I access all the components named "txt_temp" in all the opened forms

    I think Application.OpenFormsIdea.txt_temp.text = "test 2" will not work

     


  • RyanB88

    the textbox is already public in form2, but I can't call it directly (form2 is the class name). By the way, how may I know the name of the instance of the window already on-screen

    And in the second solution, f2=new Form2() doesn't that mean re-instantiation of form2 with another object than the one already onscreen


  • JamesPK

    ah, re-reading your first example, it should be:

    instanceName.Control.Property

    so...

    Form2 f2 = new Form2();

    f2.theTextBox.Text = this.theTextBox.Text; //for example

    I'm not sure how you can find out what instance of the window is currently onscreen. There is an Application.OpenForms which returns you the collection of Forms shown currently on screen, perhaps you can iterate through this collection and see which one is showing.

    as for your last question, yes, this is correct. If you want to still maintain the same instance, you can declare the instance globally and use that throughout your class scope without having to create a new instance, since there will already be an instance declared, but be sure that it has not been disposed of if you show and close form2



  • Matt Garnham

    I tried the code but it didn't retrive nothing.

    So I created my own code, but this time it retrives empty What seems to be the problem

    The purpose is to open a DialogBox (MediaURL) that has a TexBox and a Button, once the button is clicked, the text form the MediaURL TextBox goes to the TextBox in Form1.

    MediaURL Form

    public partial class MediaUrl : Form
    {
    MediaPlayerCode ss = new MediaPlayerCode();
    Form1 ns = new Form1();
    public MediaUrl()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    ss.endereco = textBox2.Text;
    }
    }

    Form1 Form

    public partial class Form1 : Form
    {
    MediaPlayerCode ss = new MediaPlayerCode();
    public Form1()
    {
    }
    private
    void button9_Click(object sender, EventArgs e)
    {
    Form abreurl = new MediaUrl();
    abreurl.ShowDialog();
    textBox1.Text = ss.endereco;
    }
    }

    MediaPlayerURL Class

    class MediaPlayerCode
    {
    string temp;
    public string endereco
    {
    get { return temp; }
    set { temp = value; }
    }
    }


  • Mamatha

    thats different.

    Application.OpenForms gives you a collection of currently opened forms in your application, so you could iterate through each form and get the properties/components you want from the form

    Example:

    foreach(Form currentForm in Application.OpenForms)

    {

    MessageBox.Show(currentForm.txt_temp.Text); //assuming that the form has txt_temp and is exposed as public

    }

    the way you gave an example should work, you are referencing a form at a particular index, so it should be ok



  • How can I move a property from a control to another form already shown?