Reputation: 230
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.
Upvotes: 1
Views: 847
Reputation: 654
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;
}
Upvotes: 0
Reputation: 8902
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();
}
Upvotes: 2
Reputation: 3694
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.
Upvotes: 0