3

I have 5 textboxes with 5 labels,named like this:

  • text1, text2, text3, etc.
  • label1, label2, label3, etc.

What I want to do is to target each of them and apply the same code, without having to write something individually. I was thinking about a loop like this:

for (int i = 1; i <= 5; i++)
{
    try
    {
        tcpCLient.ConnectAsync(text(i).Text, 80);
        label(i).Text = "Online";
    }
    catch (Exception)
    {
        label(i).Text = "Offline";
    }
}

The problem is that Visual Studio won't let me compile as "The name 'text' does not exist in the current context".

Is this the wrong approach? How would you do this?

Thank you very much!

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Goosfraba
  • 41
  • 8
  • it's web app or desktop , mvc, winforms what ? – Ehsan Sajjad Mar 04 '20 at 12:03
  • 1
    _Guessing_ it is winforms, the form has a Property you can iterate: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controls?view=netframework-4.8 but I'd actually recommend to use a Model, Controller and Binding. – Fildor Mar 04 '20 at 12:07
  • 2
    By the way, that code won't work anyway. You are trying to connect one instance of TcpClient to 4 Endpoints?? – Fildor Mar 04 '20 at 12:10
  • May I ask if you are coming from a VB or JavaScript background? Makes it easier to "speak your language" ... – Fildor Mar 04 '20 at 12:11
  • 1
    If you want to dig into Binding, you can start here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/windows-forms-data-binding – Fildor Mar 04 '20 at 12:18
  • 1
    It is a desktop WinForms in C# with Framework 4.8. I solved this using Clint's method but I will look into Binding as @Fildor suggested! It seems to work like this too as long as I close the TCP after. – Goosfraba Mar 04 '20 at 12:29

4 Answers4

2

Well, the code label(i).Text is not well-formed. That seems like you are trying to call a function label with parameter i and access the member Text of such result.

You could do something like this

Label[] labels = { label1, label2, label3, label4, label5 };
TextBox[] textBoxes = { text1, text2, text3, text4, text5 };

for (int i = 0; i < 5; i++)
{
    try
    {
        tcpCLient.ConnectAsync(textBoxes[i].Text, 80);
        labels[i].Text = "Online";
    }
    catch (Exception)
    {
        labels[i].Text = "Offline";
    }
}
Zereges
  • 5,139
  • 1
  • 25
  • 49
2

"The name 'text' does not exist in the current context".

text[i].Text you can only do this if text type implements IEnumerable

You can do this to enumerate over your controls

var labels = new List<Label> { label1, label2, label3};
var textBoxs = new List<TextBox> { text1, text2, text3};


for (int i = 1; i <= 5; i++)
{
    try
    {
        tcpCLient.ConnectAsync(textBoxs[i].Text, 80);
        labels[i].Text = "Online";
    }
    catch (Exception)
    {
        labels[i].Text = "Offline";
    }
}
Community
  • 1
  • 1
Clint
  • 6,011
  • 1
  • 21
  • 28
  • Thank you very much! This did the trick. I will keep in mind the fact that enumeration work only with arrays. – Goosfraba Mar 04 '20 at 12:27
  • "the fact that enumeration work only with arrays" - it is incorrect statement. any class can implement indexer. not to mention that `List` != array – ASh Mar 04 '20 at 12:53
  • 1
    `labels(i)` is incorrect syntax for access by index. it should be `labels[i]` – ASh Mar 04 '20 at 12:54
  • @ASh, good catch, edited – Clint Mar 04 '20 at 12:57
1

You can iterate a Form's Controls through its ControlCollection.

See Form.ControlCollection.

But mind that this includes all controls. If you want to pick specific ones, you'd have to filter by type and/or name.

That out of the way, I'd strongly advise to go a different route, though. I'd make a UserControl and make use of the MVC pattern in conjunction with Binding.

Also mind, that your code won't work as expected, since it seems to try to connect the same instance of TcpClient to 4 different Endpoints.

Fildor
  • 14,510
  • 4
  • 35
  • 67
0

In windows forms you can iterate over the Forms.Controls to get all the labels and textboxes in a more automated way.

Example:

List<TextBox> textBoxes = Controls.OfType<TextBox>()
    .Where(txt => txt.Name.StartsWith("text"))
    .ToList();

List<Label> labels = Controls.OfType<Label>()
    .Where(lbl => lbl.Name.StartsWith("label"))
    .ToList();

for (int i = 0; i < textBoxes.Count; i++)
{
    try
    {
        tcpCLient.ConnectAsync(text(i).Text, 80);
        labels[i].Text = "Online";
    }
    catch (Exception)
    {
        textBoxes[i].Text = "Offline";
    }
}

Note: This way you can only get the immediate children of the Form. If the textboxes and labels are children of a different parent control (e.g. Panel), you will need to iterate over the Controls property of that parent control. To iterate over all the control of a Form see: Loop through all controls on a form,even those in groupboxes

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37