更新时间:2022-06-12 21:48:31
在不实现接口的情况下,有两种简单的方法可以引用另一个类中的现有Form类。
Without implementing an Interface, there are two simple methods to reference an existing Form class from another class.
在被调用方的构造函数中传递调用方类的引用( this
):
Passing the reference of the caller class (this
) in the constructor of the callee:
Form2 form2 = new Form2(this);
form2.Show();
使用被调用方的所有者属性( Form2
)。使用显示(所有者)或 ShowDialog(Owner)方法。这
是调用者的实例:
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:
Form2 form2 = new Form2();
form2.Show(this);
您也可以在被叫方中拥有一个公共财产( Form2
),用于设置当前呼叫者(此
):
You could also have a public Property in the callee (Form2
), used to set the current caller (this
):
Form2 form2 = new Form2();
form2.MyCaller = this;
form2.Show();
几乎没有用,因为前两种方法已经使用标准功能实现了相同的结果。当然,还有其他方法,但是在这种情况下,它几乎是矫kill过正。
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.
在这里,我使用的是 Owner
属性,以访问实例化 Search
Form类的 Form
类的实例。该示例使用调用者类的公共方法(您的 InvoiceForm
),被调用者(您的 Search
表单)使用该方法传回用户选择的值。
使用 Form.Show(this)
还意味着显示的表单将是 parented (不要与 Parent
属性,但带有显示它并保留在其顶部的窗体。
您也可以使用 ShowDialog(this)
方法(如果您的情况更可取)。在这种情况下,表单将显示为模式对话框。
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 preferrable in your case. The Form will be shown as modal dialog in this case.
我使用此公共方法制作了两个示例:
I'm making two examples using this public method:
InvoiceForm
控件中设置的所有值。这可能是传递这些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.
公共类参数的方法:
请注意, this.Owner是InvoiceForm frm
用于标识当前的所有者
。 UpdateMyControls
类是用于传输特定值的容器。如果所有者是其他所有者,则 SearchForm
的行为可能会有所不同。
这在某种程度上得到了简化,但是您可以使用此选择来重新使用 SearchForm
具有不同的调用方,每个 Owner
的结果都不同。
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
.
注意:用于传输值/引用的类可以在 SearchForm
的构造方法中传递,可能使用众所周知的合同(一个接口),它定义了值及其类型。
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)
{
SearchForm 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 partial class InvoiceForm : Form
{
private void btnSearch_Click(object sender, EventArgs e)
{
SearchForm 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();
}
}
}
}