469,344 Members | 6,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

Using Variables to store a Button's Name and Disable it Using the Variable Value

Hello, I have a simple question,

I have a vb.net form with several buttons.

If I store the name of a button in a variable..

Dim TheName as string
TheName = Me.btnMyLittleButton.Name.ToString

How can I disable this button using the variable value?

I can do this:

Me.btnMyLittleButton.Enabled = False

However, how can I get the name of the button (stored in TheName
variable ) and use it to disable it?

Is there a way to accomplish this without going through all the
controls in the form?

Don't want something like this:
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = TheName Then
ctr.Enabled = False
End If
Next

or writing the control names into a hashtable

What I want is something simple as the original example
Me.btnMyLittleButton.Enabled = False ---TheName.enabled = False

Does anyone has something better?

Thanks!

Apr 16 '07 #1
7 5300
On Apr 16, 3:48 pm, "John Smith" <I...@NETZERO.NETwrote:
Hello, I have a simple question,

I have a vb.net form with several buttons.

If I store the name of a button in a variable..

Dim TheName as string
TheName = Me.btnMyLittleButton.Name.ToString

How can I disable this button using the variable value?

I can do this:

Me.btnMyLittleButton.Enabled = False

However, how can I get the name of the button (stored in TheName
variable ) and use it to disable it?

Is there a way to accomplish this without going through all the
controls in the form?

Don't want something like this:
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = TheName Then
ctr.Enabled = False
End If
Next

or writing the control names into a hashtable

What I want is something simple as the original example
Me.btnMyLittleButton.Enabled = False ---TheName.enabled = False

Does anyone has something better?

Thanks!

Me.Controls[TheName].Enabled = False

You can access a control in the control collection by it's name in
2005.

--
Tom Shelton

Apr 17 '07 #2
On Apr 16, 9:35 pm, Tom Shelton <tom_shel...@comcast.netwrote:
On Apr 16, 3:48 pm, "John Smith" <I...@NETZERO.NETwrote:


Hello, I have a simple question,
I have a vb.net form with several buttons.
If I store the name of a button in a variable..
Dim TheName as string
TheName = Me.btnMyLittleButton.Name.ToString
How can I disable this button using the variable value?
I can do this:
Me.btnMyLittleButton.Enabled = False
However, how can I get the name of the button (stored in TheName
variable ) and use it to disable it?
Is there a way to accomplish this without going through all the
controls in the form?
Don't want something like this:
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = TheName Then
ctr.Enabled = False
End If
Next
or writing the control names into a hashtable
What I want is something simple as the original example
Me.btnMyLittleButton.Enabled = False ---TheName.enabled = False
Does anyone has something better?
Thanks!

Me.Controls[TheName].Enabled = False

You can access a control in the control collection by it's name in
2005.

--
Tom Shelton- Hide quoted text -

- Show quoted text -
Hi Tom, thanks for replying, but I am not using vb.net 2005. I have
VB.NET 2003.

If I use that code, I get the 'Enabled' is not a member of 'string'
error.

Apr 17 '07 #3
On Apr 16, 5:48 pm, "John Smith" <I...@NETZERO.NETwrote:
Hello, I have a simple question,

I have a vb.net form with several buttons.

If I store the name of a button in a variable..

Dim TheName as string
TheName = Me.btnMyLittleButton.Name.ToString

How can I disable this button using the variable value?

I can do this:

Me.btnMyLittleButton.Enabled = False

However, how can I get the name of the button (stored in TheName
variable ) and use it to disable it?

Is there a way to accomplish this without going through all the
controls in the form?

Don't want something like this:
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = TheName Then
ctr.Enabled = False
End If
Next

or writing the control names into a hashtable

What I want is something simple as the original example
Me.btnMyLittleButton.Enabled = False ---TheName.enabled = False

Does anyone has something better?

Thanks!
Do you have to store the button name into a variable? It would be a
lot easier to just store the button into the variable:

Dim theButton as Button = Me.btnMyLittleButton

theButton.Enabled = False

Thanks,

Seth Rowe

Apr 17 '07 #4
Hi Rowe, thanks for replying.

Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.

I've tried to set the variable as a string, then as an object, but so
far I can't get it right.
I really hope this could be as easy as what Tom suggested:

Me.Controls[TheName].Enabled = False

The problem is that whenever I use the me. part, I get an error saying
that the variable is not a member of my form.

Is there a way to cast the variable as the button name using something
like DirectCast?

Thanks for all your help!

Apr 17 '07 #5
Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.
Is this a school assignment?

I see no other time when you're only "supposed to know" a button name.

Thanks,

Seth Rowe
On Apr 17, 12:44 pm, John Smith <I...@NETZERO.NETwrote:
Hi Rowe, thanks for replying.

Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.

I've tried to set the variable as a string, then as an object, but so
far I can't get it right.

I really hope this could be as easy as what Tom suggested:

Me.Controls[TheName].Enabled = False

The problem is that whenever I use the me. part, I get an error saying
that the variable is not a member of my form.

Is there a way to cast the variable as the button name using something
like DirectCast?

Thanks for all your help!

Apr 17 '07 #6
On Apr 17, 12:52 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.

Is this a school assignment?

I see no other time when you're only "supposed to know" a button name.

Thanks,

Seth Rowe

On Apr 17, 12:44 pm, John Smith <I...@NETZERO.NETwrote:
Hi Rowe, thanks for replying.
Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.
I've tried to set the variable as a string, then as an object, but so
far I can't get it right.
I really hope this could be as easy as what Tom suggested:
Me.Controls[TheName].Enabled = False
The problem is that whenever I use the me. part, I get an error saying
that the variable is not a member of my form.
Is there a way to cast the variable as the button name using something
like DirectCast?
Thanks for all your help!- Hide quoted text -

- Show quoted text -
:-)

Yes, it is and it's driving me crazy!

Apr 17 '07 #7
On Apr 17, 2:10 pm, John Smith <I...@NETZERO.NETwrote:
On Apr 17, 12:52 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.
Is this a school assignment?
I see no other time when you're only "supposed to know" a button name.
Thanks,
Seth Rowe
On Apr 17, 12:44 pm, John Smith <I...@NETZERO.NETwrote:
Hi Rowe, thanks for replying.
Well.. I cannot store the button into the variable. I'm supposed to
only know the button name.
I've tried to set the variable as a string, then as an object, but so
far I can't get it right.
I really hope this could be as easy as what Tom suggested:
Me.Controls[TheName].Enabled = False
The problem is that whenever I use the me. part, I get an error saying
that the variable is not a member of my form.
Is there a way to cast the variable as the button name using something
like DirectCast?
Thanks for all your help!- Hide quoted text -
- Show quoted text -

:-)

Yes, it is and it's driving me crazy!
Why can't you loop through the control collection or use a hash table?

In the future you should ask your professor/teacher or a fellow
student for help - this newsgroup is not here to do your homework!

Thanks,

Seth Rowe

Apr 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Gary | last post: by
1 post views Thread by ratnakarp | last post: by
3 posts views Thread by Brett Wesoloski | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.