C# 如何将值从子表单传递回父表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/280579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a value from a child back to the parent form?
提问by
How do I pass a value from a child back to the parent form? I have a string that I would like to pass back to the parent.
如何将值从子表单传递回父表单?我有一个字符串,我想传回给父级。
I launched the child using:
我使用以下方法启动了孩子:
FormOptions formOptions = new FormOptions();
formOptions.ShowDialog();
回答by Mitch Wheat
Create a property (or method) on FormOptions
, say GetMyResult
:
在 上创建一个属性(或方法)FormOptions
,比如GetMyResult
:
using (FormOptions formOptions = new FormOptions())
{
formOptions.ShowDialog();
string result = formOptions.GetMyResult;
// do what ever with result...
}
回答by kenny
Many ways to skin the cat here and @Mitch's suggestionis a good way. If you want the client form to have more 'control', you may want to pass the instance of the parent to the child when created and then you can call any public parent method on the child.
在这里给猫剥皮的方法有很多,@Mitch 的建议是一个好方法。如果您希望客户端表单具有更多的“控制”,您可能希望在创建时将父表单的实例传递给子表单,然后您可以在子表单上调用任何公共父方法。
回答by stiduck
You can also create a public property.
您还可以创建公共属性。
// Using and namespace...
public partial class FormOptions : Form
{
private string _MyString; // Use this
public string MyString { // in
get { return _MyString; } // .NET
} // 2.0
public string MyString { get; } // In .NET 3.0 or newer
// The rest of the form code
}
Then you can get it with:
然后你可以得到它:
FormOptions formOptions = new FormOptions();
formOptions.ShowDialog();
string myString = formOptions.MyString;
回答by MusiGenesis
If you're just using formOptions to pick a single value and then close, Mitch's suggestion is a good way to go. My example here would be used if you needed the child to communicate back to the parent while remaining open.
如果您只是使用 formOptions 选择单个值然后关闭,Mitch 的建议是一个很好的方法。如果您需要孩子在保持开放状态的同时与父母沟通,将使用我的示例。
In your parent form, add a public method that the child form will call, such as
在父窗体中,添加子窗体将调用的公共方法,例如
public void NotifyMe(string s)
{
// Do whatever you need to do with the string
}
Next, when you need to launch the child window from the parent, use this code:
接下来,当您需要从父窗口启动子窗口时,请使用以下代码:
using (FormOptions formOptions = new FormOptions())
{
// passing this in ShowDialog will set the .Owner
// property of the child form
formOptions.ShowDialog(this);
}
In the child form, use this code to pass a value back to the parent:
在子窗体中,使用此代码将值传递回父窗体:
ParentForm parent = (ParentForm)this.Owner;
parent.NotifyMe("whatever");
The code in this example would be better used for something like a toolbox window which is intended to float above the main form. In this case, you would open the child form (with .TopMost = true) using .Show() instead of .ShowDialog().
此示例中的代码更适合用于工具箱窗口之类的东西,该窗口旨在浮动在主窗体上方。在这种情况下,您将使用 .Show() 而不是 .ShowDialog() 打开子窗体(使用 .TopMost = true)。
A design like this means that the child form is tightly coupled to the parent form (since the child has to cast its owner as a ParentForm in order to call its NotifyMe method). However, this is not automatically a bad thing.
这样的设计意味着子窗体与父窗体紧密耦合(因为子窗体必须将其所有者强制转换为 ParentForm 才能调用其 NotifyMe 方法)。然而,这并不自然是一件坏事。
回答by Ali Ers?z
You can also create an overload of ShowDialog in your child class that gets an out parameter that returns you the result.
您还可以在您的子类中创建 ShowDialog 的重载,以获取返回结果的 out 参数。
public partial class FormOptions : Form
{
public DialogResult ShowDialog(out string result)
{
DialogResult dialogResult = base.ShowDialog();
result = m_Result;
return dialogResult;
}
}
回答by Ahmed Said
I think the easiest way is to use the Tag property in your FormOptions class set the Tag = value you need to pass and after the ShowDialog method read it as
我认为最简单的方法是在 FormOptions 类中使用 Tag 属性设置需要传递的 Tag = 值,然后在 ShowDialog 方法将其读取为
myvalue x=(myvalue)formoptions.Tag;
回答by abatishchev
Use public property of child form
使用子窗体的公共属性
frmOptions {
public string Result; }
frmMain {
frmOptions.ShowDialog(); string r = frmOptions.Result; }
Use events
使用事件
frmMain {
frmOptions.OnResult += new ResultEventHandler(frmMain.frmOptions_Resukt);
frmOptions.ShowDialog(); }
Use public property of main form
使用主窗体的公共属性
frmOptions {
public frmMain MainForm; MainForm.Result = "result"; }
frmMain {
public string Result;
frmOptions.MainForm = this;
frmOptions.ShowDialog();
string r = this.Result; }
Use object Control.Tag; This is common for all controls public property which can contains a System.Object. You can hold there string or MyClass or MainForm - anything!
使用对象 Control.Tag; 这对于可以包含 System.Object 的所有控件公共属性都很常见。您可以将字符串或 MyClass 或 MainForm 放在那里 - 任何东西!
frmOptions {
this.Tag = "result": }
frmMain {
frmOptions.ShowDialog();
string r = frmOptions.Tag as string; }
回答by Odin
When you use the ShowDialog()
or Show()
method, and then close the form, the form object does not get completely destroyed (closing != destruction). It will remain alive, only it's in a "closed" state, and you can still do things to it.
当您使用ShowDialog()
orShow()
方法,然后关闭表单时,表单对象不会被完全销毁(关闭 != destroy)。它会保持活动状态,只是处于“关闭”状态,您仍然可以对其进行操作。
回答by Bravo Mike
For Picrofo EDY
对于 Picrofo EDY
It depends, if you use the ShowDialog()
as a way of showing your form and to close it you use the close button instead of this.Close()
. The form will not be disposed or destroyed, it will only be hidden and changes can be made after is gone. In order to properly close it you will need the Dispose()
or Close()
method. In the other hand, if you use the Show()
method and you close it, the form will be disposed and can not be modified after.
这取决于,如果您将ShowDialog()
用作显示表单并关闭它的方式,则使用关闭按钮而不是this.Close()
。表单不会被处理或销毁,它只会被隐藏并且在消失后可以进行更改。为了正确关闭它,您将需要Dispose()
orClose()
方法。另一方面,如果您使用该Show()
方法并关闭它,则表单将被处理并且之后无法修改。
回答by ghfarzad
If you are displaying child form as a modal dialog box, you can set DialogResult property of child form with a value from the DialogResult enumeration which in turn hides the modal dialog box, and returns control to the calling form. At this time parent can access child form's data to get the info that it need.
如果将子窗体显示为模式对话框,则可以使用 DialogResult 枚举中的值设置子窗体的 DialogResult 属性,该值又会隐藏模式对话框,并将控制权返回给调用窗体。此时父表单可以访问子表单的数据以获取所需的信息。
For more info check this link: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult(v=vs.110).aspx
有关更多信息,请查看此链接:http: //msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult(v=vs.110).aspx