473,406 Members | 2,745 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,406 software developers and data experts.

String to control name

I see that this question has been asked before in thye newsgroups, but I
cannot find a suitable answer. I have created a user control that has quite
a number of buttons and textboxes. These I have specifically given very
similar names - e.g. button - cmdSodium, cmdPotassium, txtSodium,
txtPotassium. The user will press a command button with the caption Sodium
on it, and I want that click of the button to access the text in the
relevant textbox - i.e. txtSodium.Text. I obtain the name of the button
being used with sender.text.ToString, and I obviously get Sodium. How can I
convert this string to access the text in the relevant textbox - i.e.
txtSodium.Text. I was hoping that I could do this very easily and place very
generic code in each of the Click events.

Many thanks in anticipation.

Paul Bromley
Nov 21 '05 #1
7 1859

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...
I see that this question has been asked before in thye newsgroups, but I
cannot find a suitable answer. I have created a user control that has
quite
a number of buttons and textboxes. These I have specifically given very
similar names - e.g. button - cmdSodium, cmdPotassium, txtSodium,
txtPotassium. The user will press a command button with the caption Sodium
on it, and I want that click of the button to access the text in the
relevant textbox - i.e. txtSodium.Text. I obtain the name of the button
being used with sender.text.ToString, and I obviously get Sodium. How can
I
convert this string to access the text in the relevant textbox - i.e.
txtSodium.Text. I was hoping that I could do this very easily and place
very
generic code in each of the Click events.

Many thanks in anticipation.

Paul Bromley


I would recommend using a NameValueCollection object and store the key/value
(key=button id, value=associated textbox id). Then you can use
Me.FindControl(associated textbox id) to get the textbox control.

HTH,
Mythran

Nov 21 '05 #2
"Paul Bromley" <fl*******@dsl.pipex.com> schrieb:
I have created a user control that has quite
a number of buttons and textboxes. These I have specifically given very
similar names - e.g. button - cmdSodium, cmdPotassium, txtSodium,
txtPotassium. The user will press a command button with the caption Sodium
on it, and I want that click of the button to access the text in the
relevant textbox - i.e. txtSodium.Text. I obtain the name of the button
being used with sender.text.ToString, and I obviously get Sodium. How can
I
convert this string to access the text in the relevant textbox - i.e.
txtSodium.Text.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Paul,

In addition to the others.

A very nice method in this that I prefer is in my opinion to use for this
kind of operations is the Tag property. (is standard in Control). You can
than select with using the same name all your controls which are in the way
as you have described it in one time, with the same name withouth any
cuttiong of or whatever or creating extra collections (For me is the problem
with the last that if I delete a control, I have to change the collection as
well, however as well a fine way, before you misunderstand me).

Be aware that the tag is a object and therefore you have to get the string
as

dim str as string = mycontrol.tag.tostring

I hope this helps,

Cor
Nov 21 '05 #4
Hi Cor,

This does sound an interesting way of doing it but can you explain more how
I may use this method to do what I want?? I want to click on a command
button - cmdSodium, and have this access the text in a text box called
txtSodium. I want to do this with a number of controls in the same way -
cmdPotassium uses the text in txtPotassium. I was hoping to place generic
code in the click function of these buttons to keep things simple. However,
I'm beginning to think I would be better off just writing individual code
for each click function. I am not familiar with the NameValueCollection
object. I managed to get Herfried's technique working in a modified form to
produce generic code in a simple application, but for some reason it did not
work in my user control. I will take a look at the NameValueCollection
object method. I thought that there may have been a very simple one or two
liner way of achieving this.

Many thanks to all for your help.

Paul Bromley

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ez**************@TK2MSFTNGP15.phx.gbl...
Paul,

In addition to the others.

A very nice method in this that I prefer is in my opinion to use for this
kind of operations is the Tag property. (is standard in Control). You can
than select with using the same name all your controls which are in the way as you have described it in one time, with the same name withouth any
cuttiong of or whatever or creating extra collections (For me is the problem with the last that if I delete a control, I have to change the collection as well, however as well a fine way, before you misunderstand me).

Be aware that the tag is a object and therefore you have to get the string
as

dim str as string = mycontrol.tag.tostring

I hope this helps,

Cor

Nov 21 '05 #5
"Paul Bromley" <fl*******@dsl.pipex.com> schrieb:
This does sound an interesting way of doing it but can you explain more
how
I may use this method to do what I want?? I want to click on a command
button - cmdSodium, and have this access the text in a text box called
txtSodium. I want to do this with a number of controls in the same way -
cmdPotassium uses the text in txtPotassium. I was hoping to place generic
code in the click function of these buttons to keep things simple.


\\\
Me.cmdPotassium.Tag = Me.cmdPotassium
Me.cmdSodium.Tag = Me.cmdSodium
....
..
..
..
Private Sub Button_Click( _
ByVal sender As Object, ByVal e As EventArgs _
) Handles cmdPotassium.Click, cmdSodium.Click, ...
DirectCast(DirectCast(sender, Button).Tag, TextBox).Text = "Bla"
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
Paul,

If I understand your problem well, than

Open a new project drag two buttons on a form and two labels.
Put in the label1 and button1 in the tag a 1 and the others in the tag a 2.

Than try this code.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctr As Control In Controls
If TypeOf ctr Is Button Then
AddHandler ctr.Click, AddressOf Clicked
End If
Next
End Sub
Private Sub Clicked(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
For Each ctr As Control In Controls
If TypeOf ctr Is Label Then
If ctr.Tag.ToString = _
DirectCast(sender, Button).Tag.ToString Then
ctr.Text = Now.TimeOfDay.ToString
End If
End If
Next
End Sub
///

I hope that this gives some ideas

Cor
Nov 21 '05 #7
Many thanks Herfried & Cor - your comments have helped a lot. Mnay thanks
Cor - tried your example and this is exactly what I want.

Best wishes

Paul Bromley

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
Paul,

If I understand your problem well, than

Open a new project drag two buttons on a form and two labels.
Put in the label1 and button1 in the tag a 1 and the others in the tag a 2.
Than try this code.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctr As Control In Controls
If TypeOf ctr Is Button Then
AddHandler ctr.Click, AddressOf Clicked
End If
Next
End Sub
Private Sub Clicked(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
For Each ctr As Control In Controls
If TypeOf ctr Is Label Then
If ctr.Tag.ToString = _
DirectCast(sender, Button).Tag.ToString Then
ctr.Text = Now.TimeOfDay.ToString
End If
End If
Next
End Sub
///

I hope that this gives some ideas

Cor

Nov 21 '05 #8

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

Similar topics

3
by: carl.barrett | last post by:
Hi, I have a number of buttons on a form which run mailmerges. Next to each button is a text box/control that the user enters a date into when the letter was created/merged. When the user...
6
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file =...
1
by: Paul Bromley | last post by:
Can someone tell me if there is any way of doing this? As an example - let us say that I have a button called cmdLetter and a textbox called txtLetter, can I in some way manipulate the names of...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
5
by: glenn | last post by:
Hi folks, The problem I have is that a query string works if hard-coded but if I pass a variable to it, it does not work as shown here. This works: querystring="SELECT * FROM USERS WHERE...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
4
by: Dave | last post by:
If I have a string field that contains the name of a User Control, how could I access the User Control? I need something like the following function: dim ucUC as UserControl Dim strUC as string...
1
by: Aur_Ros | last post by:
I am trying to get a string that contains the HTML code generated for a table control, Need help, examples about how to use render method and string builder, Thanks !!!
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". ...
9
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches...
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
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: 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
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
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...
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
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.