Thread: How to cast a ListBox item to an int for a switch statment?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question How to cast a ListBox item to an int for a switch statment?

    I'm at a loss here. I'm trying to run a function with certain
    parameters according to which item the user selects in a listbox.
    I've tried all kinds of different casting methods to try to change
    the item of the listbox into an int or a char so I could use it in a
    switch statement but C++.NET 2k3 doesn't like whatever I try.
    I'm pretty sure that the item coming from the listbox is a
    managed Object*. Am I right?

    I've tried some things like this:

    Code:
    __box(char*) c = reinterpret_cast<__box(char*)>(listboxRadStr->SelectedItem->ToString());
    
    int* i = dynamic_cast<int*>(listboxRadStr->SelectedItem);
    As far as I know, there's no ->ToInt() method.

    Also, if you want to fill a listbox with items (a collection?) and
    you started your project as an empty managed C++ project,
    what's the best way to fill the listbox with items? I just want to
    put numbers 1 - 12 in the listbox, unordered (so i don't get 1,
    10, 11, 12, 2, 3...). I'd like to center the items in the listbox
    too.

    Thanks a bunch for any help you can provide.

    Swaine777

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Unhappy Hmm...

    22 views but no replies...did I stump everyone else too? /

    Swaine777

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    Maybe the lack of answers is because a listbox is nothing wich belongs to Standard C++. For Windows you get more answers in WINAPI-Forum, on .NET you get more answers in the C#-Forum.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Depends on what you want:
    I think you have to find out what info you want on the item:
    -If you want the string, you have to find out the name of the submember of the listitem struct that contains this ie. listboxRadStr->SelectedItem->InfoString
    -If you want the index, you select the index member of the struct ie. listboxRadStr->SelectedItem->index

    As to adding items: If this is a standard listbox, you can add by using the LB_ADDSTRING message.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You can only cast types to types with same size, except for primitive types (char,short, long,float,double), and that includes casting pointers. a pointer by default is 32 bit long. Casting pointers should work almost always. Try casting to void or something... or even a memcpy(...,...,4);

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You can only use integers in a switch statement; strings (i.e. arrays) don't work, and I'm not totally sure if pointers will work either even though they seem to act like integers. If pointers DO work, you might get unexpected results if you're trying to compare the data pointed to by the pointers in the switch, because even if two pointers don't point to the same location, the data they point to may have the same value but won't be recognized as the 'same' anyway. If you're trying to compare strings, you'll need if/else-if unless you have a hash algorithm that will convert your strings into integers - and that's generally more trouble than it's worth (and potentially less efficient too).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you have the name of the listbox item you should be able to use the LB_FINDSTRING message to return the index of the item.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>to return the index of the item.
    I don't believe the problem is one of finding the index of an item. He's trying to use the value of the currently selected listbox item (using .NET stuff it looks like) in a switch statement.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes well I was thinking he could get the index and use that in his switch statement instead. Just a thought...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM