469,317 Members | 1,915 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

index number of control array in C#

i am new to C# 2005 i created the array of text boxes like this

numerictextbox1.NumericTextBox[] PrReading = new numerictextbox1.NumericTextBox[6];

what i want to know, is it possible for me to display the index number of the text box in which the focus is. for example if i am in first text box the index number will be 0 and if i am in second text box the index number will be 1. I do it this is vb6 but don't know how to do it in c#.

Thanks
Jul 17 '09 #1
2 7991
GaryTexmo
1,501 Expert 1GB
Unless there's a trick I don't know about, the easiest way would be to just search for the selected text box in the array itself. I drew up a quick example using a traditional array and using a list which hooks the Enter event of a TextBox to a method that searches for that textbox in the list.

Hopefully it helps you out.

Expand|Select|Wrap|Line Numbers
  1. ...
  2.         public const int NUM_TEXTBOXES = 10;
  3.  
  4.         public TextBox[] tbArray = null;
  5.         public List<TextBox> tbList = null;
  6.  
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.  
  11.             tbArray = new TextBox[NUM_TEXTBOXES];
  12.             for (int i = 0; i < tbArray.Length; i++)
  13.             {
  14.                 tbArray[i] = new TextBox();
  15.                 tbArray[i].Text = "TextBox " + i.ToString();
  16.                 tbArray[i].Location = new Point(8, 8 + i * (tbArray[i].Size.Height + 8));
  17.                 tbArray[i].Enter += new EventHandler(GenericTBEnterHandler);
  18.             }
  19.  
  20.             foreach (TextBox tb in tbArray)
  21.                 this.Controls.Add(tb);
  22.  
  23.             tbList = new List<TextBox>();
  24.             for (int i = 0; i < NUM_TEXTBOXES; i++)
  25.             {
  26.                 TextBox newTB = new TextBox();
  27.                 newTB.Text = "TextBox (List) " + i.ToString();
  28.                 newTB.Location = new Point(tbArray[i].Location.X + tbArray[i].Size.Width + 8, tbArray[i].Location.Y);
  29.                 newTB.Enter += new EventHandler(GenericTBEnterHandler2);
  30.                 tbList.Add(newTB);
  31.             }
  32.  
  33.             foreach (TextBox tb in tbList)
  34.                 this.Controls.Add(tb);
  35.         }
  36.  
  37.         void GenericTBEnterHandler2(object sender, EventArgs e)
  38.         {
  39.             TextBox senderTB = sender as TextBox;
  40.             if (sender != null)
  41.             {
  42.                 int index = tbList.IndexOf(senderTB);
  43.                 if (index >= 0)
  44.                     Console.WriteLine(string.Format("TextBox (List) at index {0} activated!", index));
  45.             }
  46.         }
  47.  
  48.         void GenericTBEnterHandler(object sender, EventArgs e)
  49.         {
  50.             TextBox senderTB = sender as TextBox;
  51.             if (sender != null)
  52.             {
  53.                 int index = -1;
  54.                 for (int i = 0; i < tbArray.Length; i++)
  55.                 {
  56.                     if (senderTB == tbArray[i])
  57.                     {
  58.                         index = i;
  59.                         break;
  60.                     }
  61.                 }
  62.  
  63.                 if (index >= 0)
  64.                     Console.WriteLine(string.Format("TextBox at index {0} activated!", index));
  65.             }
  66.         }
  67. ...
Jul 17 '09 #2
Plater
7,872 Expert 4TB
Is there a particular reason you need to know which textbox is what index?
Jul 20 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

29 posts views Thread by shmartonak | last post: by
17 posts views Thread by Will | last post: by
3 posts views Thread by Brian Piotrowski | last post: by
6 posts views Thread by sgottenyc | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.