Results 1 to 7 of 7

Thread: Strings to integers in a list box

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    10

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    10

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Strings to integers in a list box

    Quote Originally Posted by Pradeep1210 View Post
    ...
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width