Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. i made a chat but now i have a problem with refresh userList after a user disconnect. I tried (userList.Items.Cout -1) but is not working. Any ideas? Thanks.
C#
public partial class PublicChatForm : Form
    {
        
        public PublicChatForm()
        {
            pChat = new PrivateChatForm(this);
            InitializeComponent();
        }
        
        public readonly LoginForm formLogin = new LoginForm();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            formLogin.Client.Received += _client_Received;
            formLogin.Client.Disconnected += Client_Disconnected;
            Text = "TCP Chat - " + formLogin.txtIP.Text + " - (Conectat ca: " + "user" + ")";
            formLogin.ShowDialog();
        }

        public static void Client_Disconnected(ClientSettings cs)
        {
    
        }

        private readonly PrivateChatForm pChat;

        public void _client_Received(ClientSettings cs, string received)
        {
            var cmd = received.Split('|');
            switch (cmd[0])
            {
                case "Users":
                    this.Invoke(() =>
                    {
                        userList.Items.Clear();
                        for (int i = 1; i < cmd.Length; i++)
                        {
                            if (cmd[i] != "Connected" | cmd[i] != "RefreshChat")
                            {
                                userList.Items.Add(cmd[i]);
                            }
                            
                        }
                    });
                    break;
                case "Message":
                    this.Invoke(() =>
                    {
                        txtReceive.Text += cmd[1] + "\r\n";
                    });
                    break;
                case "RefreshChat":
                    this.Invoke(() =>
                    {
                        txtReceive.Text = cmd[1];
                    });
                    break;
                case "Chat":
                    this.Invoke(() =>
                    {
                        pChat.Text = pChat.Text.Replace("user", formLogin.txtNickname.Text);
                        pChat.Show();
                    });
                    break;
                case "pMessage":
                    this.Invoke(() =>
                    {
                        pChat.txtReceive.Text += "Server says: " + cmd[1] + "\r\n";
                    });
                    break;
                case "List":
                    userList.Items.AddRange(txtReceive.Text.Split('*'));
                    userList.Items.RemoveAt(userList.Items.Count - 1);
                    break;
                case "Disconnect":
                    userList.Items.RemoveAt(userList.Items.Count - 1);
                    Application.Exit();
                    
                    break;
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != string.Empty)
            {
                formLogin.Client.Send("Message|" + formLogin.txtNickname.Text + "|" + txtInput.Text);
                txtReceive.Text += formLogin.txtNickname.Text + " says: " + txtInput.Text + "\r";
                txtInput.Text = string.Empty;
            }
        }
Posted
Comments
ZurdoDev 25-Mar-15 12:39pm    
What is the problem?
Member 11377683 25-Mar-15 14:25pm    
If a user logout his username remains on display in userList and disappear only after a user login.
Sinisa Hajnal 26-Mar-15 3:04am    
Do you have login and logout procedures for the users? If you have one and it works, just re-use the code for refreshing the list in the other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900