Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling a Button with a Click Event

Status
Not open for further replies.

klornpallier

Technical User
Aug 28, 2002
98
GB
I need to disable a menu button once it is clicked. Within the click event of the button I placed:

ButtonName.Enabled = False

This does not seem to work. Any help would be appreciated.

Thanks
 
Your syntax is correct and works for me. Is this "menu button" a System.Windows.Forms.Button or a System.Windows.Forms.ToolStripMenuItem? Also, is ButtonName the name of the control, the prefix of your click event (ButtonName_Click), or both?

Try replacing your line of code with
Code:
CType(sender, Control).Enabled = False
and see if that works.
 
Thank you for your response. Its a ToolStripItem so I changed this to the below but is doesnt still work. Oh and yes ButtonName is the naem of the control:

CType(sender, ToolStripItem).Enabled = False
 
Private sub ButtonName_click(... ,...) Handles ButtonName.Click

is the handler there? Some times if you copy-paste control or move them between forms the handler is deleted.
 
Have you placed a breakpoint to verify that your program is actually hitting that line of code? Is this a Windows Forms program?
 
Here's the full Sub. The problem is on the last 2 lines of code. Thanks again

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

'Set Defaults
KPPERSON.VARIABLE_INPUT.Columns("EMP_NO").DefaultValue = EmpNoHolderLabel.Text
KPPERSON.VARIABLE_INPUT.Columns("PERIOD_NO").DefaultValue = PeriodHolderLabel.Text
KPPERSON.VARIABLE_INPUT.Columns("EARN_DATE").DefaultValue = EarnDateHolderLabel.Text
'KPPERSON.VARIABLE_INPUT.Columns("PAYMENT_TYPE").DefaultValue = "Adjustment"

'Start on the Element Row
Dim row_no As Integer = Me.VARIABLE_INPUTDataGridView1.SelectedCells(0).RowIndex
VARIABLE_INPUTDataGridView1.CurrentCell = VARIABLE_INPUTDataGridView1(4, row_no)

'Disable the Add button until saved
CType(sender, ToolStripItem).Enabled = False

End Sub
 
The ToolStripItem also works for me. That is because the ToolStripItem is the base class (ToolStripButton derives) and the .Enable property exists there.
 
Thanks for your efforts but annoyingly neither work for me! :( Any further ideas?
 
Ironically although 'MenuToolStripButton.Enabled = False' doesnt work 'MenuToolStripButton.Available = False' does!

Now I'm really confused. I cant be bug visual studio bug surely??
 
Just to clarify, when you say
ButtonName.Enabled = False

This does not seem to work. Any help would be appreciated.
What do you mean, you can still click the button?

The .Available property will hide the button/menuitem from the users view (effectively disabling it), whereas .Enabled will leave the button/menuitem on the menustrip/toolstrip but grey it out so it can't be clicked.

What do you want to happen when you disable the control?

The only reason I ask is that .Enabled seems to work perfectly well for myself and everyone else in this thread so I'm pretty much ruling out a problem with VS.

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I want the ButtonName to be disabled and greyed out when I click another MenuToolStripButton.
 
Could you show us the code you've got using .Available that works?

Just to clarify as the terminology is getting a bit 'all over the place', the button you want to disable is on a ToolStrip yes?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top