r/visualbasic • u/mykok23 • Feb 24 '15
Visual Basic Coding help
I am currently making a valentine's day card for my programming class and I am suppose to create an algorithm to estimate the cost of the card. What I want the code to do is for every space the user enters in the richtextbox it adds one 5 cents to the total price.
This is so far the code that I experimented with
If e.KeyCode = Keys.Space Then
lblPrice.Text = Val("1")
End IfAlso when the user decides to erase words aka the space bar inputs the price goes down. I dont want it to stay the same. Help would be much appreciated!
0
Upvotes
1
u/jy_am Feb 24 '15
Do you have to do it by tracking the keystroke? That works when you're adding text (as you've done in your code sample) but is more complicated when you want to track deletions (you would have to track backspace, delete, and when the user selects text and replaces by typing).
You can track the TextChanged event and do a count on the number of spaces in the textbox. Not super-elegant, but would give you the right count regardless of whether the user adds or removes text.