[英]Need to access the other winform in c#
I have 2 forms : Form1 , Form2 . 我有2种形式: Form1 , Form2 。 Form1 has checkedlistbox : checkedlistbox1
Form1的清单框为: 清单框1
All i need is when i click a button from Form2 then checkedlistbox item should be clear. 我所需要的只是当我单击Form2中的一个按钮时,checkedlistbox项应清除。 From searching i found and applied this code but didn't work.
通过搜索,我找到并应用了此代码,但是没有用。
using (Form1 form1 = new Form1())
{
form1.checkedListBox1.Items.Clear();
}
but didn't work. 但是没有用 Please suggest some ideas.
请提出一些想法。
You are creating a new form, that you don't show, and are clearing the list box on that form. 您正在创建一个不显示的新表单,并清除了该表单上的列表框。 What you need is a reference to the form you already have opened up.
您需要的是对您已经打开的表单的引用。 So wherever you open up
Form1
(from program.cs maybe?), store the reference to Form1 so that you can use that reference from form2
so call checkedListBox1.Items.Clear();
因此,无论您在何处打开
Form1
(可能是从program.cs?),都存储对Form1的引用,以便可以使用来自form2
引用,因此调用checkedListBox1.Items.Clear();
Because when you do 因为当你做
using (Form1 form1 = new Form1())
you are actually creating a new instance of Form1
. 您实际上是在创建
Form1
的新实例。 That's why it won't work. 这就是为什么它不起作用的原因。 You need to get the current instance of
Form1
. 您需要获取
Form1
的当前实例。
foreach (var item in Application.OpenForms)
{
Form1 form1 = item as Form1;
if (form1 != null)
{
form1.checkedListBox1.Items.Clear();
}
}
or probably 或可能
((Form1) Application.OpenForms["Form1"]).checkedListBox1.Items.Clear();
What you did is creating a new instance of the Form1. 您所做的是创建Form1的新实例。 You need to access the one that is already created (having the list filled up) then do the clear.
您需要访问已创建的列表(已填充列表),然后进行清除。
You must pass the instance of Form1 to Form2, if you want to access that on the currently displayed form. 如果要访问当前显示的表单上的实例,必须将Form1的实例传递给Form2。
If you are displaying Form2 from within Form1 in the following fashion, 如果要以以下方式在Form1中显示Form2,
Form2 form2 = new Form2();
form2.ShowDialog(this);
Then, you can use, 然后,您可以使用
using (Form1 form1 = ((Form1)Owner))
{
form1.checkedListBox1.Items.Clear();
}
There are a few thing you should do: 您应该做几件事:
OnClick
event on form2 OnClick
事件 form1reference.checkedListBox1.Items.Clear();
form1reference.checkedListBox1.Items.Clear();
You are creating new instance of Form1 here it will not work. 您正在创建Form1的新实例,在这里它将无法正常工作。 Use property like Owner etc. Try something like this.
使用诸如Owner等的属性。尝试类似的方法。
var myowner = this.Owner as Form1;
myowner.checkedListBox1.Items.Clear();
检查或将您的Form1复选框的Modifys属性更改为public
in the form 1 set check box 1 modifiers to public and if form1 is already opened form2 code will be: 在“表单1”中,将“复选框1”修饰符设置为public,并且如果已经打开了form1,则form2代码将为:
private void button1_Click(object sender, EventArgs e)
{
form1.checkBox1.Checked = false;
}
Make your CheckBoxList public from Form1.Designer.cs
. 将您的CheckBoxList从
Form1.Designer.cs
公开。
Then 然后
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
//form1.checkedListBox1.SetItemChecked(0, true);
form1.checkedListBox1.Items.Clear();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.