The Mask
The Mask

Reputation: 17457

getting textbox value from another form

I have an TextBox named pass in Form1 that I need to get the value of in form2. I tried this:

public partial class Form1 : Form {
    public string GetPass() {
        return pass.Text; 
    }
}

public partial class form2 : Form {
    //... 
    MessageBox.Show(new Form1().GetPass());
}

The above code returns an empty string, why?

Upvotes: 2

Views: 22891

Answers (6)

Abas00125
Abas00125

Reputation: 1

hi you can write this :

 public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }
        internal Form2 F2=new form2();

            private void CommandBarButton1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(f2.TextBox1.Text);

    }

}

Upvotes: 0

nirav patel
nirav patel

Reputation: 465

Define one string variable as Public in declaration section for ex. we have a form with name "frmOne"

public string strVar = string.Empty;

Now, assign the value of TextBox of "frmOne" to that variable from where you are getting the value of Textbox.

for ex.

strVar = Textbox1.Text.ToString();

Now in another form say "frmTwo", you will get access the value of that textbox of "frmOne" something like that (where you want to get the value) :

frmOne frm = new frmOne();
string strValue = frm.strVar;       

So, finally strValue local variable of frmTwo contains the value of Textbox of frmOne.

Upvotes: 4

James Johnson
James Johnson

Reputation: 46067

It's returning empty because you're creating a new instance of the form. Assuming that Form1 is already open somewhere, you need to retrieve the existing instance of Form1 and pull the value from there.

Upvotes: 0

Otiel
Otiel

Reputation: 18743

Because you are creating a new instance of Form1 each time you call GetPass().

You need to get the instance of the opened form1 one way or another, and call GetPass on it:

form1.GetPass();

If there is no specifics on the order of creation of form1 and form2, you can use the following to get the instance of form1:

foreach (Form openedForm in Application.OpenForms) {
    if (openedForm.GetType() == Form1) {
        MessageBox.Show(openedForm.GetPass());
    }
}

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 161002

You are not showing your actual code as evidenced by the syntax errors etc. - the only logical explanation for your problem is that you are not passing the reference to Form1 correctly to Form2, but create a new form instead - that new form would have the empty textbox.

To further help you, please show how you pass the reference to your Form1 in your actual code.

Edit:

Is see your edit now and above is exactly the problem. You have to pass a Form1 instance to form2 instead of creating a new one, i.e.:

public partial class form2 : Form 
{
  private Form1 form1;

  public form2(Form1 otherForm)
  {
     form1 = otherForm;
  }

  public void Foo()
  {
     MessageBox.Show(form1.GetPass());
  }
}

Upvotes: 4

n8wrl
n8wrl

Reputation: 19765

You are creating a NEW form1 where the textbox is likely to be blank, and calling GetPass() on that empty form. You need an instance of the already-opened form1 where the textbox might have a value.

Upvotes: 0

Related Questions