Good Day,

I would like to ask some Help regarding on my Textfield that will not be allowed

SPACES WITH NO CHARACTER, SPACES CHARACTER

only be allowed is Characted Spaces Characted.

Your Response is Highly Appreciated.

Thank You.

S**PACES WITH NO CHARACTER, SPACES CHARACTER, only be allowed is **

What do you mean? Can you explain it clearly so we can help you?

Good day Sir,

Like this
[ HELLO WORD] , [ HELLOW WORLD ]

only be allowed is

[HELLO WORLD]

If you only want to allow certain characters then you can filter them in the KeyDown event. The clearest way is

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    Select Case e.KeyValue
        Case Keys.A To Keys.Z
        Case Keys.Space
        Case Keys.Left, Keys.Right, Keys.Back
        Case Else : e.SuppressKeyPress = True
    End Select

End Sub

Just add extra Case statements for other characters you want to allow. I allow Keys.Left, Keys.Right and Keys.Back to allow the user to edit already entered characters. Setting e.SuppressKeyPress to True causes the typed character to be ignored.

Good Day,

What if i have a many Textbox?

if there easy to initialize this Case?

Thank you for your Response.

Eos

You can create a public function or procedure so you can call it in every textbox

Hi,

You can add as many Events as you want on the End. As long as the Subroutine can Handle them, the Event will happen.

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)Handles TextBox1.Keydown,TextBox2.Keydown,TextBox3.Keydown,TextBox4.Keydown ' as many as you have...

I didn't get your point Luc001 can you explain it thoroughly?

Hi GeekPlease,

You have the event for TextBox1_Keydown and this event wil happen only for KeyBox1.
Now, When you add more TextBoxes at the end of this event, like I showed you in previous post, then the event will also happen for the other TextBoxes when they have the focus.
Hope it helps,

Hi Luc001, so you mean that the when the event is triggered in a certain textbox then all the textboxes will do what the event instructed? Am I right?

Hi,
No, only the one who has the focus and you want to write numbers in that textbox.
You can show numbers in the textboxes but not write numbers in them.
Try this example;

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox2.Text = "555-666-777"
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown
        Select Case e.KeyValue
            Case Keys.A To Keys.Z
            Case Keys.Space
            Case Keys.Left, Keys.Right, Keys.Back
            Case Else : e.SuppressKeyPress = True
        End Select
    End Sub

After debugging this example you'll see the text: 555-666-777 in textbox2.
Delete the text in TextBox2 and try to rewrite the numbers into the textbox2.

Hope it helps,

For handling the events for multiple controls in one handler, you might find it easier to omit the handles clause and use the addhandler method.

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Each tb As Textbox In Me.Controls.OfType(Of TextBox)
            AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
        Next
    End Sub

Then filtering out other punctuation could be done like this:

    Private Sub TextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
        Dim c As Char = e.KeyChar
        If Char.IsPunctuation(c) Then
            If Not c = " "c Then
                e.Handled = True
            End If
        End If
    End Sub

Thank you guys now i how to do it.

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.