PaulN 09-25-2009, 01:42 PM Hi, I'm very new to VB and am trying to figure out how to assign a number to a letter so as I'm typing, it's adding it up.
a=6
b=12
c=18
and so on.
I tried to find an example to do this but I can't.
Can anyone please give me a start on how to do this?
Any help would be appreciated.
Thanks.
loquin 09-27-2009, 10:16 AM how to assign a number to a letter so as I'm typing, it's adding it up.I think you need to explain more completely just what you're trying to do.
Typing... where? Into a textbox?
Do you need to display the original letters as entered? Or, discard them?
Are upper case letters treated the same as lower case letters?
What about non-alpha characters? How do you handle them?
You apparently want to keep the sum of the assigned letters. does this need to be displayed somewhere?
What about the backspace or delete keystrokes? Does entering a backstroke mean that you need to subtract the prior letter's value from the sum as you delete the letter?
PaulN 09-27-2009, 03:25 PM Loquin, Thanks for your reply. What I want to do is: Type a 'word' in a text box, press a '+' button, and get the added result in a 'label'.
I found a simple calculator program tutorial on the internet and want to modify it to do what I want it to do. Right now it +,-,*, and / , two number that are typed into two different textboxes and displays the result in a 'label'. I want to use one TextBox, Label, and '+'.
All I have to figure out is how to, convert the letters that have assigned values, to numbers, to display the result. Thanks. PaulN
Edit: Lower case, do not count spaces
Edit:
So far I have this, using only the one TextBox:
Dim a As String
Dim aa As Integer = 6
a = CType(aa, String)
Label1.Text = (a)
Iceplug 09-27-2009, 07:36 PM What are you adding to the word in the textbox?
I see:
[TextBox1] + [TextBox2] = [Label]
And you've gotten rid of the other textbox; now I see:
[TextBox1] + _________= [Label]
What's in the blank?
PaulN 09-27-2009, 07:54 PM IcePlug, I'm using only TextBox1. I can't get it to add. When I type "aa" in the textbox, I get only '6', I should get '12'. How do I get it to add when I type 'aa'.
The TextBox2 will be taken out.Thanks. PaulN.
Iceplug 09-28-2009, 05:10 AM So you want to go through each character in the textbox and add their equivalent values together?
To go through each character in the textbox:
For LV = 0 To Textboxname.TextLength - 1
Next
The character can be found in Textboxname.Text.Chars(LV)
You'll then check if that character is:
= "a"c Then
Add the result to a summation variable:
Sum += 6 'the value for a or if you have a constant value already setup for a.
Once you're finished with the loop, display the result in Sum.
PaulN 09-30-2009, 04:27 PM Iceplug, I tried what you suggested and I couldn't get it to work. I don't think I put the code in the right place. Since I'm new to this, I really need an example to go by. Thanks. PaulN
Iceplug 09-30-2009, 08:06 PM Since you're new to this, I really need to see what you actually did in comparison to what I assumed you were going to do...
PaulN 10-05-2009, 01:53 PM Iceplug, I figured it out:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim a As String
Dim aa As Integer = 6
Dim Sum As Double
a = CType(aa, String)
Sum = Val(Label1.Text)
Label1.Text = Sum + a
Now all I have to do is figure out how to the same for each letter and get the sum. You suggested I do a Loop, do I start it after the 'Dim"s? Any Suggestions? Thanks.
PaulN
Iceplug 10-05-2009, 02:34 PM Yes, you need to Do a Loop.
From what I see, your code merely adds 6 to the value of label text
Label1.Text = Val(Label1.Text) + 6
...but it doesn't refer to the value in TextBox.Text at al, which leads me to believe that I may have misinterpreted what you wanted to do... (or maybe I didn't and the solution that I'm about to give you will only satisfy a short term goal...)
To detect which key the user pressed, use the TextBoxes keypress event.
(You should find it in the boxes at the top of the code window)
You should see code that looks something like:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
The key pressed is in the e (the KeyPressEventArgs), specifically found at e.Char
You can do comparisons with the KeyChar to see if it is a particular letter, i.e.
If e.Char = "a"c Then
'The user has typed an a - add 6 to the label.
ElseIf e.Char = "b"c Then
'The user has typed a b - add 12 to the label.
End If
'Add other letters if needed.
I will say again that this will not work properly if/when you want to add support for backspace, so if you wanted that (I suggest trying this to get a hang of the event, if you haven't already)... then you should do a Loop. The loop can essentially go anywhere in the loop, since I think you are going to replace the code so that it doesn't just add 6.
...or maybe if you at least put the For Loop that I posted above into the sub, I will see which method you want.
PaulN 10-05-2009, 03:18 PM Iceplug, Thanks for your reply. I will play around with the info you gave me.
Thanks again!
PaulN
Edit: When I type a in the textbox, it reads 6 in the Label1. Then when I type a again, It reads 12. Thats how I want it to read as I type in different letters.
|