简体   繁体   English

通过包含对象名称的指定部分的字符串(或整数)引用对象的属性 - C#

[英]Reference an object's property by a string (or integer) that contains specified part of the object's name - C#

For example I have a method that returns the value "4"... My button's name is " b4 ". 例如,我有一个方法返回值“4”...我的按钮的名称是“ b4 ”。 I want depending on the number that the method returns to change the Text property of that b "X". 我希望取决于方法返回的数字来更改b “X”的Text属性。 Eeasiest way to do it in C#. 最简单的方法在C#中做到这一点。 I am a beginner so please explain it well... I know this may be a duplicate post. 我是初学者,所以请解释一下......我知道这可能是一个重复的帖子。 But I didn't understand the answers in all similar posts. 但我不明白所有类似帖子中的答案。 The code's layout is something like this: 代码的布局是这样的:

I have an array of five numbers (eg "int[] rnum = {1, 6, 7, 3, 8}")... I also have 5 buttons that are supposed to get disabled depending on the integers given in the array... I have 25 buttons and their names are as follows "b1, b2, b3, b4.... etc.". 我有一个包含五个数字的数组(例如“int [] rnum = {1,6,7,3,8}”)...我还有5个按钮应该被禁用,具体取决于数组中给出的整数...我有25个按钮,它们的名字如下“b1,b2,b3,b4 ......等”。 So what is the easiest way to change a button's "Enabled" property by referencing the button object's name with the integers given in the array... For example the rnum[1] = 6 ==> b6.Enabled = false... I know I can make a switch statement but if there are lots of buttons how can I automate this? 那么通过使用数组中给出的整数引用按钮对象的名称来更改按钮的“启用”属性的最简单方法是什么?例如,rnum [1] = 6 ==> b6.Enabled = false ...我知道我可以做一个switch语句但是如果有很多按钮我怎么能自动化呢?

As @Alex K. mentioned 正如@Alex K.所说

public Button GetButtonByIndex(int index)
{
  return (Button)this.Controls.Find("b" + index, true).FirstOrDefault();
}

then GetButtonByIndex(1) will return b1 , etc. 那么GetButtonByIndex(1)将返回b1等。

You can do it using reflection. 你可以用反射来做。 here is an example: 这是一个例子:

class Foo
{
    public int Bar1 { get; set; }
    public int Bar2 { get; set; }
    public Foo()
    {
        Bar1 = 2;
        Bar2 = 3;
    }
    public int GetBar(int barNum) //return type should be Button for you
    {
        PropertyInfo i = this.GetType().GetProperty("Bar"+barNum);
        if (i == null)
            throw new Exception("Bar" + barNum + " does not exist");
        return (int)i.GetValue(this); //you should cast to Button instead of int
    }
}

and Main: 和主要:

class Program
{
    static void Main(string[] args)
    {
        Foo f = new Foo();
        for (int i = 1; i <= 3; i++)
            try
            {
                Console.WriteLine(f.GetBar(i));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        Console.ReadLine();
    }
}

The output will be: 输出将是:

2
3
Bar3 does not exist

note that while i printed the result of foo.GetBar(i) , you could in your case do something like that: foo.GetButton(i).Enabled = false; 请注意,虽然我打印了foo.GetBar(i)的结果,但在你的情况下你可以这样做: foo.GetButton(i).Enabled = false;

While looking for buttons inside Controls (recursively or of known container) will works, much easier solution (in general) is this 虽然查找Controls内的Controls (递归或已知容器)将起作用,但更容易解决(一般)是这样

var buttons = new[] {b1, b2, b3, b4, b5 }; // can be a field initialized in form constructor
buttons[number - 1].Text = "lalala"; // number is from 1 to 5

If you don't want to convert received number to index , then you can add null as first element into array. 如果您不想将接收到的number转换为index ,则可以将null作为第一个元素添加到数组中。

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

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