visual basic 6 list box to text box calculation?
hi , I have list box in my program , single line in the list contains price and QTY and I have text box called txt_sum , I want program to do calculations when something added in side the list box so answer will be in the text box simultaneously
How list box looks like
e.g 2 QTY. £3.57 ,
- ?Lv 71 decade agoFavorite Answer
Since you may have several text boxes with different bits of info in them, I assume that you are assembling (Concatenating) these different textbox values into a string and loading this string into the list box....
Create a module level private variable
Private m_ListTotal as double 'holds total value of items in list
You will need to also DIM two more variables in your sub/function
dim str as string 'hold a copy of text
dim num as double 'hold converted value
Although you may cram many steps into one line and not use some or all extra variables... I preferr to use them so as to improve readability of the code.
The m_ListTotal value is cleared to zero whenever you clear the listbox
Next whenever you add an item to your list you will use the text in textBox_Price convert the text to a number and add its value to the total
str = textbox_Price.text 'copy text into str
num = cdbl(val(str)) 'convert str into a number and ensure it is of type double
m_ListTotal = m_ListTotal + num 'totalize
You will then format m_ListTotal and display in a labelbox
The key to this working properly is the VAL function....
Val will convert string numbers into a value. This function starts the numeric conversion on the left most character and continues working right until it encounters the first non-numeric character.
It may only convert a portion of a string...
45 ==> 45
45$ ==> 45
$45 ==> 0
45 or 50 ==> 45
If you have difficult because you are including a monetary symbol in your textbox you should limit entry into the textbox to numeric data only through validation. Add your symbol during the string concatenation before adding the string to the listbox