N
N
Neiro House2020-02-27 12:47:24
C++ / C#
Neiro House, 2020-02-27 12:47:24

How to use an array of object names?

Faced the following problem. There are about a hundred checkboxes in my project and it would be necessary to somehow save the data from the checkboxes (1 or 0). It occurred to me to shove all the names of objects into an array. Now the task is how to extract a value from the array so that the element in the array is listed as the name of the object.
the code I used to create the array:

public string MASSIVE;
public string[] cikl;
 private void Form1_Load(object sender, EventArgs e)

  {
        string first = "CheckBox";
        for (int i = 1; i < 96; i++)
   {
        MASSIVE = MASSIVE + (first + i + ",");
   }
        cikl = (GOD_MASSIVE.Split(','));
 }

Here, and now the task is to make something like this code:
private void Button1_MouseClick(object sender, MouseEventArgs e)
{ 
  string save;       
  for (int i = 0; i<cikl.Length; i++)
{
  if(cikl[0].checked == true)
{
  save = save + ( 1 + ",");
}
else
{
   save = save + ( 0 + ",");
}               
}
}

Anyone have any ideas how to recreate this code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
fan92rus, 2020-02-27
@GlueStack

We create checkboxes based on names and subscribe to the event, when it occurs, we call the desired method with the name.

private void Form1_Load(object sender, EventArgs e)
        {
            string[] names = { "first", "second", "threes" };
            int yPosition = 33;
            foreach (var name in names)
            {
                var checkBox = new CheckBox() { Location = new Point(101, yPosition), Text = name, };

                checkBox.Click += (o, args) => { this.Select(name); };
                this.Controls.Add(checkBox);
                yPosition += 22;
            }
        }

        public void Select(string name)
        {
            MessageBox.Show(name);
        }

V
Vladimir S, 2020-02-27
@hePPer

can be done with reflection

private void button1_Click(object sender, EventArgs e)
        {
            var fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic |
                                                BindingFlags.Instance).Where(w => w.FieldType == typeof(CheckBox))
                .Select(s => ((CheckBox)s.GetValue(this)).Name + "=" + ((CheckBox)s.GetValue(this)).Checked)
                .ToList();

            var fileContent = string.Join(",", fields);

            File.AppendAllText("config.txt", fileContent);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(!File.Exists("config.txt")) return;
            var fileContent = File.ReadAllText("config.txt");
            var fieldsValues = fileContent.Split(',');
            var fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic |
                                                  BindingFlags.Instance).Where(w => w.FieldType == typeof(CheckBox)).ToList();

            foreach(var field in fieldsValues)
            {
                var data = field.Split('=');
                var name = data[0];
                var value = bool.Parse(data[1]);
                var fieldObj = fields.FirstOrDefault(f => f.Name == name);
                if(fieldObj != null)
                {
                    var checkBox = (CheckBox)fieldObj.GetValue(this);
                    checkBox.Checked = value;
                }
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question