从第二个窗体向列表框中添加项目

3
我将尝试从另一个表单向列表框添加项目。Form1具有带有“Dummy”项的列表框,当我尝试从该表单添加更多项目时,一切正常。然而,当我尝试从另一个表单(AddContact.cs)添加项目时,没有项目被添加。我将提供来自两个表单的代码。
PS:列表框设置为公共,以便可以从Form1外部访问它。
Form1:
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        list_names.Items.Add("Dummy");
    }

    private void btn_check_Click(object sender, EventArgs e)
    {
        if (list_names.SelectedItem == null)
        {
            MessageBox.Show("No item has been selected.", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (list_names.SelectedItem.ToString() == "Dummy")
        {
            //Dummy code for testing
            MessageBox.Show("Dummy has been selected!");
        }
    }

    private void btn_add_Click(object sender, EventArgs e)
    {
        new AddContact().Show();
    }

    private void btn_remove_Click(object sender, EventArgs e)
    {
        //TODO: Remove items from listbox
    }

添加联系人:

    Form1 form;

    public AddContact()
    {
        InitializeComponent();
        form = new Form1();
    }

    private void btn_add_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == string.Empty)
        {
            MessageBox.Show("No input has been given.");
        }
        else
        {
            //This doesn't work
            string s = textBox1.Text;
            form.list_names.Items.Add(s);
            textBox1.Text = "";
        }
    }
4个回答

0
问题在于你在 AddContact 中创建了新的表单,你需要引用 Form1
Form1 form;

public AddContact(Form1 frm)
{
    InitializeComponent();
    form = frm;
}

而且

private void btn_add_Click(object sender, EventArgs e)
{
    new AddContact(this).Show();
}

只是了解一下,因为这个代码:Form1 form;public AddContact(Form1 frm) { InitializeComponent(); form = frm; }会发生什么:在这个代码中,我们声明了一个名为“form”的Form1对象。然后我们创建了一个名为“AddContact”的构造函数,并且将传入的Form1对象赋值给“form”变量。此后,我们调用该类的初始化组件方法“InitializeComponent()”。 - Freelancer

0
一个小的解决方法是使用事件处理程序。 只需派生您自己的EventArgs即可。
public class AddItemEventArgs : EventArgs
{
    public string Item { get; set; } 
}

将以下代码添加至您的“添加联系人”表单:

public event EventHandler OnAddItemNeeded(object sender, AddItemEventArgs);

btn_add_click 方法中,您需要触发此事件:
this.OnAddItemNeeded(this, new AddItemEventArgs() { Item = textBox1.Text });

在 Form1 中调用 AddContact 的新实例时:

AddContact ac = new AddContact();
ac.OnAddItemNeeded += new EventHandler(this.OnAddItemNeeded);
ac.Show();

同时在Form1中编写处理程序来完成相应的工作:

private void OnAddItemNeeded(object sender, AddItemEventArgs e)
{
    list_names.Items.Add(e.Item);
}

0
public partial class Form1 : Form
{
    public static List<string> lst=new List<string>();
    private void Form1_Load(object sender, EventArgs e)
    {
          if (lst != null)
            listBox1.DataSource = lst;
    }
}


public partial class Form2 : Form
{
       private void button1_Click(object sender, EventArgs e)
       {             
         Form1.lst.Add(textBox1.Text);
         textBox1.Text = string.Empty;
       }
 } 

两个表单代码写在一起,请在使用时将它们分开。 - Pramod Sawant

-1

嗯,你在 AddContact 构造函数中初始化了 Form1,这就是问题所在。

尝试这个:

AddContact:

Form1 form;

public AddContact(Form1 f)
{
    InitializeComponent();
    form = f;
}

private void btn_add_Click(object sender, EventArgs e)
{
    if (textBox1.Text == string.Empty)
    {
        MessageBox.Show("No input has been given.");
    }
    else
    {
        string s = textBox1.Text;
        form.list_names.Items.Add(s);
        textBox1.Text = "";
    }
}

并添加按钮:

private void btn_add_Click(object sender, EventArgs e)
{
    new AddContact(this).Show();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接