如何访问另一个表单的表单控件?
我有两个 Form
类,其中一个有一个 ListBox
。我需要为 ListBox
的 SelectedIndex
属性设置一个设置器,我想从第二个 Form
调用该属性。
目前我正在执行以下操作:
表格 1
public int MyListBoxSelectedIndex
{
set { lsbMyList.SelectedIndex = value; }
}
表格 2
private ControlForm mainForm; // form 1
public AddNewObjForm()
{
InitializeComponent();
mainForm = new ControlForm();
}
public void SomeMethod()
{
mainForm.MyListBoxSelectedIndex = -1;
}
这是执行此操作的最佳方法吗?
I have two Form
classes, one of which has a ListBox
. I need a setter for the SelectedIndex
property of the ListBox
, which I want to call from the second Form
.
At the moment I am doing the following:
Form 1
public int MyListBoxSelectedIndex
{
set { lsbMyList.SelectedIndex = value; }
}
Form 2
private ControlForm mainForm; // form 1
public AddNewObjForm()
{
InitializeComponent();
mainForm = new ControlForm();
}
public void SomeMethod()
{
mainForm.MyListBoxSelectedIndex = -1;
}
Is this the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
让它们成为 Singleton 并不是一个完全坏的主意,但我个人不喜欢那样做。我宁愿将一种形式的引用传递给另一种形式。这是一个例子。
Form1 触发 Form2 打开。 Form2 具有重载构造函数,它将调用 form 作为参数并提供对 Form2 成员的引用。这样就解决了通讯问题。例如,我在 Form1 中将 Label 属性公开为公共,并在 Form2 中进行了修改。
通过这种方法,您可以通过不同的方式进行沟通。
示例项目的下载链接
//您的 Form1
//您的 Form2
(来源:ruchitsurati.net)
(来源:ruchitsurati.net)
Making them Singleton is not a completely bad idea, but personally I would not prefer to do it that way. I'd rather pass the reference of one to another form. Here's an example.
Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.
With this approach you can do communication in different ways.
Download Link for Sample Project
//Your Form1
//Your Form2
(source: ruchitsurati.net)
(source: ruchitsurati.net)
像这样访问表单的控件:
您可以转换为适当的控件类型,示例:
Access the form's controls like this:
You can cast as appropriate control type, Example:
如果您不想像 Joe Dabones 建议的那样循环遍历“所有”控件,还有另一种方法。
在 Form2 中创建一个函数并从 Form1 中调用它。
There is one more way, in case you don't want to loop through "ALL" controls like Joe Dabones suggested.
Make a function in Form2 and call it from Form1.
如果
ChildForm
想要访问ParentForm
将
ParentForm
实例传递给ChildForm
构造函数。If
ChildForm
wants to access theParentForm
Pass
ParentForm
instance to theChildForm
constructor.我通常使用单例设计模式来实现这样的http://en.wikipedia.org/wiki/Singleton_pattern< /a> .我将创建应用程序在单例下运行的主窗体,然后创建我想要在其他区域触摸的窗体和控件的访问器。然后,其他窗体可以获得指向它们想要修改的控件的指针,或者它们想要更改的应用程序主要部分中的数据。
另一种方法是在不同的表单上设置事件以进行通信,并使用主表单作为各种类型的中心,将事件消息从一个表单传递到应用程序中的另一个表单。
I usually use the Singleton Design Pattern for something like this http://en.wikipedia.org/wiki/Singleton_pattern . I'll make the main form that the application is running under the singleton, and then create accessors to forms and controls I want to touch in other areas. The other forms can then either get a pointer to the control they want to modify, or the data in the main part of the application they wish to change.
Another approach is to setup events on the different forms for communicating, and use the main form as a hub of sorts to pass the event messages from one form to another within the application.
这很简单,首先您可以像这样访问其他表单:
(假设您的其他表单是
Form2
)就是这样,您只需在
Form1
的Form2
中的TextBox1
中写入即可。It's easy, first you can access the other form like this:
(let's say your other form is
Form2
)That's it, you just write in
TextBox1
inForm2
fromForm1
.这里还有另一个执行“查找并突出显示”的示例。还有第二个表单(模式),它打开并包含一个文本框来输入一些文本,然后我们的程序在 RichTextBox(在调用表单中)中查找并突出显示搜索到的文本。 为了在调用表单中选择RichTextBox元素,我们可以使用
.Controls.OfType()
方法:也在同一个类中(modal的表单) ,要访问调用表单,请使用@CuiousGeek 的答案中提到的技术:
Here's also another example that does "Find and Highlight". There's a second form (a modal) that opens and contains a textbox to enter some text and then our program finds and highlights the searched text in the RichTextBox (in the calling form). In order to select the RichTextBox element in the calling form, we can use the
.Controls.OfType<T>()
method:Also in the same class (modal's form), to access the calling form use the technique mentioned in the @CuiousGeek's answer: