C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2004, 11:21 PM   #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
Swaine777 is offline   Reply With Quote
Old 09-26-2004, 02:48 AM   #2
Registered User
 
Join Date: Mar 2003
Posts: 30
Unhappy Hmm...

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

Swaine777
Swaine777 is offline   Reply With Quote
Old 09-26-2004, 03:49 AM   #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.
Koyaanisqatsi is offline   Reply With Quote
Old 09-26-2004, 05:19 AM   #4
Registered User
 
Join Date: Feb 2002
Posts: 328
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.
knutso is offline   Reply With Quote
Old 09-26-2004, 06:46 PM   #5
#include<xErath.h>
 
xErath's Avatar
 
Join Date: Jun 2004
Posts: 724
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);
xErath is offline   Reply With Quote
Old 09-26-2004, 06:59 PM   #6
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,865
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.
Hunter2 is offline   Reply With Quote
Old 09-26-2004, 07:54 PM   #7
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
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
JaWiB is offline   Reply With Quote
Old 09-26-2004, 08:00 PM   #8
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,865
>>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.
Hunter2 is offline   Reply With Quote
Old 09-26-2004, 08:52 PM   #9
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
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
JaWiB is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to combine these working parts?? transgalactic2 C Programming 0 02-01-2009 08:19 AM
Incorporating en-passant and Castling into a chess program mac025 C Programming 2 03-24-2006 08:36 PM
Converted from Dev-C++ 4 to Dev-C++ 5 Wraithan C++ Programming 8 12-03-2005 07:45 AM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
A Simple (?) Problem Unregistered C++ Programming 8 10-12-2001 04:28 AM


All times are GMT -6. The time now is 01:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22