locked
UI Invoke dosent work RRS feed

  • Question

  • hello i'm making a chat app 

    each client have his own color they can pick when try login,  the color should be change on what they write or they name even on the 

    list who see with user is connected 

    my prob is when the 2nd user login he just color all the other user's with his own color 

    i'm trying to use foreach but its dosent work...

    here is my code   i try 3 diff way to do it ... all same result 

                var AllUsers = new List<User>();
                AllUsers = obj;
                //lstClients.Invoke(new Action(() =>
                //{
                //    AllUsers.ForEach(user =>
                //    {
                //        {
                //            lstClients.ForeColor = user.Color;
                //            lstClients.Items.Add(user);
                //        };
                //    }
                //    );
                //}));
    
    
    
                if (lstClients.InvokeRequired)
                {
    
                    lstClients.Invoke(new Action<List<User>>(Client_OnClientList), AllUsers);
                }
                else
                {
                    lstClients.Items.Clear();
    
                    foreach (var user in AllUsers )
                    {
                        lstClients.ForeColor = user.Color;
                        lstClients.Items.Add(user);
                    }
                    //AllUsers.ForEach(user =>
                    //{
                    //    lstClients.ForeColor = user.Color;
                    //    lstClients.Items.Add(user);
                    //});
                }

    Thursday, September 22, 2016 9:44 AM

Answers

  • The colour for separate items of a ListView control can be set with a code like this:

        var lvi = new ListViewItem( user.Name ) { ForeColor = user.Color };
        lstClients.Items.Add( lvi );

    If you have a ListBox control, where this cannot be done in such manner, then maybe consider switching to ListView (with one or more columns, in Details mode).

    Thursday, September 22, 2016 11:17 AM
  • Hi Daniel,

     

    >>UI Invoke doesn’t work

    Windows form application can’t allow access control in a thread which not create this control,  we can use control’ invoke method to access this control in another thread, control’s InvokeRequired is true  mean  that will  use a delegate to access the control, So, if you want call the invoke method, you must confirm you access the control in a non UI thread.

     

    >>  lstClients.ForeColor = user.Color;

    If you want to difficult  forecolor  into the ListView Items and not set the ListView’s ForeColor ,please use the code below.

    ListViewItem item = new ListViewItem(){ForeColor = user.Color};
    ListView.Items.Add( item);

    Best Regards,

    Ding

     

    Friday, September 23, 2016 8:49 AM

All replies

  • The colour for separate items of a ListView control can be set with a code like this:

        var lvi = new ListViewItem( user.Name ) { ForeColor = user.Color };
        lstClients.Items.Add( lvi );

    If you have a ListBox control, where this cannot be done in such manner, then maybe consider switching to ListView (with one or more columns, in Details mode).

    Thursday, September 22, 2016 11:17 AM
  • Hi Daniel,

     

    >>UI Invoke doesn’t work

    Windows form application can’t allow access control in a thread which not create this control,  we can use control’ invoke method to access this control in another thread, control’s InvokeRequired is true  mean  that will  use a delegate to access the control, So, if you want call the invoke method, you must confirm you access the control in a non UI thread.

     

    >>  lstClients.ForeColor = user.Color;

    If you want to difficult  forecolor  into the ListView Items and not set the ListView’s ForeColor ,please use the code below.

    ListViewItem item = new ListViewItem(){ForeColor = user.Color};
    ListView.Items.Add( item);

    Best Regards,

    Ding

     

    Friday, September 23, 2016 8:49 AM