473,414 Members | 1,618 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,414 software developers and data experts.

access form control propertys via control name as string

if i have the name of a control in a string is there anyway to access
its properties ?

Eg

Button btnClickme1;

i want to do this

("btnClickme" + "1").text = "newtext";

is this possible

i am trying to do this becasue i have 8 buttons on the form, they are
only differentiated in the name by a number

Eg

btnClickme1, btnClickme2 etc etc

and i dont know any way of selectively accessing the properties within
a for loop

any advice appreciated
thanks

Peted
May 22 '07 #1
3 7879
for (int i = 1; i < 9;i++)
this.Controls["btnClickme" + i.ToString()].Text = "newtext";

will change the text on all the buttons.

"Peted" wrote:
if i have the name of a control in a string is there anyway to access
its properties ?

Eg

Button btnClickme1;

i want to do this

("btnClickme" + "1").text = "newtext";

is this possible

i am trying to do this becasue i have 8 buttons on the form, they are
only differentiated in the name by a number

Eg

btnClickme1, btnClickme2 etc etc

and i dont know any way of selectively accessing the properties within
a for loop

any advice appreciated
thanks

Peted
May 22 '07 #2
On Tue, 22 May 2007 01:20:23 -0700, <Petedwrote:
i want to do this

("btnClickme" + "1").text = "newtext";

is this possible
In the form containing the controls:

for (int i = 0; i < cbuttonMax; i++)
{
Controls.Find("btnClickme" + i.ToString()).Text = "newtext";
}

Generally speaking, if you know the type of control you're dealing with,
you can always cast it to the appropriate type and access the property
directly. In this case you don't even need to cast, since the Text
property is in the base Control class returned by the Find() method.

Pete
May 22 '07 #3
On Tue, 22 May 2007 01:59:46 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
In the form containing the controls:

for (int i = 0; i < cbuttonMax; i++)
{
Controls.Find("btnClickme" + i.ToString()).Text = "newtext";
}
Sorry...shouldn't post off the top of my head so late. :)

Of course, the reply from tony is correct. You can fix mine by indexing
the array returned by the Find() method (eg "Controls.Find("btnClickme"
+ i.ToString())[0].Text = "newtext";"), but that's overkill.

Note that whether you use Find() or the [] operator tony suggests, it is
better to check the returned value (non-empty array using Find(), non-null
result using []) before attempting to access the Text property. This will
prevent an exception in case there's some bug somewhere that causes your
index to wind up out of range of which controls are actually present.

Pete
May 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: qwerty | last post by:
I have two User controls in a page. Them ID-propertys are example UC1 and UC2. In code behind file they are declared: Public UC1 As UC1 Public UC1 As UC1 From the page I can call them with...
4
by: Martin | last post by:
Hi, I am getting an "Access Denied" error when attempting to browse asp.net pages. I have installed ASP.NET 1.1.4322 on a domain controller. The machine also has ASP.NET 1.0.3705 installed on...
6
by: dbuchanan | last post by:
I have a Windows Forms application that accesses SQL Server 2k from a small local network. The application has been used for weeks on other systmes but a new install on a new machine retruns...
0
by: Olivier Salzgeber | last post by:
Hello everybody I have an application which is grouped in a MainApplication and several Modules (DLL's) The Main Application is basically a Form with a NavigationBar on the Left Side (like in...
9
by: redivider | last post by:
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceException when trying to access any controls via their name using "this.Controls". ...
0
by: yukijocelyn | last post by:
I have experienced a problem here while doing a form for accessing datadbase using MS Access. I'm using the AccessDataSource control, and using Gridview to pull the data from the database. I am able...
7
by: robert.waters | last post by:
I have an Access database frontend linked via ODBC to a large (gigabytes) mysql database. I need to view a large amount of data in a a textbox (variable up to 300K), but I receive a 'there isnt...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.