从另一个类设置单选按钮属性

时间:2019-02-26 15:04:30

标签: c# winforms

我是Windows的新手从设计到使用Visual Studio 2017版本。我的mainFrom中有两个单选按钮。我在主窗体中有两个如下所示的单选按钮Check_changed事件处理程序。

    private void radioButtonSystem_CheckedChanged(object sender, EventArgs e)
    {
        if (radioButtonSystem.Checked == true)
        {
            Address = 192;            
            ToAddress = 150;          
        }
     }

默认情况下,我在设计时将两个单选按钮在属性中都设置为false。

我有一个串行端口类,我在该类中从硬件接收数据,基于传入的数据,我必须将单选按钮之一设置为true。主窗口类拥有串行端口类的实例

我想知道如何访问单选按钮,以及如何将串行类的检查状态更改为true?

谢谢。

编辑:

我已经尝试过这种方法,但是仍然看不到单选按钮被选中。有人可以建议我我在做什么错。 我有这样的串行类中的代码。

    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.radioButtonSystem.Checked = true;
        }
     }

它正在mainfrom中引发RadioButton_checkChanged事件,但是由于它设置为true并在应用程序中被选择,因此未显示。如下图所示

enter image description here

它应该作为系统显示为true并被选中,但我看不到。

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点:

1)您可以设置单选按钮属性Modifiers:public,并且可以从您的串行端口类访问此单选按钮,例如

regex

2)您可以在mainform上声明私有值,并创建getter和setter方法以通过初始化表单对象到达此变量。您可以使用计时器检查此变量状态,如果为true,则使单选按钮处于选中状态。或直接在form1中使用公共变量。

3)使用backgroundworker监听串口类变量,如果变量改变,则直接在主窗体中更改单选按钮状态。

答案 1 :(得分:0)

您应该尝试使其他形式的主窗体失效

GeneralPlayer

这将使您主窗体的所有子级以及单选按钮失效。

另一种方法是在调用主窗体时将其作为参数传递给另一个窗体。然后,您将在已创建的表单上编辑单选按钮。通过创建新实例,您将编辑未显示的实例。 喜欢:

playerUiHandler

在表单构造函数中,您将拥有

Context

然后,您将单选按钮设置为_main选中或未选中的子项。

相关问题