0

Still pretty new to programming, but I've got a small sort of schedule app I'm working on and the base functionality is: Drag entries from a datagrid onto a panel, three labels nested in the panel update with relevant text, database is queried and the proper cells in the table are saved.

My big problem comes with loading all of that data from the database and populating all of the labels cleanly in one loop. I know what I need to query from the database to get the data back but as far as presenting it in the UI, I'm hoping to be able to something as simple as tie the counter for the loop to the label names but I'm not sure how I could do that if that's even a thing? For example: If all of my labels are named panel1Title, panel2Title, and so on, I was hoping to do something like this where the name of the label selected increments where I've placed numbers but they increment in unison with the counter:

while (X<=42)
{
panelXTitle.Text = "Title";
}

Is it possible to do what I want to do? If no, what's an alternate direction I should be heading towards to achieve the same results?

  • Use an array or dictionary. – Progman Jul 11 '20 at 12:13
  • Do you mean store the data I'm pulling from the db in an array, or store the labels/label names in an array? – YoungvLondon Jul 11 '20 at 12:22
  • "If no, what's an alternate direction I should be heading towards to achieve the same results?" Obviously it can be done, but have you considered using a DataGrid instead of a ton of separate Labels? – Idle_Mind Jul 11 '20 at 13:43
  • __No you can't do that__. You can set the `Name` __property__ but not the __variable names__. The latter must be unique, the former needn't. which makes them rather useless.. – TaW Jul 11 '20 at 14:18
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Jul 11 '20 at 14:20

1 Answers1

1

You can use that (assuming you have labels as you wrote and not panels):

using System.Linq;

var labels = this.Controls.OfType<Label>();
int X = 0;
while ( X++ <= 42 )
{
  var label = labels.Where(p => p.Name == "panel" + X + "Title").FirstOrDefault();
  if (label != null) label.Text = "Title " + X;
}

We take all labels of the form, at the root level, but you can use any container instead of this.Controls, SomePanel.Controls for example (you can use recursion if needed, see below).

Next in the loop we change the Text property matching the search criteria.

You should be able to optimize for example with a regex and/or some Linq method to avoid the loop...

Before the loop

enter image description here

After the loop

enter image description here

You can also create a list of labels and parse it instead of searching them every time by name:

using System.Collections.Generic;

private Dictionary<int, Label> Labels = new Dictionary<int, Label>();

private void FormTest_Load(object sender, EventArgs e)
{
  var labels = this.Controls.OfType<Label>();
  int X = 0;
  while ( X++ <= 42 )
  {
    var label = labels.Where(p => p.Name == "panel" + X + "Title").FirstOrDefault();
    if ( label != null ) Labels.Add(X, label);
  }
}

private void ButtonAction_Click(object sender, EventArgs e)
{
  foreach ( var label in Labels )
  {
    if ( label.Key == 10 )
      label.Value.Text = "A special label";
    else
      label.Value.Text = "Text " + label.Key;
  }

  Labels[5].Text = "Modified text";
}

If you need recurse parsing, you can take a look at:

How to format all textbox values on LostFocus event in Winform

Mouseover controls and display its handle

How to toggle visibility in Windows Forms C#