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

access control by string

Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another function.

I solved it in Delphi years ago - what about C#?
Can anybody help?

Feb 8 '07 #1
7 1870
Its actually not that difficult, all you have to do do is access the
ControlCollection that contains your buttons. So in the case of a Windows
Form there is a property called Controls (its a ControlCollection) and you
can access members by index (int) or key (string).

So for a quick example I created a very simple WinForm app and placed a
couple buttons on it and placed the following in the form load event
handler:

this.Controls["button1"].Text = "Test";

and sure enough the button text changed from button1 to Test on load. You
will want to cast your control to a button if you want to access button
specific properties/methods. The only reason my example works without a cast
is because Text is inherited from the Control class.

Hope that helps.

-Matt Newman
-http://www.bestsnowman.com/

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another
function.

I solved it in Delphi years ago - what about C#?
Can anybody help?
Feb 8 '07 #2
t f
Hi

Also it might be useful to use

if (this.Controls.ContainsKey("button1"))
{
...
}

to make sure that the button exists

hope that helps too

T F

"Matthew Newman" <be*********@bestsnowman.comwrote in message
news:O$*************@TK2MSFTNGP02.phx.gbl...
Its actually not that difficult, all you have to do do is access the
ControlCollection that contains your buttons. So in the case of a Windows
Form there is a property called Controls (its a ControlCollection) and you
can access members by index (int) or key (string).

So for a quick example I created a very simple WinForm app and placed a
couple buttons on it and placed the following in the form load event
handler:

this.Controls["button1"].Text = "Test";

and sure enough the button text changed from button1 to Test on load. You
will want to cast your control to a button if you want to access button
specific properties/methods. The only reason my example works without a
cast
is because Text is inherited from the Control class.

Hope that helps.

-Matt Newman
-http://www.bestsnowman.com/

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
>Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another
function.

I solved it in Delphi years ago - what about C#?
Can anybody help?

Feb 8 '07 #3
Here is an article on how to create the eqquivalent of "FindControl" for
Windows Forms:
http://www.eggheadcafe.com/articles/20040506.asp
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rob" wrote:
Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another function.

I solved it in Delphi years ago - what about C#?
Can anybody help?
Feb 8 '07 #4
Thanks for fast reply, but my problem is still there...

My Button is a custom control and I cannot access for example:

this.Controls["bmcButton1"].Image

Looks difficult...
Feb 8 '07 #5
Thats easy all you need to do is cast it,

((MyCustomControl)this.Controls["bmcButton1"]).Image

-Matthew Newman
-http://www.bestsnowman.com/

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:22**********************************@microsof t.com...
Thanks for fast reply, but my problem is still there...

My Button is a custom control and I cannot access for example:

this.Controls["bmcButton1"].Image

Looks difficult...
Feb 8 '07 #6
This would work, but I don't want to walk through all controls.

Just:

public void InterpreteRXCOM1(byte c)
{

Button+c.ToString().Image = ...

}

Rob
Feb 8 '07 #7
What you are asking for is the equivalent of the Javascript "eval" Statement.
It doesn't exist in C#.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rob" wrote:
This would work, but I don't want to walk through all controls.

Just:

public void InterpreteRXCOM1(byte c)
{

Button+c.ToString().Image = ...

}

Rob
Feb 8 '07 #8

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...
3
by: Cherry | last post by:
I have an ID Innovation card reader that I am trying to connect to my Access database. The first problem is that when I swipe the card through the reader, it reads the id but it adds digits to the...
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...
5
by: Steve Richter | last post by:
In my user control I want to read the ViewState dictionary of the Parent control. But this sensible idea is not permitted by the compiler: Compiler Error Message: CS1540: Cannot access...
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...
5
by: c676228 | last post by:
Hi, I guess I am confused. In aspx script, I mean (you won't use Codebehind="enrollinfo.aspx.vb", but mix code with html and code together) You can access user control's property directly. Since I...
2
by: job | last post by:
In a sharepoint setup using smartpart to load our user controls using enterprise blocks (data) we are getting some strange errors (logged to the event log). We dont get the error all the time. When...
3
by: shapper | last post by:
Hello, In have a class inside a compiled DLL where I have the following code: ' Create site map XML document Dim siteMap As XmlDocument = New XmlDocument ' Load site map XML document from...
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...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.