I am desiging a Form in which I am asking user to enter:
Name in Textbox1
ID Card Number in Text2

here i want to code for textbox1 that it accepts only alphabates but don't know how to control. Can anyone help me?

Recommended Answers

All 13 Replies

The following code will allow/prevent the entry of specific keys. Add a Case statement for each key or range of keys you want to allow. Note that you have to add a Case to allow BackSpace but not Left/Right Arrow or Delete.

Public Class Form1

    Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        'Remove the following line for production code
        Me.Text = "ASC " & Asc(e.KeyChar) & "    HEX " & Asc(e.KeyChar).ToString("X2")

        Select Case e.KeyChar
            Case "a" To "z"     'lower case letter
            Case "A" To "Z"     'upper case letter
            Case " "            'blank
            Case Chr(8)         'backspace
            Case Else:          e.Handled = True
        End Select

    End Sub

End Class

The following codes will prevent the keys other than the alphabates. But you can allow the deletion and nevigation keys.

Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If TypeOf Me.ActiveControl Is TextBox Then
            If Not ((e.KeyCode = Keys.A) Or (e.KeyCode = Keys.B) Or (e.KeyCode = Keys.C) Or (e.KeyCode = Keys.D) Or (e.KeyCode = Keys.E) Or (e.KeyCode = Keys.F) Or (e.KeyCode = Keys.G) Or (e.KeyCode = Keys.H) Or _
                    (e.KeyCode = Keys.I) Or (e.KeyCode = Keys.J) Or (e.KeyCode = Keys.K) Or (e.KeyCode = Keys.L) Or (e.KeyCode = Keys.M) Or (e.KeyCode = Keys.N) Or (e.KeyCode = Keys.O) Or (e.KeyCode = Keys.P) Or _
                    (e.KeyCode = Keys.Q) Or (e.KeyCode = Keys.R) Or (e.KeyCode = Keys.S) Or (e.KeyCode = Keys.T) Or (e.KeyCode = Keys.U) Or (e.KeyCode = Keys.V) Or (e.KeyCode = Keys.W) Or (e.KeyCode = Keys.X) Or _
                    (e.KeyCode = Keys.Y) Or (e.KeyCode = Keys.Z) Or (e.KeyCode = Keys.Back) Or (e.KeyCode = Keys.Delete) Or (e.KeyCode = Keys.Home) Or (e.KeyCode = Keys.End) Or _
                    (e.KeyCode = Keys.Left) Or (e.KeyCode = Keys.Right)) Then
                e.SuppressKeyPress = True
            End If

        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = ""

    End Sub
End Class

You posted essentially the same code in another thread. I downvoted you there so I won't do it here. Nobody should take the above as a good example of how to code.

  1. The above code affects all textboxes at the top level of the form, not just a particular textbox.
  2. It is difficult to read
  3. It is inefficient
  4. It tests many individual cases where ranges would be better

You can do this for all TextBoxes in a Form from FormLavel without disturbing the TextBoxes, which have the same input restriction.If you do this for a specified textbox, you can alter the line No. 4 i.e. If TypeOf Me.ActiveControl Is TextBox Then by If Me.ActiveControl Is <Specified TextBox Name> Then.

Still a bad idea. You are much better off doing the filtering within event handlers for the specific textboxes. You are altering the behaviour of a control so you want to keep that code logically bound to the control. If it is necessary to alter the behaviour of more than one textbox, all you have to do is add the controls via extra Handles... clauses on the event handler as in

Private Sub Tbx_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) _
    Handles TextBox1.KeyPress, TextBox2.KeyPress

Ok, I think what you want is here.
EXE File : example <link to possibly dangerous executable removed>

To Download the Full Source of this get it from here <warning - read through the code very carefully before running>

The above linked tutorial may be listed as such but it fails horribly for several reasons

  1. the download raises an error (I tried to DL it 3 times) even though it seems to be complete
  2. there is no explanation associated with the code
  3. there are no comments in the code
  4. two of the buttons start other processes liniked to two suspicious websites
  5. it appears to contain code completely unrelated to its stated purpose

Without further investigation I would label this a very suspicious, possibly dangerous app to run. As such I am removing the link to the executable from the previous post. I am leaving the link to the actual site where the source can be downloaded.

@revered Jim
It contain button to my website.
YES I AGREE.
the code where i am gooing to post. its one. (Website 2) which will lead to Educational Site so they can learn I was Thinking to create it so I dont buy domain too.

THE MAIN THING IS: CODING.
CODING ARE ALREADY EXPLANED. CLICK ABOUT IMG, will slide andd you will get what was working there.

I also given the download link only of source in it.
I am very disappointed with this, That As i get dislikes for my post.
I dont find any excitment as before, now I think no value of mine to help people.
Sorry, from now I will not post anything again....
Good Byeee....

Consider the other reasons. You should never distribute an executable on DaniWeb. With over a million members it would be too easy for someone to post an executable with malicious content. Whether or not the code is legitimate it still remains that it comes with no instructions, no documentation and no comments. If the code is yours then it should have been posted in the forum so that the reference and code are together rather than depending on the external link remaining valid. I can see no good reason why tutorial code for programming textboxes should start external processes, or start timers for that matter.

At the very least, it completely fails as a tutorial because it does not attempt to teach. Lacking comments it doesn't even make for a good code snippet.

Ok, I got there is my fault.
just delete all of my post that I post in DANIWEB

DaniWeb is a place to exchange ideas and to gain knowledge of techniques that you don't have. If you post some code and there is a better (and often easier) way to accomplish the task then someone will likely point it out. When I was first learning to program I got a lot of criticism on the code that I wrote. That's how I learned to become a better programmer. I've been doing it for a long time and I still get comments from others on how to make my code better. I hope you stick around and remain a part of the community.

@Deep Modi - I just read your signature. Keep in mind that no matter how good a programmer you are someone will always know more than you. That applies to you, me, Dani and even the guru programmers at Microsoft and Google.

Sir, I know you are professional and having more knowledge than me,
np, have you read the my information. (you will see there I am not from computer engineering) so Obviously I will not have enough knowledge compare to you

But the question is that as I insstalled the theme in it, My programe is suspicious.
and here I already add the ONLY CODING PART too. where I tell where the module should be pressed.
If the user is going to read those coding he will be able to understand all the things, If he cant then it means that he want to use this as for some apliication (without knowledge to vb.net) as I watch many videos in youtube and many other website regarding this.
The coding is of basic level only. The thing he want to use is the way to use coding.

The same thing i did...

I was not disappointed as you did this,
If you pm me the same thing then I caan edit and save it.
But you did it in public, and this all also affect the Reputation as well as many other things.

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.