473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 8238
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

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

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
2
by: deko | last post by:
Can I return the index number of an array if all I have is the element? For example, if I want to index the alphabet, I can put the letters in the array: Dim varLtr As Variant varLtr =...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
17
by: Will | last post by:
Hey guys, i have 5 buttons created on runtime, in vb 6.0, each button had a unique index, how is done in vb.net? bellow is the code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
2
by: John | last post by:
Hello everyone, I'm currently writing a program to keep track of schedule changes at a school. The goal is to have someone using the program to declare changes, then the program writes a html...
4
by: Antoine | last post by:
Herfried and Cor:- I used tracing and actually tracked down the code that was causing the problem most likely. I wonder if you wanted to comment on it. Also I wonder if there is a better way...
3
by: Brian Piotrowski | last post by:
Hi All, I've probably done this before, but for the life of me I can't remember how I did it. I need to move values from a DB table into an array to be used for other queries. The number of...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
7
by: chanshaw | last post by:
Ok I'm looking to find the array index of the entered employee number here is my code #!/usr/bin/perl # Uses module Text::CSV # compiler directives use strict; use warnings; use Text::CSV;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.