1

I have a Winform (C#), how can i receive a value from another form without form load event. Is any possibility to pass the value? I have a Patient Entry Form. i have one search button link to get a Patient ID. Search button have a separate form. it gets open and i choose the ID from the Search Form. how can i send the selected id value to the patient entry form with out form load event.

Madhavan
  • 232
  • 1
  • 7
  • 15
  • 1
    Can you create a form without calling Show on it, that should make it's public methods and propeties available in the code behind without calling form load – owenrumney Jan 23 '14 at 13:58

3 Answers3

3

Add a public property to Patient Entry Form

public string ID { get; set; }

Within the button click event of the first Form set the value of this property and then open the form. Then you can access the ID directly within Patient Entry Form.

private void button1_Click(object sender, EventArgs e)
{
    PatientEntryForm entryForm = new PatientEntryForm();
    entryForm.ID = "selected ID";
    entryForm.ShowDialog();
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
0

Forms can be instantiated without being shown. The load event is a form being shown for the first time. So if you have functions/getters/public variables, you'd be able to access them from another form, provided you aren't cross-threading.

Community
  • 1
  • 1
Slate
  • 3,189
  • 1
  • 31
  • 32
0

there are 2 ways to do that.

1)

Keep in the caller form a reference to the other form like:

private void button1_Click(object sender, EventArgs e)
{ var f = new Form2();
  f.id = "value you want";
  f.Show();

}

2) when you create the form pass in the costructor a reference to the current form

private void button1_Click(object sender, EventArgs e)
    { var f = new Form2(this);
            f.Show();

    }

now you can access the "pather" form in the "children form"

3) when you call show() method

 private void button1_Click(object sender, EventArgs e)
        { var f = new Form2();
                f.Show(this);

        }

now the current form is the owner of the Form2 instance. you can access in the Form2 calling

var owner = this.Owner as Form1;

}

abrfra
  • 634
  • 2
  • 6
  • 24