简体   繁体   中英

Problem with adding item to the list from another form

I would like to click "add button" to add contacts to the list of contacts in ClientApp, but nothing added to box, only to a list named users.

I want to see new usser nickname on the listbox. But when I evokes function AddContact from other form I don't see new usser on the listbox, from this same is good.

In atributes I see this cell, named "dwa".

Someone will help?

AddUsser:

  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();
    }
  }

ClientApp:

public partial class ClientApp: Form
{
    ...

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

enter image description here

To Gellio Gao. I sew ObjectDisposedException in private void Msg when I close program on go:

 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();
}

You should pass an instance of ClientApp to NewUser instead of creating a new instance inside of NewUser . Something like:

   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();
        }
   }

Update: Give a sample of using this._client .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM