4 Replies - 5532 Views - Last Post: 22 December 2012 - 02:03 PM Rate Topic: -----

#1 FatBarry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 13-August 12

Referring to textbox by array value

Posted 21 December 2012 - 01:57 AM

Hi all,

I have 6 textboxes, each of which has a number at the end of the name (1-6).I also have an array (numbers) holding 6 values.

I'm currently using the code below to place the array value in the corresponding textbox :-

                    foreach (Control ctl in Controls)
                    {
                        var box = ctl as TextBox;
                        if (box != null)
                        {
                            if (box.Name.Contains((x + 1).ToString()))
                            {
                                box.Text = numbers[x].ToString();
                                box.Refresh();
                            }
                        }
                    }


As I'm new to c#, I was wondering if there was a more elegant way of completing this task. I'd be glad of any help.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Referring to textbox by array value

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Referring to textbox by array value

Posted 21 December 2012 - 05:25 AM

I'm assuming x is some kind of indexer variable you're increasing in a loop. (On a side note, a standard convention is to name and indexing variable i, unless used for X and Y coordinates or multi-dimensional arrays)

Question: Is Controls A) a collection of Controls (as it sounds) or B) a collection of TextBox controls?

If A, you're trying to cast a Control to TextBox, one of its derived classes. If it fails, it will cast to null, so you're right in catching in this way. But you could also use if(ctl is TextBox) before trying to cast.

If B, then maybe Controls is a bad name. TextBoxes might suit better. And you can use foreach(TextBox box in TextBoxes) so you don't need lines 3-5.

Your condition on line 6 is checking if a textbox's name contains x + 1. You might also have if(box.Name == "textbox" + (x+1)) but that depends on preference. I don't see anything wrong with how you're doing it: in fact, it's more robust.

To be honest, from the small sample I see here, your code is not half bad for a newbie. Well done.
Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: Referring to textbox by array value

Posted 21 December 2012 - 05:35 AM

Quote

Is Controls A) a collection of Controls (as it sounds)


I'm guessing it's the form's ControlCollection.

FatBarry, where do the numbers in your array come from?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Referring to textbox by array value

Posted 21 December 2012 - 05:43 AM

I'd make an array of the controls you wish to do this with. Then just loop over that:
TextBox[] numberBoxes = new TextBox [] { this.numBox1, this.numBox2 };

for(int i=0; i<numbers.Length; i++) {
	numberBoxes[i].Text = numbers[i].ToString();
	// shouldn't need this box.Refresh();
}


Was This Post Helpful? 1
  • +
  • -

#5 FatBarry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 13-August 12

Re: Referring to textbox by array value

Posted 22 December 2012 - 02:03 PM

Thanks for the replies. Mr Shoes, thank you for the positive feedback. I ended up making an array of the textboxes as suggested by baavgai.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1