Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to use a text boxes to enter only alphabets keys and space key only.that is to enter persons names only.and i want to assign each alphaphets a number say like this

A , B ,C = 1
D ,E, F = 2
G ,H ,I = 3
J ,K ,L = 4
M , N =5
O , P Q,R ,S ,T, U =6
V ,W, X, Y,Z = 7 and for space no values

it doesnt matter whether it is lower case or upper case.i am three text boxes .one is to enter the name ,other text box is to get the sum of charecters and the third text box is to get sum of the sum.if u enter the name in one text box,the sum to the charecters should display in another textbox.
for example
if u enter a name TOM JOHN in one text box it should display the sum in another text box 35 and in another text box sum of 35 means 8 should display give me an idea how to design table and coding for this.i am using vb.net and sql server
Posted
Comments
Orcun Iyigun 21-Nov-11 18:41pm    
This sounds more like a homework to me? what have you tried so far?
LanFanNinja 21-Nov-11 18:53pm    
Me too.
LanFanNinja 21-Nov-11 20:02pm    
Recheck my solution for your brief explanation. ;)
LanFanNinja 21-Nov-11 21:32pm    
Solution updated to handle backspace key.

VB
Partial Public Class Form1
    Inherits Form
    Private sum As Integer = 0
    Private summation As Integer = 0

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub nameTextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles nameTextBox.KeyDown
        Dim value As Integer = GetValueOfKey(e.KeyCode)

        If value = -1 Then 'Space
            'do nothing
        ElseIf value = -2 Then 'Backspace
            If nameTextBox.Text.Length > 0 Then
                Dim keyVal As Integer = AscW(nameTextBox.Text(nameTextBox.Text.Length - 1).ToString().ToUpper())
                If keyVal <> 32 Then
                    AddValue(-GetValueOfKey(DirectCast(keyVal, Keys)))
                End If
            End If
        ElseIf value = -3 Then 'Invalid
            e.SuppressKeyPress = True
        Else
            AddValue(value)
        End If
    End Sub

    Private Sub AddValue(value As Integer)
        sum += value
        sumTextBox.Text = sum.ToString()
        Dim temp As Integer = 0
        For i As Integer = 0 To sumTextBox.Text.Length - 1
            temp += Int32.Parse(sumTextBox.Text(i).ToString())
        Next
        summation = temp
        summationTextBox.Text = summation.ToString()
    End Sub

    Private Function GetValueOfKey(key As Keys) As Integer
        Select Case key
            Case Keys.A, Keys.B, Keys.C
                Return 1
            Case Keys.D, Keys.E, Keys.F
                Return 2
            Case Keys.G, Keys.H, Keys.I
                Return 3
            Case Keys.J, Keys.K, Keys.L
                Return 4
            Case Keys.M, Keys.N
                Return 5
            Case Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T, _
             Keys.U
                Return 6
            Case Keys.V, Keys.W, Keys.X, Keys.Y, Keys.Z
                Return 7
            Case Keys.Space
                Return -1
            Case Keys.Back
                Return -2
            Case Else
                Return -3
        End Select
    End Function
End Class
 
Share this answer
 
v4
Comments
jayasakthi shanmuggarajah 21-Nov-11 19:27pm    
i didnt understand ur explanation.also this is for my project work.pls give me a brief explanation
LanFanNinja 21-Nov-11 19:53pm    
Recheck my solution.
LanFanNinja 21-Nov-11 21:32pm    
I updated the solution just now to handle backspace key.
To Validate TextBox for Alphabets and BackSpace Use the following function
VB
Public Sub ValidText(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Select Case Asc(e.KeyChar)'Validating Text using Ascii
            Case 48 To 57, 58 To 64         'If Ascii is 48...57 or 58..64 then Provide Message
                e.Handled = True
                MessageBox.Show("You can not enter 'Number' here!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case Else             'If Ascii is other than 48...57 then allow
                e.Handled = False
        End Select
    End Sub

Call above function on TextBox KeyPress Event.
To Get Sum of Char Use Function
VB
Private Function GetCharValue(key As Keys) As Integer
        Select Case key  'Return Integer Values for Each Alphabet
            Case Keys.A, Keys.B, Keys.C
                Return 1
            Case Keys.D, Keys.E, Keys.F
                Return 2
            Case Keys.G, Keys.H, Keys.I
                Return 3
            Case Keys.J, Keys.K, Keys.L
                Return 4
            Case Keys.M, Keys.N
                Return 5
            Case Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T, Keys.U
                Return 6
            Case Keys.V, Keys.W, Keys.X, Keys.Y, Keys.Z
                Return 7
            End Select
    End Function

TO get sum call above function call above function on TextBox KeyDown Event
VB
TextBox2.Text=Val(TextBox2.Text)+GetCharValue(DirectCast(keyVal, Keys))

To get the Length of Name use the Length Function
VB
TextBox3.Text=TextBox1.Text.Length
 
Share this answer
 
v2

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