简体   繁体   English

在C#中修改变量名

[英]Modifying Variable Names in C#

I'm not sure if the title is correct because I wasn't sure how to explain it. 我不确定标题是否正确,因为我不确定如何解释。 I've encountered many scenarios where being able to dynamically modify part of a variable name such a suffixing integer could save me a great deal of time and keep my code much cleaner but I'm not sure how to do it. 我遇到过许多情况,能够动态修改变量名的一部分(例如带后缀的整数)可以为我节省大量时间,并使我的代码更整洁,但我不确定该怎么做。 Here's an example of my most recent encounter. 这是我最近遇到的一个例子。

I have 9 PictureBox's in a 3 x 3 grid. 我在3 x 3的网格中有9个PictureBox。 Each PictureBox has a name of cell followed by it's number so cell1, cell2, cell3 etc. I want to get the background colour of each of these cells and assign them to a variable whilst converting them to strings... something like this: 每个PictureBox都有一个单元格名称,其后是数字,因此为cell1,cell2,cell3等。我想获取每个单元格的背景色,并将它们分配给变量,同时将它们转换为字符串,如下所示:

        for (int i = 1; i < 10; i++)
        {
            string ci = celli.BackColor.ToString();
        }

Is there a way I can have the i variable insert only it's numeric value to the placeholder rather than appending an i to the variable name? 有什么方法可以让i变量仅将其数字值插入占位符,而不是在变量名后附加i? Can I wrap it in some sort of bracket? 我可以用某种支架包装吗? I've tried Googleing this but I'm finding it difficult to search for using just keywords. 我已经尝试过使用Google进行搜索,但发现仅使用关键字很难搜索。

Thanks in advance. 提前致谢。

You are probably using a visual form editor, the best way to do this whould probably be to generate the grid by code (and not visually). 您可能正在使用可视化表单编辑器,执行此操作的最佳方法可能是通过代码(而不是可视化)生成网格。

Another solution is to make it a matrix: 另一个解决方案是使其成为矩阵:

PictureBox[,] cell = new PictureBox[,] {
    { cell1, cell2, cell3 },
    { cell4, cell5, cell6 },
    { cell7, cell8, cell9 }
};

string[,] c = new string[3, 3];

for(int y=0; y<3; y++)
    for(int x=0; x<3; x++)
        c[x, y] = cell[x, y].BackColor.ToString();

Good luck with your code. 祝您的代码好运。

You would like to generate a list or collection of all your pictureboxes so that you can access them by specifying their index. 您想生成所有图片框的列表或集合,以便可以通过指定它们的索引来访问它们。 One way is to generate the PictureBoxes on runtime: 一种方法是在运行时生成PictureBoxes:

Like this: 像这样:

List<PictureBox> myPics = new List<PictureBox>();
int picWidth = 100;
int picHeight = 100;
for (x = 0; x <= this.Width; x += picWidth) {
    for (y = 0; y <= this.Height; y += picHeight) {
        PictureBox pic = new PictureBox();
        pic.Image = pic.Image;
        // Your image
        pic.Location = new Point(x, y);
        this.Controls.Add(pic);
        myPics.Add(pic);
    }
}

// Do something with myPics...

The other method is that when you do have all the pictureboxes on your form already, you can iterate through all the controls, check which ones are pictureboxes and then check their Name property to identify their index. 另一种方法是,当窗体上确实具有所有图片框时,可以遍历所有控件,检查哪些是图片框,然后检查其Name属性以标识其索引。 Then do something with them accordingly. 然后相应地对他们进行处理。

foreach (void ctrl_loopVariable in this.Controls) {
    ctrl = ctrl_loopVariable;
    if (ctrl.GetType() == typeof(PictureBox)) {
        if (ctrl.Name == "your picture box name to test") {
            // Do something here with ctrl
        }
    }
}

(The above code is converted from VB to C#, excuse conversion issues) (以上代码已从VB转换为C#,请谅解转换问题)

Your intend here is to dynamically reference those controls. 您的意图是动态引用这些控件。

In order to achieve this, there is two options: 为了实现这一点,有两种选择:

  • You create those controls dynamically 您动态创建这些控件
  • You create dynamic references for the controls created by your form-designer 您为表单设计者创建的控件创建动态引用。

The first point is explained ny Shreyas Kapur's answer. 第一点解释了什雷亚斯·卡普尔的回答。 The second could be cone like this, 第二个可能是像这样的圆锥体

readonly Dictionary<Point,PictureBox> _dynamicMappedBoxes = 
         new Dictionary<Point,PictureBox>();

// Call this once in the beginning ofr your program
void createDynamicMapping()
{
    foreach(PictureBox box in Controls.OfType<PictureBox>())
    {
        Point coords = getCoordinatesFromName(box);
        _dynamicMappedBoxes.Add(coords, box);
    }
}

Point getCoordinatesFromName(PictrueBox box)
{
    int x = int.Parse(box.Name.SubString(IdontKnow);
    int y = int.Parse(box.Name.SubString(IdontKnow);
    retrun new Point(x,y);
}

//usage
string colorName = dynamicMappedBoxes[new Point(x,y)].BackColor.ToString();

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

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