473,761 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

index number of control array in C#

1 New Member
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 8257
GaryTexmo
1,501 Recognized Expert Top Contributor
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 Recognized Expert Expert
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
11730
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
7117
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 = Array("A", "B", "C", "D", "E", "F", "G", _ "H", "I", "J", "K", "L", "M", "N", _ "O", "P", "Q", "R", "S", "T", "U", _ "V", "W", "X", "Y", "Z")
29
5475
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 elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
17
1601
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 System.EventArgs) Handles MyBase.Loa Dim i As Integer = Dim myPic(10) as pictureBo Dim yAxis As Integer = For i = 0 To MyPic(i) = New PictureBo
2
2254
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 file, which is uploaded to a webserver. Then students and teachers can view it online, but there are also a couple of computers with 19" monitors standing around the school to display the webpage (IE kiosk mode). The program has a form containing...
4
3609
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 of testing if there is data than testing the length of the xml string I used as stringreader to create the dataset, but thats a side issue. I think I tried isdbnull and is nothing and stuff like that and they cause
3
1909
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 records will vary, so I need to make the array dynamic. Can someone remind me how I can increment the index when I write a new record? Here's a sample of the code I wrote: If rsGETKD.EOF = False Then Dim KDLOTSQ
6
2725
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 action buttons, such as "Edit", "Add", and "Comment". Since I do not know how many rows of data will be retrieved - and therefore how many buttons I need - I am using button arrays for each button, like so: echo "<input type=\"submit\"...
7
2040
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
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.