Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get control name in a string, then use it

280 views
Skip to first unread message

Kathleen

unread,
Apr 7, 2003, 3:21:48 PM4/7/03
to
Is there any way I can get the id of a control in a string, and then use it
to set properties of that control. For example, if I have a button btnMine,
can I do something like this:

stringA = "btn"
stringB = "Mine"
stringC = stringA & stringB
'now need a way to set btnMine property, like:
(stringC).text = "Click Me" 'like btnMind.text = "Click Me"

...of course, that doesn't work. Is there anything that does?

Thanks,
-Kathleen Christian


Herfried K. Wagner

unread,
Apr 7, 2003, 3:55:24 PM4/7/03
to
Hello Kathleen,

"Kathleen" <not_...@hotmail.com> schrieb:


> Is there any way I can get the id of a control in a string,
> and then use it to set properties of that control.

\\\
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Dim i As Integer
Dim obj As Object
For i = 1 To 40
DirectCast( _
SearchControl( _
Me, _
"pyramide" & i.ToString() _:
), _
Button _
).BackColor = Color.Red
Next i
End Sub

Private Function FindControl( _
ByRef Container As Control, _
ByVal Name As String _
) As Control
Return SearchControl(Container, Name)
End Function

Private Function SearchControl( _
ByRef Control_ As Control, _
ByRef Name_ As String _
) As Control
If Control_.Name = Name_ Then
Return Control_
End If
Dim ctr As Control
Dim c As Control
For Each ctr In Control_.Controls
c = SearchControl(ctr, Name_)
If Not c Is Nothing Then
Return c
End If
Next ctr
End Function
///

Regards,
Herfried K. Wagner


Armin Zingler

unread,
Apr 7, 2003, 3:59:22 PM4/7/03
to
"Kathleen" <not_...@hotmail.com> schrieb

Variable names, like btnMind, shouldn't matter at run time (I've never
needed it so far). What's your intention? Why not use btnMine.text = "Click
Me" ?


--
Armin

Kathleen

unread,
Apr 7, 2003, 4:14:49 PM4/7/03
to
Wow that was fast!
This looks great, I'll try using this in my page and let you know how it
goes.

Thanks!!!
-Kathleen

"Herfried K. Wagner" <hirf...@m.activevb.de> wrote in message
news:em6Im#T$CHA....@TK2MSFTNGP12.phx.gbl...

Liz

unread,
Apr 7, 2003, 4:25:49 PM4/7/03
to

"Herfried K. Wagner" <hirf...@m.activevb.de> wrote in message
news:em6Im#T$CHA....@TK2MSFTNGP12.phx.gbl...

I was considering the kind of code you gave here but wondered if there were
not a more direct approach; in JScript, you can do this sort of thing with

Eval(StringA + StringB).Text = "Some new string"

Does VB.NET not have some equivalent ?

Herfried K. Wagner

unread,
Apr 7, 2003, 4:46:05 PM4/7/03
to
Hello Liz,

"Liz" <lizs...@swbell.net> schrieb:


> I was considering the kind of code you gave here
> but wondered if there were not a more direct approach;
> in JScript, you can do this sort of thing with
>
> Eval(StringA + StringB).Text = "Some new string"
>
> Does VB.NET not have some equivalent ?

IMHO there is no "one-command" equivalent. Eval statements are usually contained
in scripting languages.

Regards,
Herfried K. Wagner


Chris J. Breisch

unread,
Apr 7, 2003, 5:01:28 PM4/7/03
to

In ASP.NET one can do

CType(FindControl("lblFoo"), Label).Text = "Hello!"

FindControl returns Nothing if it can't find the control, so it might be
wiser to split the statement up a bit, though, or put it in a Try/Catch
block.

-chris


--
Chris J. Breisch, MCSD.NET, MCDBA

Kathleen

unread,
Apr 7, 2003, 5:19:23 PM4/7/03
to
Trying to check on a bunch of controls, and change each one's color if
they're checked. Want to do it with one routine rather than writing the
same code over and over again.

Prefer calling this routine from the page_load routine if IsPostBack, so I
don't have to put an OnServerValidate clause in each control.

-Kathleen

"Armin Zingler" <dontwa...@anywhere.de> wrote in message
news:#57VfCU$CHA....@TK2MSFTNGP11.phx.gbl...

Armin Zingler

unread,
Apr 7, 2003, 5:57:20 PM4/7/03
to
"Kathleen" <not_...@hotmail.com> schrieb

> Trying to check on a bunch of controls, and change each one's
> color if they're checked. Want to do it with one routine
> rather than writing the same code over and over again.
>
> Prefer calling this routine from the page_load routine if
> IsPostBack, so I don't have to put an OnServerValidate clause in
> each control.

Sorry, don't know ASP. In Winforms, I'd add the controls to an array or
iterate the controls collection (for each).

Armin

Chris J. Breisch

unread,
Apr 7, 2003, 6:35:42 PM4/7/03
to
On Mon, 07 Apr 2003 21:19:23 GMT, Kathleen <not_...@hotmail.com> wrote:

> Trying to check on a bunch of controls, and change each one's color if
> they're checked. Want to do it with one routine rather than writing the
> same code over and over again.
>
> Prefer calling this routine from the page_load routine if IsPostBack, so
> I
> don't have to put an OnServerValidate clause in each control.
>
> -Kathleen
>

As I said in a previous message, you can do CType(FindControl("chkBox"),
CheckBox).ForeColor = ...

but it might be easier if you did something like:

For Each ctl in Controls
If TypeOf(ctl) Is CheckBox Then
ctl.ForeColor = ...
End If
Next

If you have other checkboxes on the page that you don't want to change the
color of, you can put all the ones you do on a panel, and then just iterate
through the controls on that panel, rather than the page.

For Each ctl in pnl.Controls
...
Next

Forgive me if the syntax isn't 100% correct. I'm on a machine without
VS.NET on it, and I'm trying to do this from memory. It should get you
pretty close, though.

0 new messages