index number of control array in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmad Sheeraz Saeed
    New Member
    • Jul 2009
    • 1

    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
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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.

    Code:
    ...
            public const int NUM_TEXTBOXES = 10;
    
            public TextBox[] tbArray = null;
            public List<TextBox> tbList = null;
    
            public Form1()
            {
                InitializeComponent();
    
                tbArray = new TextBox[NUM_TEXTBOXES];
                for (int i = 0; i < tbArray.Length; i++)
                {
                    tbArray[i] = new TextBox();
                    tbArray[i].Text = "TextBox " + i.ToString();
                    tbArray[i].Location = new Point(8, 8 + i * (tbArray[i].Size.Height + 8));
                    tbArray[i].Enter += new EventHandler(GenericTBEnterHandler);
                }
    
                foreach (TextBox tb in tbArray)
                    this.Controls.Add(tb);
    
                tbList = new List<TextBox>();
                for (int i = 0; i < NUM_TEXTBOXES; i++)
                {
                    TextBox newTB = new TextBox();
                    newTB.Text = "TextBox (List) " + i.ToString();
                    newTB.Location = new Point(tbArray[i].Location.X + tbArray[i].Size.Width + 8, tbArray[i].Location.Y);
                    newTB.Enter += new EventHandler(GenericTBEnterHandler2);
                    tbList.Add(newTB);
                }
    
                foreach (TextBox tb in tbList)
                    this.Controls.Add(tb);
            }
    
            void GenericTBEnterHandler2(object sender, EventArgs e)
            {
                TextBox senderTB = sender as TextBox;
                if (sender != null)
                {
                    int index = tbList.IndexOf(senderTB);
                    if (index >= 0)
                        Console.WriteLine(string.Format("TextBox (List) at index {0} activated!", index));
                }
            }
    
            void GenericTBEnterHandler(object sender, EventArgs e)
            {
                TextBox senderTB = sender as TextBox;
                if (sender != null)
                {
                    int index = -1;
                    for (int i = 0; i < tbArray.Length; i++)
                    {
                        if (senderTB == tbArray[i])
                        {
                            index = i;
                            break;
                        }
                    }
    
                    if (index >= 0)
                        Console.WriteLine(string.Format("TextBox at index {0} activated!", index));
                }
            }
    ...

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Is there a particular reason you need to know which textbox is what index?

      Comment

      Working...