Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a small problem but don't know how to solve it.

I have 3 textboxes, one combobox, one maskedtextbox and a button.

I have successfully implemented a code, where when all the textboxes, combobox and maskedtextbox are blank, the button is disabled. Good!

However, when a person wil fill one of these fields, he can erase a the word/character by the backspace and delete key or simply erase it with his mouse, so I want that if the user does this, the button disabled again. I mean when he will insert a character/word and erase it with his mouse or keyboard by using the backspace and delete key, the button must be disabled again.

Help, thank you.
Posted

1 solution

What you need to do is implement a function which gets called in each control's Leave event.
Something like this

VB
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, MaskedTextBox1.Leave, ComboBox1.Leave
        DisableButton()
    End Sub
    Private Sub DisableButton()
        'Disable button here if all controls empty
    End Sub


Hope this helps
 
Share this answer
 
Comments
555336 28-Apr-11 7:08am    
Nope does not work. I forgot to say that I have a pretext also on my textbox only.
Wayne Gaylard 28-Apr-11 7:15am    
Can you let me know what didn't work. The Leave event will be fired whenever the user leaves each control. Once the user has left the control you need to use this event to check and see if what the use entered hass valid or has erased what was in the control.
NuttingCDEF 28-Apr-11 7:21am    
Probably need to handle the TextChanged events (fired whenever text changes, not just when focus moves away from the control). This works for pasting text / deleting text with the mouse / context menus as well as keystrokes.

I frequently write a single method UpdateControls that gets called any time any text or anything else changes in a form that updates button states etc. depending on current control values (so check for your pretext as well as blanks). Just watch that you make it lightweight as otherwise the user interface might get quite sluggish.
Wayne Gaylard 28-Apr-11 7:29am    
That means running a check after each key entry/deletion, which is OK I guess, if the user changes something and doesn't leave the control.
555336 28-Apr-11 7:27am    
B

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900