String to control name

  • Thread starter Thread starter Paul Bromley
  • Start date Start date
P

Paul Bromley

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
 
Paul Bromley said:
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
 
Paul Bromley said:
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>
 
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
 
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
 
Paul Bromley said:
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
///
 
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
 
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
 
Back
Top