从Form2中的按钮添加项目到From1中的listBox1 [英] Add items to listBox1 in From1 from a button in Form2

查看:98
本文介绍了从Form2中的按钮添加项目到From1中的listBox1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我的项目中有2个winforms。 Form1和Form2。

Form1中有一个名为listBox1的列表框,Form2中有一个名为button2的按钮。

我想在Form1中的列表框中添加一个字符串用户单击Form2中的button2。



我尝试用那个设置的东西来做那个...

这是我的尝试。 />


////这些行在Form1类中

Hi.

I have 2 winforms in my project. Form1 and Form2.
And there's a list box named listBox1 in Form1 and a button named button2 in Form2.
I want to add a string to the listbox in the Form1 when user clicks the button2 in Form2.

And I tried to do that with that get set thing..
Here's my attempt.

////These lines are in the Form1 class

private string itemToAdd;
public string itemToAddChanged
{
    get
    {
        return itemToAdd;
    }
    set
    {
        itemToAdd = value;
        listBox1.Items.Add(itemToAdd);
    }
}







////和这些行在Form2 button2点击事件中




////And these lines are in the Form2 button2 click event

Form1 form = new Form1();
form.itemToAddChanged = "foo x 2";





所以它不会给listbox1添加东西。

我试过使用公共方法(普通方法和静态方法)将项目添加到listBox1,它也会失败。



这样做的正确方法是什么? />


提前致谢!



So it doesn't add a thing to the listbox1.
And I tried to use public methods (both normal one and static one) to add items to the listBox1 and it fails too.

What is the correct way to do that ?

Thanks in advance !

推荐答案

您正在创建Form1的新实例每次用户单击Form2中的button2;我的猜测是,你永远不会通过调用'Show on it来使该实例可见。



如果Form2是你的主表格,并创建Form1,那么创建 Form1的一个实例:在Form2的Load EventHandler中,通过'Show显示它,然后你会发现你的button2 Click EventHandler将做正确的事:示例1:
You are creating a new instance of Form1 every time the user clicks button2 in Form2; my guess is that you are never making that instance visible by calling 'Show on it.

If Form2 is your Main Form, and creates Form1, then create one instance of Form1: in the Load EventHandler for Form2, make it visible with 'Show, and then you'll find that your button2 Click EventHandler will do the right thing: Example 1:
// in Form2's class-level code
Form1 InstanceOfForm1 = new Form1();

// in Form2's Load Event
private void Form2_Load(object sender, System.EventArgs e)
{
     InstanceOfForm1.Show();
}

// in Form2's button2 click event
private void button2_Click(object sender, EventArgs e)
{
     InstanceOfForm1.itemToAddChanged = "foo x 2";
}

但是,如果Form1是您的主窗体,而Form1创建Form2,则需要调用另一种策略:示例2:

However, if Form1 is your main Form, and Form1 creates Form2, a different strategy is called for: Example 2:

// in Form1's class-level code
Form2 InstanceOfForm2 = new Form2();

// in Form1's Load Event
private void Form2_Load(object sender, System.EventArgs e)
{
     InstanceOfForm2.InstanceOfForm1 = this;
     InstanceOfForm2.Show();
}

// in Form2's class-level code:
public Form1 InstanceOfForm1;

// in Form2's button2 Click EventHandler
private void button2_Click(object sender, EventArgs e)
{
     InstanceOfForm1.itemToAddChanged = "foo x 2";
}

但是,有更好的方法来处理Form1是主窗体的情况,并创建Form2,例如:只将Form1上的ListBox暴露给Form2,或者编写自定义事件单击button2时在Form2中触发,并由Form1订阅。



另一种方法是将Form2上的Button2暴露给Form1;然后Form1可以订阅按钮2的Click事件:示例3:Form1中的

However, there are better ways to handle the case where Form1 is the Main Form, and creates Form2, such as: exposing only the ListBox on Form1 to Form2, or writing a custom Event that is fired in Form2 when button2 is clicked, and which is subscribed to by Form1.

Another alternative is to expose Button2 on Form2 to Form1; then Form1 can subscribe to the Click Event of button2: Example 3:

// in Form1's class-level code
Form2 InstanceOfForm2 = new Form2();

// in Form1's Load Event
private void Form1_Load(object sender, System.EventArgs e)
{
     InstanceOfForm2.Form2Button2 .Click += Form2Button2_Click;
     InstanceOfForm2.Show();
}

private void Form2Button2_Click(object sender, EventArgs e)
{
    itemToAddChanged = "foo x 2";
}

// in Form2's class-level code:
public Button Form2Button2 { set; get; }

// in Form2's Load EventHandler
private void Form2_Load(object sender, EventArgs e)
{
    Form2Button2 = this.button2;
}

在第二个例子中,Form1的实例暴露给Form2的实例;在第三个示例中,只有Form1上的ListBox暴露给Form2。而且,第三个例子假设Form2不需要传递给Form1不同的文本消息,这很可能是不正确的。



希望你从这些例子中得到一些想法。还有更多的方法可以实现通信之间的通信(例如通过使用接口,或者将动态指针Action,Func插入到类中)。 imho,遵循的一个好原则是尝试向任何给定表格(或对象)的外部展示给定任务所需的最小值;这会增加代码的模块性,减少错误的可能性,以及令人费解的副作用。



如果你写出来,我建议你学到更多东西,测试,你自己,一个例子,其中Form2引发一个由Form1订阅的自定义事件,以及一个示例,其中Form1只将ListBox暴露给Form2。

In the second example, the instance of Form1 was exposed to the instance of Form2; in the third example, only the ListBox on Form1 was exposed to Form2. And, the third example makes the assumption that Form2 does not need to "pass" to Form1 different text messages, which is, quite possibly, incorrect.

Hopefully, you'll get some ideas from these examples. There are even more ways to achieve inter-form communication (such as via the use of Interfaces, or the insertion of dynamic pointers Action, Func into Classes). imho, a good principle to follow is to try and expose to the "outside" of any given Form (or Object) only the minimum necessary for a given task; this increases the modularity of your code, and reduces potential for errors, and puzzling side-effects.

May I suggest that you will learn more if you write out, and test, yourself, an example where Form2 raises a custom Event which is subscribed to by Form1, and an example where Form1 exposes only the ListBox to Form2.


这篇关于从Form2中的按钮添加项目到From1中的listBox1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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