Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I access a control within a static method? #123

Open
abcdef123ghi opened this issue May 20, 2020 · 1 comment
Open

How do I access a control within a static method? #123

abcdef123ghi opened this issue May 20, 2020 · 1 comment

Comments

@abcdef123ghi
Copy link

abcdef123ghi commented May 20, 2020

I need to show the message on a Datagridview,I tried the follwoing code,but it doesn't work

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
       {
        // handle message received 
        string message = System.Text.Encoding.UTF8.GetString(e.Message);

        string guildstr = Guid.NewGuid().ToString();
        string topicstr = "/lists/thenumber";
        string clientstr = "client";
        string usernostr = "userno";
    
         
        int index = myform.MsgDGView.Rows.Add();
        myform.MsgDGView.Rows[index].Cells[0].Value = "1"; // Exception occured
        myform.MsgDGView.Rows[index].Cells[1].Value = guildstr;
        myform.MsgDGView.Rows[index].Cells[2].Value = topicstr;
        myform.MsgDGView.Rows[index].Cells[3].Value = clientstr;
        myform.MsgDGView.Rows[index].Cells[4].Value = usernostr;
        myform.MsgDGView.Rows[index].Cells[5].Value = message;
        myform.MsgDGView.Rows[index].Cells[6].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        for (int i = 0; i < myform.MsgDGView.RowCount; i++)
        {
            myform.MsgDGView.Rows[index].Cells[0].Value = (i + 1).ToString();
        }

        myform.MsgTextBox.Text = message;
 }
@billbarni
Copy link

I don't know if it is related to the UI thread.
Try this:

Invoke((Action)delegate
            {
                myform.MsgTextBox.Text = message;
            });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants