How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control
How to use a string to identify a button control How to use a string to identify a button control
How to use a string to identify a button control
Go Back  Xtreme Visual Basic Talk > > > How to use a string to identify a button control


Reply
 
Thread Tools Display Modes
  #1  
Old 06-22-2010, 03:21 PM
BennieGouws BennieGouws is offline
Newcomer
 
Join Date: Jun 2010
Posts: 3
Default How to use a string to identify a button control


Hi,

I'm a bit new to vb.net and this forum, so please bare with me.

Is there any way to refer to a button by using string?
For example: I want to set a value on specific buttons only, the naming convention is, btnA0, btnA1 etc.. So in a for loop I want to be able to change a setting on each pass. Basically something like: btnA+"string variable here".visible=true.

The variable for the second part of the button name is actually an integer, I hope I have made my problem clear. I have googled and searched for a sollution but did not succeed, please help.

Thanks in advance!
Reply With Quote
  #2  
Old 06-22-2010, 03:27 PM
AtmaWeapon's Avatar
AtmaWeaponHow to use a string to identify a button control AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It is almost always superior to use a control array for accessing controls in this manner. It takes a little bit of setup effort, but it's more reliable than the other method you can use. I always use control arrays unless I am in a really big hurry and plan to throw away the application when finished.

The other method involves taking advantage of the string indexer for the Controls collection of containers. For example, this line of code will get the control named "btn01" from a form:
Code:
Dim theControl As Control = Me.Controls("btn01")
Note that the output of the Controls property is of type Control; if you need something unique to the type of control you are seeking, you'll have to cast:
Code:
Dim theButton As Button = CType(Me.Controls("btn01"), Button)
Also note that this is not recursive. If your button is in a group box, you need to use that group box's Controls collection.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 06-23-2010, 12:42 AM
BennieGouws BennieGouws is offline
Newcomer
 
Join Date: Jun 2010
Posts: 3
Default

Hi,

Thanks for the speedy reply, the control arrays looks promising, I just need to do a bit of reading about them to gain more knowledge. But your other method also looks great for what I want to accomplish. One last thing though, the buttons are created at design-time, would you still recommend control arrays if the buttons just need to be hidden/shown depending on the value of the variable?

Thanks for your help!
Reply With Quote
  #4  
Old 06-23-2010, 08:16 AM
AtmaWeapon's Avatar
AtmaWeaponHow to use a string to identify a button control AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
I always use control arrays unless I am in a really big hurry and plan to throw away the application when finished.
It's more work, but it's more reliable. If you've got tons of controls to manage, it's not hard to assume that one day you might want group boxes or other containers to help make them visually distinct. If you move the controls and you aren't using control arrays, you have to modify the code that accesses the button to be recursive. If you are using control arrays, you don't have to change anything.

It's a personal preference, but I find that a control array makes the code a bit more clear than using names.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply

Tags
control indexing, control name


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control How to use a string to identify a button control
How to use a string to identify a button control
How to use a string to identify a button control
 
How to use a string to identify a button control
How to use a string to identify a button control
 
-->