Hi

I have made a program on Visual Basic 2008. I have put textbox1 and button1 ONLY.
I have disabled button1. I want button1 only to be enabled when we type some numbers in textbox1. I also want that we can ONLY type numbers in textbox1, not letters or other symbols.

Any help will be highly appreciated
Thanks,
Hassan

Recommended Answers

All 5 Replies

Rather than a textbox, use a masked textbox. Set the mask to a number of the appropriate length. For the event, use mtxtTextbox_TextChanged and enable the button with

Me.btnButton.Enabled = True

Will this do what you need?
(I'm sorry if the code isn't displayed properly. This is my first post.)

================================================================

Hi

I have made a program on Visual Basic 2008. I have put textbox1 and button1 ONLY.
I have disabled button1. I want button1 only to be enabled when we type some numbers in textbox1. I also want that we can ONLY type numbers in textbox1, not letters or other symbols.

Any help will be highly appreciated
Thanks,
Hassan

Hi GomerTee,

I can do the step that you told me. This is not the thing i want.

I have disabled button1. I want when the user types some NUMBERS in textbox1, button1 should then be enabled. I also want that the user can only type numbers in textbox1, not letters, spaces or other symbols.

But even then, thanks for your help!
Regards,
Hassan

Hi GomerTee,

I can do the step that you told me. This is not the thing i want.

I have disabled button1. I want when the user types some NUMBERS in textbox1, button1 should then be enabled. I also want that the user can only type numbers in textbox1, not letters, spaces or other symbols.

But even then, thanks for your help!
Regards,
Hassan

Are you using a regular TextBox or a MASKED Textbox? They are different controls and the Masked TextBox is the one you need. It can be set (using a "mask") so it accepts only numbers.

Here's what I did: I created a blank form and added a button, changing its enabled property to FALSE and its visible property to FALSE. I added a masked textbox to the form and changed the mask to 00000. (that's five zeroes) I didn't change the names or anything else in properties, though you probably would in a real program. (Obviously, if you only wanted the button grayed out, but still visible, you'd leave the visible property alone.)

With the Masked TextBox selected, I clicked the lightning bolt ("events") at the top of the Properties box to the right of the buttons for setting the properties alphabetically or in categories. I double-clicked on TextChanged and the code editing window popped up a sub, so I could add two lines. Here it is:

Private Sub MaskedTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.TextChanged
        Me.Button1.Enabled = True
        Me.Button1.Visible = True
    End Sub

If I type a letter or punctuation mark in the Masked TextBox, nothing happens. If I type a number 0-9, the button becomes visible and enabled.

If you're going to need this Masked TextBox to accept text at another point in the program, I guess you could change the mask in code or just set it to nothing; I've never needed that. Or you could add a regular TextBox and put it on top of the other, with only one enabled and visible at a time.

Good luck!

Hi GomerTee,

I can do the step that you told me. This is not the thing i want.

I have disabled button1. I want when the user types some NUMBERS in textbox1, button1 should then be enabled. I also want that the user can only type numbers in textbox1, not letters, spaces or other symbols.

But even then, thanks for your help!
Regards,
Hassan

See if this helps.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Enabled = False
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        '// code from this thread here.
        '//--- http://www.daniweb.com/forums/post1345601.html#post1345601
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not TextBox1.Text = "" Then '// check it TextBox1 has any text.
            Button1.Enabled = True '// if so, Enable Button1.
        Else
            Button1.Enabled = False '// otherwise, Disable Button1.
        End If
    End Sub
End Class

Link to thread for KeyPress event.
http://www.daniweb.com/forums/post1345601.html#post1345601

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.