文本框计算中字符的总和 [英] charecters sum in a text box calculation
问题描述
我想使用文本框仅输入字母键和空格键.也就是仅输入人名.我想为每个字母拼写字母分配一个这样的数字
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并且对于空间没有值
不管是小写还是大写.我是三个文本框.一个是输入名称,另一个文本框是获取字符的总和,第三个文本框是求和的总和.在一个文本框中输入名称,字符的总和应显示在另一个文本框中.
例如
如果您在一个文本框中输入名称TOM JOHN,则应在另一个文本框中显示总和35,在另一个文本框中显示总和35意味着8应显示8,这给了我一个如何设计表格和编码的想法.我正在使用vb .net和sql server
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
推荐答案
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
要验证字母的文本框和BackSpace使用以下功能
To Validate TextBox for Alphabets and BackSpace Use the following function
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
在TextBox KeyPress事件上调用上述函数.
获取Char使用功能的总和
Call above function on TextBox KeyPress Event.
To Get Sum of Char Use Function
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
要获得上述函数的sum调用,请在TextBox KeyDown事件上调用上述函数
TO get sum call above function call above function on TextBox KeyDown Event
TextBox2.Text=Val(TextBox2.Text)+GetCharValue(DirectCast(keyVal, Keys))
要获取名称的长度,请使用长度功能
To get the Length of Name use the Length Function
TextBox3.Text=TextBox1.Text.Length
这篇关于文本框计算中字符的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!