Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am designing small application using visual studio 2017 edition. I have two radio buttons in the main from. I have set them checked property to false while designing and set proprty modifiers to public. I have RadioButton_checkedChanged event in the mainFroms.cs as below.
private void radioButtonA_CheckedChanged(object sender, EventArgs e)
    if (radioButtonA.Checked == true)
        Address = 192;
        ToAddress = 150;
private void radioButtonB_CheckedChanged(object sender, EventArgs e)
    if (radioButtonB.Checked == true)
        Address = 255;
        ToAddress = 100;

I have a serial port class where i am receiving data from hardware. I have serial port instance in mainForm.
in serial class i have used mainFrom instance to changed the radio button status. like cheked to true.

What I have tried:

void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
      //retrieve number of bytes in the buffer
       int bytes = configcomPort.BytesToRead;
       //create a byte array to hold the awaiting data
       byte[] comBuffer = new byte[bytes];
       //read the data and store it
       configcomPort.Read(comBuffer, 0, bytes);
       if(comBuffer[0] == 6 && comBuffer.Length > 5)
           Software.FrmMain frmMain = new Software.FrmMain();
           if (comBuffer[3] == 100) frmMain.radioButtonA.Checked = true;

I am not sure why this was not setting RadioButton Checked to true. While debugging i can see the status is true and the RadioButton_CheckChanged event is raising in mainFrom but in the designer radiobutton is empty. it was showing not selected or checked fasle.
Could someone point me where i am doing wrong?? how to solve this??

Thanks in advance.
Updated 5-Mar-19 8:31am
Rob Philpott 28-Feb-19 6:51am    
Why are you creating a new form when data is received? That's weird, and it won't work. That form never gets displayed, and even if it did there would be profound threading issues as mentioned in the answer below...
vebi1000 28-Feb-19 8:19am    
@Rob Philpott When the data received i need to set which system is connected, for setting that i have radio buttons for two systems A and B. Depends on the data i have to set one of them. those radio buttons are in main from. I have another class serial communication. How to call those radio button chechkChanged evnts from SerialPort calss??

Software.FrmMain frmMain = new Software.FrmMain();
if (comBuffer[3] == 100) frmMain.radioButtonA.Checked = true;

As Rob pointed out in the comments, you are creating a new instance of your form, changing its state, and then throwing it away without ever showing it to the user.

You said the COM port is on the main form - the same form as the radio buttons. If that's the case, simply update the radio buttons on the current instance:
if (comBuffer[3] == 100) this.radioButtonA.Checked = true;

If that's not the case, then you'll need to have a reference to the correct FrmMain instance available to your event handler.

Have a look at this series of articles for a better understanding:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
Share this answer
You are trying to set the RadioButton.Checked property from a different thread.
Please read this:
How to: Make Thread-Safe Calls to Windows Forms Controls | Microsoft Docs[^]
There you can find an example of setting the Text property of a TextBox.
private void SetText(string text)
	// InvokeRequired required compares the thread ID of the
	// calling thread to the thread ID of the creating thread.
	// If these threads are different, it returns true.
	if (this.textBox1.InvokeRequired)
		StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetText);
		this.Invoke(d, new object[] { text });
		this.textBox1.Text = text;
Share this answer
jsc42 28-Feb-19 6:28am    
You say "in the designer radiobutton is empty". This is to be expected. The Form Designer shows the design of the form, not what happens at runtime.

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