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
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. - ...
-
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));
-
}
-
}
-
...
Plater 7,872
Recognized Expert Expert
Is there a particular reason you need to know which textbox is what index?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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")
|
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?
|
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
|
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...
| |
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
|
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
|
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\"...
|
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;
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |