-1

I am trying to get data from form 1 and use it in form 3. I've done some googling and some have said to use class constructors, ive tried a few things and this is the closest thing i have so far but it still wont work. is anybody able to advise please?

Form1:

string userNameText = userName.Text;
userNameText = Form3.user;

Form2:

public partial class Form3 : Form
{
    public string user
    {
        get { return userName.Text; }
        set { userName.Text = value; }
    }
}

Can anybody see what i am doing wrong here?

Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
Ash
  • 85
  • 2
  • 11
  • what if you make user static(which you're clearly supposed to do), since you're accessing a variable on a class not on an object – barlop Apr 26 '16 at 09:32
  • Form3 doesnt have a static property of user so it cant work like that, so, Form3 needs to have an instance, so is your instance available in form1? – BugFinder Apr 26 '16 at 09:32
  • @BugFinder I think that should be either or rather than (firstly) and secondly. – barlop Apr 26 '16 at 09:33
  • Your example won't even work with plain classes because of the static issue. As a troubleshooting thing you should've tried it with regular classes rather than forms and you'd have then seen it has nothing to do with forms – barlop Apr 26 '16 at 09:34
  • @barlop, you're right, it could be worded better.. – BugFinder Apr 26 '16 at 09:34

1 Answers1

1

Try this

//In your first forms(so form1) button handler
using(Form3 form3 = new Form3()) 
{
  if(form3.ShowDialog() == DialogResult.OK) 
  {
    someControlOnForm1.Text = form3.TheValue;
  }
}

//In Form3

//Define public property to serve value

public string TheValue 
{
  get { return someTextBoxOnForm2.Text; }
}
  • what is the reason for using 'using' when creating an instance of form? I don't see looking at the msdn documentation, form being IDisposable. – barlop Apr 26 '16 at 09:40
  • i see you are correct in using 'using' with new Form3().. and if it weren't IDisposable, it wouldn't have compiled. – barlop May 08 '16 at 17:59
  • @barlop `using` must be used when using `Form.Show`. It is not required with `ShowDialog`. Better practice to always use it though. – Patrick Hofman Sep 21 '16 at 07:49