0

I've been trying to send a string from one form named mainForm to my other form unlockForm.

The references I've found are sending values from things like textboxes. I simply want to send a string.

Dylan
  • 47
  • 6
  • what did you have tried ? you would use a simple static class to share data. – Ali Adlavaran Jul 08 '15 at 20:14
  • @AliAdlavaran I've tried using stuff like (mainForm form1 = new mainForm) For some reason I can't make the main form in my 2nd class though. Can you link me to an example of a simple static class? I'm pretty new to programming. – Dylan Jul 08 '15 at 20:16

1 Answers1

2

This is done in two ways. If you are trying to pass a string from the owner form to the child form, you can do it in a parameter, like so:

class Owner : Form
{
   private Child Child;
   public Owner()
   {
       Child = new Child("Value To Pass");
   }
}
class Child : Form
{

    public Child(string value)
    {
        //do something with value
    }
}

If you want to pass from the Child to the Owner, it would be like this:

class Owner : Form
{
   public Owner()
   {
       Child = new Child();
       Child.ShowDialog();
       string childValue = Child.Value;
   }
}
class Child : Form
{
    public string Value{get;set;}
    public Child()
    {

    }

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        value = "Value To Set";
        this.Close();
    }
}

Note that I'm using "Child.ShowDialog()" to make sure that the child form is closed before the value returns. This isn't necessary, but safer.

oppassum
  • 1,746
  • 13
  • 22