-
Jan 22nd, 2012, 08:29 AM
#1
Thread Starter
New Member
Strings to integers in a list box
Hi all,
List boxes in VB 6 can be filled with strings,bit how can I fill them with integers?.
For example if I wanted to take a number from list box 1 and add it to list box 2 and place the result in a textbox.
One way round it is by initiating a load of Int variables then adding an If statement like this
Dim a As Int
Dim b As int
If List1.Text = 1 then
a= 20(or any integer)
end if
If List1.Text = 2 then
a= 20(or any integer)
Text1.Text = a
..and so on.But this could take all day especially if I have hundreds of numbers in my list box.
So is there any way I can just convert the strings to integers...or even just enter the list box items as ints(or floats or any numeric)
Kind Regards
Tommy
-
Jan 22nd, 2012, 08:50 AM
#2
Re: Strings to integers in a list box
Most controls (including the TextBox and ListBox) can only contain Strings.
You can convert Strings to other data types using a variety of functions, such as CInt (Convert to INTeger), eg:
Code:
a = CInt(List1.Text)
The ListBox does have an extra hidden list (the same size/order as the main one) called ItemData which stores Integer values, but that takes extra effort that probably isn't worth it for this.
-
Jan 22nd, 2012, 09:46 AM
#3
Thread Starter
New Member
Re: Strings to integers in a list box
Hi Si,
At what point (and where) would you tell the program to convert the string to an integer.
For example
I place two list boxes,a button and a text box on my form
In both my lists I place the numbers 1 to 50.
So the user can scroll and find a number.
I then wrote
Dim a As Integer
Dim b As Integer
a= Cint(List1.Text)
b= CInt(List2.Text)
Text1.Text = a+b
However I was presented with a runtime error 13 type mismatch.
The same thing happened when I replaced the original variables (a and b) with a string definition Like this
Dim a As String
Dim b As String
I also tried using List1.ItemData instead of Text but got the same problem.
Ang suggestion would be appreciated
Kind Regards
Tommy
-
Jan 22nd, 2012, 11:54 AM
#4
Re: Strings to integers in a list box
CInt(List1.Text) & CInt(List2.Text) should only result in type mismatch if:
1) The .Text values are blank
2) The .Text values are non-numeric
3) No list item is selected
Ensure none of the above apply
-
Jan 22nd, 2012, 12:58 PM
#5
Re: Strings to integers in a list box
Trust the VB's internal type conversion features.
If you try to assign a variable to another type of variable, VB tries to convert it automatically for you.
So you can directly do:
Code:
Dim a As Integer
Dim b As Integer
a = List1.Text
b = List2.Text
Here a and b are of type integers. So VB will automatically try to covert List1.Text and List2.Text to integers before assinging to a and b. You don't need an explicit conversion.
However when doing calculations, be careful about the datatypes of the variables.
So things like the following might show different results and you should be cautious about such things.
Code:
MsgBox List1.Text + List2.Text
MsgBox a + b
-
Jan 22nd, 2012, 01:07 PM
#6
Re: Strings to integers in a list box
Originally Posted by Pradeep1210
...
Here a and b are of type integers. So VB will automatically try to covert List1.Text and List2.Text to integers before assinging to a and b. You don't need an explicit conversion.
Type mismatch error will still occur if the .Text values cannot be converted to integer, explicitly or via automatic VB type-conversion. The problem I believe is one of the issues noted in my previous reply
-
Jan 22nd, 2012, 01:17 PM
#7
Re: Strings to integers in a list box
Whether you use the CInt() or you don't use it, the result will be the same.
If the value can be converted, it will be converted. If it can't then it will result in an error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|