question

BradleyRogers-3891 avatar image
0 Votes"
BradleyRogers-3891 asked BradleyRogers-3891 answered

How to reach RichTextbox1 from a static method?

vs2019 Windows Form app (not web) there is the Serial IO and its event handler that uses a delegate,
public static void DataReceivedHandler(object sender, SerialDateReceivedEventArgs e) {

serialport sp = (SerialPort)sender;
read, put into string, etc...


}

HOW to get that data into RichTextBox1 ? or if not possible how to even use that data? Since its static it wont see the form. anything Ive tried gets a RUNTIME error saying cross thread something or other. trying to modify RichTextBox1 from a thread that didnt create it. Sorry, you lose!

whats the solution? use of Assembly? Win32 calls making the handler from scratch?

vs-generalwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Why did you make it static? Is it possible to make it a non-static member of the form? (The problem with "cross-thread" can be easily solved).


0 Votes 0 ·

removed static and its the same.

In the static/not delegate handler the serial port is read. is this the problem? should there only be some call to another method that gets the data? unlikley

i made a private method that takes the string and sends it to RichTextbox1.text and it compiles fine, but still get the cross thread error at runtime

thanks

0 Votes 0 ·
BradleyRogers-3891 avatar image
0 Votes"
BradleyRogers-3891 answered BradleyRogers-3891 commented

Thanks, well that got it closer, found the answer to be:

   private delegate void AppendTextDelegate(string text, RichTextBox rtb);
    
         private void AppendText(string text, RichTextBox rtb)
         {
             if (rtb.InvokeRequired)
             {
                 rtb.Invoke(new AppendTextDelegate(AppendText), text, rtb);
             }
             else
             {
                 rtb.AppendText(text + Environment.NewLine);
             }
         }


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


I thought that 'Invoke' is the answer.

0 Votes 0 ·

yes Invoke is the category of the answer but then it needs the delegate, right?

just putting the "Action" in there was a part of it; but by all means, if you have a simpler answer I would be very happy to try it.

0 Votes 0 ·
Viorel-1 avatar image
1 Vote"
Viorel-1 answered

If the "cross-thread" issue is the only problem, then replace the statements like richTextBox1.Text = "some text" with:

richTextBox1.Invoke( new Action( ( ) => richTextBox1.Text = "some text" ) );


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BradleyRogers-3891 avatar image
0 Votes"
BradleyRogers-3891 answered

richTextBox1.Invoke( new Action( ( ) => richTextBox1.Text = "some text" ) );

compiler says "Action"? not known unless I missed something else Action was not defined, not found

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.