我如何从另一种形式访问控件 [英] how do i access a control from another form

查看:54
本文介绍了我如何从另一种形式访问控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过表单2上的按钮单击事件访问表单1上的控件?
我试图用更新的数据填充表格一的组合框.数据在表格2中更新.我该怎么做?

这是我正在使用的代码

how do i access a control on form 1 from a button click event on form 2?
am trying to fill a combo box on form one with the updated data. The data is updated on form two. how do i go about doing this?

this is the code am using

mainForm mf = new mainForm();
mf.intdept.Items.Clear();

推荐答案

您可以:

0)创建一个自定义事件,该事件在单击form2按钮时触发,然后让form1挂接该事件.这样,您可以创建自己的EventArgs派生对象,该事件会将您想要的任何内容传递回form1事件处理程序.

1)将form2中的按钮公开,并在创建表单后,在form1中为form2中的按钮添加事件处理程序.

2)公开form1中的变量/控件,将form1对象传递给form2,然后让form2进行工作.

在这三种方式中,我认为您应该执行第一种.
You could:

0) Create a custom event that is fired when the form2 button is clicked, and let form1 hook that event. This way, you can create your own EventArgs derived object and the event will pass whatever you want back to the form1 event handler.

1) Make the button in form2 public and after the form is created, add an event handler in form1 for the button in form2.

2) Make the variable/control in form1 public, pass the form1 object to form2, and let form2 do the work.

Of the three ways, I think you should do the first one.


这是一个关于表单协作的流行问题.最健壮的方法是在表单类中实现适当的接口.

请查看我过去的解决方案:
如何在列表框之间复制所有项目两种形式 [ ^ ].

-SA
This is a popular question about form collaboration. The most robust method is implementing appropriate interface in form class.

Please see my past solution:
How to copy all the items between listboxes in two forms[^].

—SA


在Form1中:

创建1个标签控件集文本为``Form1''

Label1.Modifier =选择为PUblic


在Form2中

放置Button1:

Form2的button1_click事件下的代码:

Form1 obj =新的Form1();
obj.button1.Text ="raise";
obj.Show();
in Form1 :

Create 1 Label control set text as ''Form1''

Label1.Modifier=select as PUblic


in Form2

place Button1:

code under button1_click event of Form2 :

Form1 obj = new Form1();
obj.button1.Text = "raise";
obj.Show();


这篇关于我如何从另一种形式访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆