从textbox1 - form2和textbox2 - form2添加项目到listbox1 - form1? C# [英] Add items from textbox1 - form2 and textbox2 - form2 to listbox1 - form1? C#

查看:117
本文介绍了从textbox1 - form2和textbox2 - form2添加项目到listbox1 - form1? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This code below works well thanks to you.
Please for just one more question.
I would like to add items from Form2 to listBox1 - Form1.
Form2 has textBox1 and textBox2 and button1.
On Form1 I have listBox1
I want to use this code below in this manner described above. Thanks.







foreach (var item in textBox2.Lines.Select(l => textBox1.Text + " " + l).ToArray())
            {
                if (!listBox.Items.Contains(item))
                    listBox1.Items.Add(item);
            }





我的尝试:



类似于:

Form1:



private void button1_Click(object sender,EventArgs e)

{

Form2 f2 = new Form2();

f2.Owner = this;

f2.ShowDialog();

}



public void AddListItem(object text)

{

listBox1.Items.Add(text);

}



表格2:

private void cmdOK_Click_1(对象发件人,EventArgs e)

{

Form1 f1 =(Form1)所有者;

foreach(textBox2.Lines.Select中的var项目(l => textBox1.Text ++ l)。ToArray())

f1.AddListItem(item)

关闭();

}



What I have tried:

something like:
Form1:

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
f2.ShowDialog();
}

public void AddListItem(object text)
{
listBox1.Items.Add(text);
}

Form2:
private void cmdOK_Click_1(object sender, EventArgs e)
{
Form1 f1 = (Form1)Owner;
foreach (var item in textBox2.Lines.Select(l => textBox1.Text + " " + l).ToArray())
f1.AddListItem(item)
Close();
}

推荐答案

停止这样做。不要在表单之间传递表单引用 - 它将它们联系在一起并使以后维护所有内容变得更加困难。

相反,在Form2中设置一个属性,它返回Form1想要显示的信息,并且让它决定如何处理它:

Stop doing that. Don't pass form references between forms - it ties them together and make it a lot harder to maintain everything later.
Instead, set up a property in Form2 which returns the information Form1 wants to display, and let it decide what to do with it:
Form2 f2 = new Form2(); 
f2.ShowDialog();
string[] data = f2.Results;
listbox1.AddRange(data);



Results属性返回前面问题中汇总的不同值。



这样,Form2不必知道Form1甚至存在,也不需要暴露任何东西,以后再进行维护。


The Results property returns the distinct values as assembled in your previous questions.

That way, Form2 doesn't have to know that Form1 even exists, and nothing needs to be exposed making maintenance difficult later.


尝试这样来更新数据父表格



try this to update the data in parent form

foreach (Form f in Application.OpenForms)
          {
              if (f.Name == "Form1")
              {
                  (f as Form1).AddListItem(item);
              }
          }











or

Form form1 = Application.OpenForms.OfType<Form>().SingleOrDefault(k => k.Name == "Form1");
         if (form1 != null)
             form1.AddListItem(item);


简单:在Form1的AddListItem方法中进行测试,而不是Form2的cmdOK事件处理程序。

Simple: do the test in the AddListItem method of Form1 instead of cmdOK event handler of Form2.
using System.Linq;

public void AddListItem(object text)
{
   if (!listBox1.Items.Any(item => item.Text == text))
   {
      listBox1.Items.Add(text);
   }
}


这篇关于从textbox1 - form2和textbox2 - form2添加项目到listbox1 - form1? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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