ListBox input as integer ? 
Author Message
 ListBox input as integer ?

Hello,

I'm a newbie.  I'd like to enter user numerical data into two
listboxes and click a button and have the sum displayed in
another listbox.  I've tried to output the data to both a
listbox and a label, but it is apparently seeing the data as
string data and the numbers, instead of being added, are simply
appended on to one another.  

I imagine my problem involves incorrect data types, but I don't
know what to do about it.  I believe the data entered into the
listbox is variant, isn't it?  I don't know how to convert that
to integer or real.  I thought VB might automatically do that for
me when I did the x = mult1.Text, since it didn't flag it at
compile or runtime.  Here is my code:  

Option Explicit
Dim x, y, z As Integer
Private Sub AddThem_Click()
x = mult1.Text
y = mult2.Text
z = y + x
Label2.Caption = z
second.Text = z
End Sub

Any help would be appreciated.  I am using VB6.

Thanks, miker



Fri, 02 Sep 2005 20:30:45 GMT  
 ListBox input as integer ?
Hi!

Look into the function CInt to convert a string to an integer.
The other way around the function is called Str

Get yourself a copy of 'VB&VBA in a Nutshell'

Lars
:-))


Quote:
> Hello,

> I'm a newbie.  I'd like to enter user numerical data into two
> listboxes and click a button and have the sum displayed in
> another listbox.  I've tried to output the data to both a
> listbox and a label, but it is apparently seeing the data as
> string data and the numbers, instead of being added, are simply
> appended on to one another.

> I imagine my problem involves incorrect data types, but I don't
> know what to do about it.  I believe the data entered into the
> listbox is variant, isn't it?  I don't know how to convert that
> to integer or real.  I thought VB might automatically do that for
> me when I did the x = mult1.Text, since it didn't flag it at
> compile or runtime.  Here is my code:

> Option Explicit
> Dim x, y, z As Integer
> Private Sub AddThem_Click()
> x = mult1.Text
> y = mult2.Text
> z = y + x
> Label2.Caption = z
> second.Text = z
> End Sub

> Any help would be appreciated.  I am using VB6.

> Thanks, miker



Fri, 02 Sep 2005 21:57:18 GMT  
 ListBox input as integer ?


It's the usual beginner's problem.  Use Val() when doing arithmetics
on string variables.
do it like this.

Quote:
>Option Explicit
>Dim x, y, z As Integer

Here you are creating x and y as Variant, z as integer.
Dim x as integer, y as integer, z as integer is the proper way.

Quote:
>Private Sub AddThem_Click()
>x = mult1.Text

x = Val(mult1.Text)

Quote:
>y = mult2.Text

y = Val(mult2.Text)

Quote:
>z = y + x
>Label2.Caption = z
>second.Text = z

Note, if second doesn't have that list item, this will not add an item
to the list.  Unless there is a reason to keep the additions in
another listbox, just use a text box for the result.

--

Regards, Frank



Fri, 02 Sep 2005 22:22:09 GMT  
 ListBox input as integer ?
VAL Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr...
vafctVal.asp

-Nobody

<snip>

Quote:
> I'm a newbie.  I'd like to enter user numerical data into two
> listboxes and click a button and have the sum displayed in
> another listbox.  I've tried to output the data to both a
> listbox and a label, but it is apparently seeing the data as
> string data and the numbers, instead of being added, are simply
> appended on to one another.

> I imagine my problem involves incorrect data types, but I don't
> know what to do about it.  I believe the data entered into the
> listbox is variant, isn't it?  I don't know how to convert that
> to integer or real.  I thought VB might automatically do that for
> me when I did the x = mult1.Text, since it didn't flag it at
> compile or runtime.  Here is my code:

> Option Explicit
> Dim x, y, z As Integer
> Private Sub AddThem_Click()
> x = mult1.Text
> y = mult2.Text
> z = y + x
> Label2.Caption = z
> second.Text = z
> End Sub

> Any help would be appreciated.  I am using VB6.

> Thanks, miker



Fri, 02 Sep 2005 22:26:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. forcing input to integers

2. Checking whether input text is an integer

3. Checking whether input text is an integer

4. integer input

5. Listbox.Text to integer value

6. How to Sort integer numbers inside listbox

7. Listbox Index limited to Integer

8. How to sort integer numbers inside Listbox control

9. How to sort integer numbers inside ListBox

10. Formatting an integer for a listbox

11. Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

12. Converting integers and long integers to byte arrays

 

 
Powered by phpBB® Forum Software