简体   繁体   English

如何在每个单选按钮上添加变量?

[英]How to add variables on each radio button?

Lets say I have for each radio button different variables. 可以说每个单选按钮都有不同的变量。 How can I do that? 我怎样才能做到这一点? I searched the internet but haven't found nothing. 我搜索了互联网,但一无所获。

radioButton1 radioButton1

int a = 1
int b = 2
int c = 3

radioButton2 radioButton2

int a = 5
int b = 4
int c = 2

its unclear what your asking but I assume you want to change the value of the ints depending on what radio button is checked. 它不清楚您的要求是什么,但我认为您想根据选中的单选按钮来更改int的值。

int a, b, c;
if (radioButton1.Checked)
{
    a = 1, b = 2, c = 3;
}
else
{
    a = 5, b = 4, c = 2;
}

Create a user Control and add those Vars as a properties in a class 创建一个用户控件,并将那些Vars作为属性添加到类中

public class {

prop1 int var1
{
set, get
}
prop2 int var2
{
set, get
}
prop3 int var3
{
set, get
}
}

Like TaW said: You can extend your Button class and add the variables to it. 就像TaW所说的那样:您可以扩展Button类并向其中添加变量。 Not really sure about the syntax but it should be something like this, where RadioButton is the class you want to extend and MyButton will be your custom class. 语法不是很确定,但是应该是这样的,其中RadioButton是要扩展的类, MyButton是您的自定义类。

public class MyButton : RadioButton
{

    public int a;
    public int b;
    public int c;
}

All the methods and properties of RadioButton will also be available to your new class. RadioButton所有方法和属性也将对您的新类可用。 And you can extend it with methods to read and write your additional properties a , b and c . 您可以使用读取和写入其他属性abc方法来扩展它。

private void button1_Click(object sender, EventArgs e)
        {
            int a, b, c;
            if (radioButton1.Checked == true)
            {
                a = 1; b = 2; c = 3;
            }
            if (radioButton2.Checked == true)
            {
                a = 5; b = 4; c = 2;

            }
        }

As well as you can use same thing for radioButton1_CheckedChanged event,it will be depended on your requirement :) Good luck! 您可以对radioButton1_CheckedChanged事件使用相同的东西,这取决于您的要求:)祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM