[英]Updating Form1's widgets by clicking Form2's button in Visual C# Windows Forms
I'm fairly new to Visual C# and I'm writing an GUI app with multiple forms. 我是Visual C#的新手,正在编写具有多种形式的GUI应用程序。 One form is main window, and the rest are some kind of option windows.
一种形式是主窗口,其余形式是某种选项窗口。 When showing an option window, I need to load some data to it (for example a string to window's editbox), then edit it and return back to main window when closing option window.
显示选项窗口时,我需要向其中加载一些数据(例如,窗口编辑框的字符串),然后对其进行编辑并在关闭选项窗口时返回到主窗口。 Is there any simple way I can achieve it?
有什么简单的方法可以实现吗? I've found some solutions like , or c# event handling between two forms , but I can't really conform it to my needs.
我已经找到了一些解决方案, 例如 ,或两种形式之间的c#事件处理 ,但我无法真正使其符合我的需求。 I was thinking about passing data in constructor, but how to get it back?
我当时正在考虑在构造函数中传递数据,但是如何取回它呢? I've found something about ShowDialog, but as I said I'm new to C# (started yesterday ^^) and don't know if I can use it.
我发现了一些有关ShowDialog的信息,但是正如我所说的,我是C#的新手(昨天开始^^),不知道我是否可以使用它。
Any ideas, please? 有什么想法吗?
I found the following previous answer which outlines sending specific properties from the one form to another: 我发现以下先前的答案,概述了将特定属性从一种形式发送到另一种形式:
Send values from one form to another form 将值从一种形式发送到另一种形式
The using keyword will also ensure that the form is cleaned-up properly, here's a link to it's usage (pardon the pun...) : http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx using关键字还将确保正确清理表单,这是表单用法的链接(请双关语): http : //msdn.microsoft.com/zh-cn/library/vstudio/yh598w02。 ASPX
I've run into the same issue to be honest, and I have to say that prior to this discussion I would just pass the parent form itself to the child and alter it in that way. 老实说,我也遇到过同样的问题,我不得不说,在进行讨论之前,我只是将父表单本身传递给孩子,然后以这种方式对其进行更改。 Such as:
如:
ChildForm child = new ChildForm(this); //from the parent
and 和
public ChildForm(ParentForm parent)
{
this.parent = parent;
}
Probably not the best convention though, as you probably don't need to access that much from the parent as the child. 不过,可能不是最好的约定,因为您可能不需要像孩子一样从父级那里访问太多内容。
Thanks guys, I think I finally get it. 谢谢大家,我想我终于明白了。 Idle_Mind, your idea was the easiest in my point of view, so I decided to use it.
Idle_Mind,按照我的观点,您的想法是最简单的,因此我决定使用它。 If someone else has a problem like this, here's what I've coded:
如果其他人有这样的问题,这是我编写的代码:
In main window form: when button is clicked, a new form appears; 在主窗口窗体中:单击按钮时,将出现一个新窗体; after closing it, label1 shows the text typed in that form
关闭它后,label1显示以该形式键入的文本
private void Button1_Click(object sender, EventArgs e)
{
LoadDataForm loaddata = new LoadDataForm("initial value");
if (loaddata.ShowDialog() == DialogResult.OK)
{
label1.Text = loaddata.textBox1.Text;
}
}
In load data form: argument passed in form's constructor appears in textBox1; 在加载数据表单中:传递给表单构造函数的参数出现在textBox1中; textBox1's Modifiers property has to be modified to "public"
textBox1的Modifiers属性必须修改为“ public”
public LoadDataForm(string initvalue)
{
InitializeComponent();
textBox1.Text = initvalue;
}
private void ApplyButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Regards, mopsiok 此致,Mopsiok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.