Click to See Complete Forum and Search --> : Displaying Integers in List Box
Kristin Wheeler
May 10th, 1999, 04:18 PM
How can I display an integer in a listbox? I have set the listbox to be of type int in the property panel. I need to know which function I can use to insert an integer value at a given index. You're help is greatly appreciated!
Bob Clarke
May 10th, 1999, 11:52 PM
> I have set the listbox to be of type int in the property panel.
I don't know what you mean by this since the standard listbox has no such property in Visual C++, though in VB you can set a listbox property data format as Number with 0 decimal places.
In C++, convert the integer to a string, then call InsertString():
int nIndex = 2;
char* pInteger = "100";
char pIToA[10]; // make it large enough to hold the largest integer plus a null byte
itoa( pInteger, pIToA, 10);
listbox.InsertString( nIndex, pIToA );
Dan Haddix
May 11th, 1999, 01:29 AM
Actually you can set an editi control to hold in int!!! All you need to do is to go into class wizard and select the dialog class from the drop down liast. The select the member variable tab. Then in the ControlID list double click on the edit controls ID. This will bring up a dialog asking you the name and type of variable you want associated with the edit control. By selcting int as the type it will create an int member variable that is atutomaticly associated with that edit control, and even allows you to set a minimum and maximum value. You can then asign an initial value to the edit control by simply asigning a value to the member variable in the OnInitDialog() function.
Bob Clarke
May 11th, 1999, 02:19 AM
The question involved a listbox, not an edit control.
Dan Haddix
May 11th, 1999, 02:36 AM
Well I just screwed that up didn't I??? Sorry I'll make sure to read the post completely before I open my big mouth next time!!!
Jason Teagle
May 11th, 1999, 03:41 AM
I wonder if the 'properties' box the original poster was talking about was the 'map to member variable' box, for which you CAN select int? If so, this int (for a list box) represents the currently-selected item (or the item which currently has the focus, for multiple-selection list boxes) - not the value displayed.
If this is so, then it does not do what the original poster wanted. I suspect they thought along the lines of the edit control mentioned in later responses, in that you can create a numeric variable which represents the value which gets shown in the control.
I ought to point out that the code given in the above response will not compile, since itoa() takes an integer as the first parameter, not a character string! I wonder if the following would be quicker?:
---
CString strNumber ;
int iValue = 7 ;
strNumber.Format("%d", iValue);
lstBox.AddString(strNumber);
---
To interpret the selected value:
---
CString strNumber ;
int iIndex, iValue ;
iIndex = lstBox.GetCurSel();
if (iIndex != LB_ERR)
{
lstBox.GetText(iIndex, strNumber);
iValue = atoi( (const char *)strNumber);
}
---
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.