从另一个表单向列表添加项目时出现问题

Problem with adding item to the list from another form

我想单击“添加按钮”将联系人添加到 ClientApp 中的联系人列表,但没有添加到框中,仅添加到名为用户的列表中。

我想在列表框中看到新的用户昵称。但是,当我从其他形式调用函数 AddContact 时,我在列表框中看不到新用户,这很好。

在属性中我看到这个单元格,名为“dwa”。

有人会帮忙吗?

添加用户:

  public partial class NewUser: Form
  {
    ...

     public void New()
     {
        ClientApp.users.Add(new accounts(textBox1.Text, textBox2.Text));
        ClientApp x = new KlientApp();
        x.AddContact(textBox2.Text);
        this.Hide();
    }
  }

客户端应用程序:

public partial class ClientApp: Form
{
    ...

    public void AddContact(string nick)
    {
        contacts.BeginUpdate();
        contacts.Items.Add(nick);
        contacts.EndUpdate();
    }
}

enter image description here

致 Gellio Gao。 当我关闭程序时,我在 private void Msg 中缝制了 ObjectDisposedException:

 private void ShowMsg()
{
    bool temp = true;
    while( temp == true)
    {
        if(DateTime.Now.Second % 3  == 0)
        {  
            Msg();
            showed.WaitOne();
            showed.Reset();
            showed.WaitOne(1000);                  
        }
    }
}

  private void Msg()
{
    ClientLog.send_msg= "Wyswietl wiadomosci";
    ClientLog.received.Reset();

    Thread wątek = new Thread(new ThreadStart(AsynchronousClient.StartClient));
    wątek.IsBackground = true;
    wątek.Start();

    ClientLog.received.WaitOne();
   
    Invoke(new Action(() =>
    {
        if (ClientLog.send_msg!= "")
        {
        messages.AppendText(ClientLog.send_msg+ Environment.NewLine);
        }
    }));
    showed.Set();
}

您应该将 ClientApp 的一个实例传递给 NewUser,而不是在 NewUser 中创建一个新实例。类似于:

   public partial class NewUser: Form
   {
        private ClientApp _client;
        public NewUser(ClientApp client)
        {
            this._client = client;
        }
    ................
        public void New()
        {
            ClientApp.users.Add(new accounts(textBox1.Text, textBox2.Text));
            this._client.AddContact(textBox2.Text);
            this.Hide();
        }
   }

更新:给出一个使用this._client的例子。