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

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

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!
 
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.
 
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?

Me.Controls[TheName].Enabled = False

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

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.
 
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
 
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!
 
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


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!
 
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

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!
 
Is this a school assignment?
I see no other time when you're only "supposed to know" a button name.

Seth Rowe
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
 
Back
Top