PC Review


Reply
Thread Tools Rate Thread

access form control propertys via control name as string

 
 
Peted
Guest
Posts: n/a
 
      22nd May 2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?dG9ueSBsb2Nr?=
Guest
Posts: n/a
 
      22nd May 2007
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
>

 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      22nd May 2007
On Tue, 22 May 2007 01:20:23 -0700, <Peted> wrote:

> 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
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      22nd May 2007
On Tue, 22 May 2007 01:59:46 -0700, Peter Duniho
<(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i read visio shape propertys with access vbacode =?Utf-8?B?Qm9yRGVXb2xm?= Microsoft Access VBA Modules 2 28th Jan 2007 03:34 PM
Access objects/propertys from another DLL Olivier Salzgeber Microsoft VB .NET 0 31st Jan 2006 11:56 AM
Display Propertys Doug Windows XP Hardware 3 3rd May 2005 03:12 AM
desktop propertys Carl G Windows XP Performance 9 17th Feb 2005 03:41 PM
pentium III 500 reads 249 in system propertys hawaiiantim Windows XP Hardware 3 30th Sep 2004 08:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:58 AM.