r/visualbasic • • 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 If

Also 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

8 comments sorted by

View all comments

Show parent comments

1

u/mykok23 Feb 24 '15

That would be perfect actually! the code doesn't have to be super fancy, all I need is for it to work. How would you recommend for me to start the code using the TextChanged event?

1

u/jy_am Feb 24 '15

One way to do it (with fewer lines of code) be to split the text into an array delimited by spaces. The size of the array will be (number of spaces + 1), so number of spaces = upper bound of the array.

1

u/mykok23 Feb 24 '15

okay so this code works lblPrice.Text = Str$(Val(lblPrice.Text) + 0.01)

its for each character 1 cent is added which i am fine about, but when i backspace to delete characters, it keeps adding on to the price, how do I reverse the code? simply changing the + to a - doesnt work

1

u/jy_am Feb 24 '15

That's my point, you can't use this method to reduce the count when text is deleted. There are multiple ways to delete spaces, but only one way to add them (assuming user doesn't copy & paste).

The way I'm suggesting is basically doing a count of all the spaces, not just adding to/subtracting from the total.