how to change form background color from a private subroutine...

dtvonly

Member
Joined
Jan 3, 2013
Messages
22
Programming Experience
5-10
Hello. In Form1_load subroutine, I can easily change the background color of the form. No problem there.
Once in another subroutine I can not; i.e. a subroutine that checks for data received: a failed test result would turn the background to red and a pass test result to green.

How can I do this? Thanks.
 
VB.NET:
        Dim Test As Boolean
        'Define Test here


        If Test = True Then
            Me.BackColor = Color.Green
        ElseIf Test = False Then
            Me.BackColor = Color.Red
        End If

To elaborate a bit.
 
This is an example of why you should always provide a FULL and CLEAR description of the problem. When you say:
a subroutine that checks for data received
I'm guessing that you mean the DataReceived event handler of a SerialPort. Is that correct? If so then the issue has nothing to do with the fact that that event handler is a private method. The form's Load event handler is a private method too. The issue is that the DataReceived event is raised on a secondary thread and you cannot affect the UI on any but the UI thread. As such, you're going to have to marshal a method call back to the UI thread in order to set the form's BackColor. I provide step-by-step instructions on how to do that here:

Accessing Controls from Worker Threads
 
Back
Top