10 Replies - 4460 Views - Last Post: 08 December 2011 - 05:24 PM Rate Topic: -----

#1 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 20-March 11

TEXTBOX check min.char and special char

Posted 07 December 2011 - 12:26 PM

Hello.

I have one textbox on my form. can someone help me rewrite the below code.
i want to set minimum character to 3, i have already set the maximum character to 10 in the textbox property. i also want to block space and special character. i need to coordinate this codes to achieve my aim.

Thanks in advance


Private Sub TextBox1_KeyPress(ByVal KeyAscii As Integer)
        Dim strSpecialChars As String
        strSpecialChars = "`~!@#$%^&*()_-+=\|]}[{:;,<.>/?'"""   '~~~ All special characters

        If InStr(1, strSpecialChars, Chr(KeyAscii)) > 0 Then
            KeyAscii = 0
            MsgBox("Invalid")
        End If
    End Sub
    Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown


        If e.KeyCode = Keys.Enter And TextBox1.Text <> "" Then
            e.SuppressKeyPress = True
            'stop the music.
            My.Computer.Audio.Stop()
            Main1.Show()
            Me.Hide()


        ElseIf e.KeyCode = Keys.Enter And TextBox1.Text = "" Then
            e.SuppressKeyPress = False
            Label2.Text = "Please Enter Your Name, To Continue Press Enter Key."

        ElseIf e.KeyCode = Keys.Enter And Len(TextBox1.Text) <= 3 Then
            e.SuppressKeyPress = False
            Label1.Text = ("Enter Name Minimum 3, Maximum 10 Letters")

        End If

    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: TEXTBOX check min.char and special char

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: TEXTBOX check min.char and special char

Posted 07 December 2011 - 12:50 PM

What do you mean by *help* you to re-write it?
Do you mean re-write it for you and tell you what it should be?

If you need the textbox to have a minimum of 3 characters then you need to pre-populate it, don't you? Otherwise it starts out empty and that's less than 3 characters.

YOu want to block certain characters. Ok. Do that. You already seem to know how to do that as you are checking if you have a Keys.Enter. So what's the problem?

Maybe I can help by giving you some logic to code:

If (specialCharacters.Contains(keyIjustTyped)) then ignoreIt.

If that doesn't get you going, then we need you to be more specific about the help you need and what exactly you are having trouble with.
Was This Post Helpful? 1
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: TEXTBOX check min.char and special char

Posted 07 December 2011 - 03:37 PM

Hi - I'll give you a better idea of what you need to do:
If specialcharacters.Contains(e.KeyChar) Then
'execute code
End If


Was This Post Helpful? 1
  • +
  • -

#4 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 20-March 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 02:46 AM

View PosttlhIn`toq, on 07 December 2011 - 12:50 PM, said:

What do you mean by *help* you to re-write it?
Do you mean re-write it for you and tell you what it should be?

If you need the textbox to have a minimum of 3 characters then you need to pre-populate it, don't you? Otherwise it starts out empty and that's less than 3 characters.

YOu want to block certain characters. Ok. Do that. You already seem to know how to do that as you are checking if you have a Keys.Enter. So what's the problem?

Maybe I can help by giving you some logic to code:

If (specialCharacters.Contains(keyIjustTyped)) then ignoreIt.

If that doesn't get you going, then we need you to be more specific about the help you need and what exactly you are having trouble with.


Thanks for your reply. i mean the code is not working as it should. when a special character is enter on the textbox it still go through. and please how do i write code to check if the char is not less than 3.
Was This Post Helpful? 0
  • +
  • -

#5 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 20-March 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 03:04 AM

Something like ..


If e.KeyCode = Keys.Enter And TextBox1.Text value is not less than 3 char and doesnt contain specialcharacters "" Then

e.SuppressKeyPress = True
'stop the music
My.Computer.Audio.Stop()
Main1.Show()
Me.Hide()
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 09:01 AM

View Posttimosilver, on 08 December 2011 - 03:46 AM, said:

please how do i write code to check if the char is not less than 3.


I'm going to stop you here. You're not ready to design and write a program from scratch yet. Checking the .Length property of the TextBox is something you would learn in the first week of doing any course, on-line tutorial or self teaching book. The fact that you don't know this indicates you haven't really taken any time to learn the language but instead just jumped in trying to write a program. That never works. It always results in exactly what we are seeing here: The rookie asking to be hand-held through every little step that they should have taken upon themeless to learn before asking the question. We're happy to help you with genuine problems, be the volunteers here don't have the resources to be the private tutors for the half-million members of the site.


Quote

I have a little programming experience but I need to write ...


For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program. Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience." I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

Was This Post Helpful? 1
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 03:54 PM

You would use this:
If specialcharacters.Contains(e.KeyChar) And Not TextBox1.Text.Contains(specialcharacters) And TextBox1.Text.Length <3 Then
'do stuff
End If


Was This Post Helpful? 1
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 04:19 PM

Did it work for you? I am not sure as I am nowhere near a copy of VS and it came off the top of my head.
Was This Post Helpful? 0
  • +
  • -

#9 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 20-March 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 04:54 PM

Thanks for your help jimmyBo, Am trying to get it right.
Your code is helpful but its e.Keycode nor e.KeyChar.
Take a look at the code below. thats what am trying to do.

 Dim specialCharacters As String
    
    Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        specialCharacters = "`~!@#$%^&*()_-+=\|]}[{:;,<.>/?'"""

        If e.KeyCode = Keys.Enter And TextBox1.Text = "" Then
            e.SuppressKeyPress = False
            Label2.Text = "Please Enter Your Name, To Continue Press Enter Key."

        ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Contains(specialCharacters) Then
            e.SuppressKeyPress = False
            Label1.Text = "No special charater"

        ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Length < 3 Then
            e.SuppressKeyPress = False
            Label1.Text = "less than 3 words"

        Else
            e.SuppressKeyPress = True
            Main1.Show()
        End If

Was This Post Helpful? 0
  • +
  • -

#10 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 04:59 PM

Looks good - glad I could help you! Just out of curiosity - what is this for?
Maybe outside the KeyDown event declare the specialcharacters string with a value, that way it doesn't keep setting the value:

Dim specialCharacters As String
   specialCharacters = "`~!@#$%^&*()_-+=\|]}[{:;,<.>/?'"""
   Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

       If e.KeyCode = Keys.Enter And TextBox1.Text = "" Then
           e.SuppressKeyPress = False
           Label2.Text = "Please Enter Your Name, To Continue Press Enter Key."

       ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Contains(specialCharacters) Then
           e.SuppressKeyPress = False
           Label1.Text = "No special charater"

       ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Length < 3 Then
           e.SuppressKeyPress = False
           Label1.Text = "less than 3 words"

       Else
           e.SuppressKeyPress = True
           Main1.Show()
       End If


Also, if you are showing a form and this is a login form, close the login form with this code:
Dim specialCharacters As String
   specialCharacters = "`~!@#$%^&*()_-+=\|]}[{:;,<.>/?'"""
   Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

       If e.KeyCode = Keys.Enter And TextBox1.Text = "" Then
           e.SuppressKeyPress = False
           Label2.Text = "Please Enter Your Name, To Continue Press Enter Key."

       ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Contains(specialCharacters) Then
           e.SuppressKeyPress = False
           Label1.Text = "No special charater"

       ElseIf e.KeyCode = Keys.Enter And TextBox1.Text.Length < 3 Then
           e.SuppressKeyPress = False
           Label1.Text = "less than 3 words"

       Else
           e.SuppressKeyPress = True
           Main1.Show()
Me.Close() 'so the login window is no longer showing
       End If


HTH! :)
Was This Post Helpful? 1
  • +
  • -

#11 timosilver  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 20-March 11

Re: TEXTBOX check min.char and special char

Posted 08 December 2011 - 05:24 PM

Thanks alot. jimmyBo. got it now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1