为什么选中的单选按钮未设置为true。 [英] Why radio button checked is not setting to true.

查看:132
本文介绍了为什么选中的单选按钮未设置为true。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2017版设计小型应用程序。我主要有两个单选按钮。我在设计时将check checked属性设置为false,并将proprty修饰符设置为public。我在mainFroms.cs中有RadioButton_checkedChanged事件,如下所示。

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;
    }
}



我有一个串口类,我从硬件接收数据。我在mainForm中有串口实例。
串口类中的
我使用了mainFrom实例改变了单选按钮的状态。喜欢cheked to true。



我尝试过:




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;
       }
    }



我不知道为什么这不是设置RadioButton Checked为true。调试时,我可以看到状态为true,并且在mainFrom中引发了RadioButton_CheckChanged事件,但在设计器中,radiobutton为空。它显示未选择或检查fasle。

有人能指出我在哪里做错了吗?怎么解决这个问题??



提前谢谢。


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.

推荐答案

你正试图设置RadioButton 。来自不同主题的房产。

请阅读:

如何:对Windows窗体控件进行线程安全调用Microsoft Docs [ ^ ]

在那里你可以找到一个设置TextBox的Text属性的例子。

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 });
	}
	else
	{
		this.textBox1.Text = text;
	}
}


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



正如Rob在评论中指出的那样,你正在创建一个新的你的表单的实例,改变它的状态,然后扔掉它而不向用户显示它。



你说COM端口在主窗体上 - 与单选按钮的形式相同。如果是这种情况,只需更新当前实例上的单选按钮:


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;



如果不是这样的话,那么你需要引用你的事件处理程序可用的正确 FrmMain 实例。



看看这一系列文章以便更好地理解:

在两种形式之间传递信息,第1部分:父母对儿童 [ ^ ]

在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]

在两种形式之间转移信息,第3部分:儿童到儿童 [ ^ ]


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[^]


这篇关于为什么选中的单选按钮未设置为true。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆