Without implementing an Interface, there are two simple methods to reference an existing Form class from another class.
Passing the reference of the caller class (this
) in the constructor of the callee:
var f2 = new Form2(this);
f2.Show();
Using the Owner property of the callee (Form2
). The Owner is set using the Show(Owner) or ShowDialog(Owner) methods. this
is the instance of the caller:
var f2 = new Form2();
f2.Show(this);
You could also have a public Property in the callee (Form2
), used to set the current caller (this
):
var f2 = new Form2();
f2.MyCaller = this;
f2.Show();
Pretty much useless since the two former methods already achieve the same result using the standard features. There are other means, of course, but pretty much overkill in this context.
Here, I'm using the Owner
property to access the instance of the Form
class that instantiated your Search
Form class. The example uses a public method of the caller class (your InvoiceForm
), which the callee (your Search
Form) uses to pass back the values a user selected.
Using Form.Show(this)
also implies that the Form shown will be parented (not to be confused with the Parent
property, though) with the Form that showed it and will stay on top of it.
You could also use the ShowDialog(this)
method, if it's preferable in your case. The Form will be shown as modal dialog in this case.
I'm making two examples using this public method:
- A public method with a class parameter, which contains all the values that can be set in the
InvoiceForm
controls. This is probably the preferred method to pass these values, because it can be more easily extended and re-used in different contexts.
- A public method with string parameters, corresponding to the TextBoxes values to set
Public method with a class parameter:
Note that this.Owner is InvoiceForm frm
is used to identify the current Owner
.
The UpdateMyControls
class is the container used to transfer specific values. The SearchForm
could act differently if the Owner was a different one.
This is somewhat simplified, but you can use this selection to re-use the SearchForm
with different callers, having different results for each Owner
.
Note: The class used to transfer the values/references, could be passed in the contructor of SearchForm
, possibly using a well-known contract (an Interface), which defines the values and their types. Too broad to describe here, but you should consider exploring this possibility.
public partial class InvoiceForm : Form
{
public class UpdateMyControls
{
public string CodeText { get; set; }
public string NameText { get; set; }
public string BlahText { get; set; }
}
private void btnSearch_Click(object sender, EventArgs e)
{
var searcher = new SearchForm();
searcher.Show(this);
}
public void UpdateControls(UpdateMyControls allValues)
{
this.CodeTextBox.Text = allValues.CodeText;
this.NameTextBox.Text = allValues.NameText;
this.BlahTextBox.Text = allValues.BlahText;
}
}
public partial class SearchForm : Form
{
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
if (this.Owner is InvoiceForm frm) {
InvoiceForm.UpdateMyControls updateClass = new InvoiceForm.UpdateMyControls();
updateClass.CodeText = sqldr[codecolumn].ToString();
updateClass.NameText = sqldr[Namecolumn].ToString();
updateClass.BlahText = sqldr[Blahcolumn].ToString();
frm.UpdateControls(updateClass);
this.Close();
}
}
}
}
Public method with multiple parameters:
public partial class InvoiceForm : Form
{
private void btnSearch_Click(object sender, EventArgs e)
{
var searcher = new SearchForm();
searcher.Show(this);
}
public void UpdateControls(string Code, string Name, string Blah)
{
this.CodeTextBox.Text = Code;
this.NameTextBox.Text = Name;
this.BlahTextBox.Text = Blah;
}
}
public partial class SearchForm : Form
{
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
string CodeValue = sqldr[codecolumn].ToString()
string NameValue = sqldr[Namecolumn].Tostring
string BlahValue = sqldr[Blahcolumn].Tostring
if (this.Owner is InvoiceForm frm) {
frm.UpdateControls(CodeValue, NameValue, BlahValue);
this.Close();
}
}
}
}