|
-
December 11th, 2007, 01:13 AM
#1
On Combo Box
i hava a combo box in my application. now this is declared as int. and there are 2 values in the combo box. when i am clicking the value the index is returned. that is known. but what should i do get the original value? i cant map the combo box to CString. that is giving me some errors because this int value which is declared is used in other files too. so it has to an int.
-
December 11th, 2007, 01:48 AM
#2
Re: On Combo Box
i'm not sure i understand what you mean.
If you want to get list of string from index, you can use GetLBText() function,
or if you want to get data inside combobox, then use GetItemData().
Henky
henky
----------------------------------
[email protected] is not my email address anymore...
Jangan Pernah Menyerah
-
December 11th, 2007, 02:28 AM
#3
Re: On Combo Box
i have a combo box which holds 2 values. 100 and 200. if i click on 100 then the index of the value is returned. i need the value not the index. i have declared the combo box as int. and there are lot of places which uses the same value. so cant change the data type. is there any way to get the value. ?
-
December 11th, 2007, 02:45 AM
#4
Re: On Combo Box
create a new member variable of type control for the combobox.
Then call GetItemData on the control.
Laitinen
-
December 11th, 2007, 02:56 AM
#5
Re: On Combo Box
i am still getting the retuen value as 0.
-
December 11th, 2007, 03:59 AM
#6
Re: On Combo Box
yes ,i think you are right.
-
December 11th, 2007, 05:38 AM
#7
Re: On Combo Box
need a solution for this.. i know its right. that is why i send a message like that.
-
December 11th, 2007, 05:46 AM
#8
Re: On Combo Box
 Originally Posted by Chandru080
need a solution for this.. i know its right. that is why i send a message like that.
It might be better to attach your code. So, we'll know what your problem is.
As i wrote:
 Originally Posted by [email protected]
If you want to get list of string from index, you can use GetLBText() function,
or if you want to get data inside combobox, then use GetItemData().
henky
----------------------------------
[email protected] is not my email address anymore...
Jangan Pernah Menyerah
-
December 11th, 2007, 06:05 AM
#9
Re: On Combo Box
<CODE> int value =m_combo1.GetCurSel();
CString str;
str.Format("%d",value);
m_combo1.GetLBText(value,str);
int value1 = atoi(str);</CODE>
m_combo1 is mapped with CCombobox.
now int value1 needs to be passed to another file. how do i hold this value and pass it to another file?
-
December 11th, 2007, 08:19 AM
#10
Re: On Combo Box
 Originally Posted by Chandru080
<CODE> int value =m_combo1.GetCurSel();
CString str;
str.Format("%d",value);
m_combo1.GetLBText(value,str);
int value1 = atoi(str);</CODE>
m_combo1 is mapped with CCombobox.
now int value1 needs to be passed to another file. how do i hold this value and pass it to another file?
Check it step by step.
Does value have the current selection?
I don't know what the str.Format is all about. You don't need it.
What's in str after the call to GetLBText()?
What's in value1?
Use the square brackets for code tags []
-
December 11th, 2007, 11:10 PM
#11
Re: On Combo Box
yes value does have the corrent selection. str.Format is to convert the interger that is selected from the combo box to string. and the value of str is a number that is there in the combo box. value1 will have the interger that is converted to string. i need to pass this value to a file. how do i do it?
-
December 12th, 2007, 07:57 AM
#12
Re: On Combo Box
 Originally Posted by Chandru080
yes value does have the corrent selection. str.Format is to convert the interger that is selected from the combo box to string. and the value of str is a number that is there in the combo box. value1 will have the interger that is converted to string. i need to pass this value to a file. how do i do it?
Okay, but you need to answer the questions. We're trying to find out which line fails.
str.Format does nothing useful for you here. You're converting the index of the selection to a string - why I don't know, then overwriting it with GetLBText().
You say value has the correct selection.
What's in str after you call GetLBText()? Is it right?
-
December 12th, 2007, 02:30 PM
#13
Re: On Combo Box
You can use small arrays of int and CString to do this:
Code:
//------------------------------------------------------------------------------------------
// In the class header...
CArray <int, int> value;
CStringArray str_value;
CComboBox m_Combo1;
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// In the class implementation....
//------------------------------------------------------------------------------------------
// in OnInitialUpdate() OR OnInitDialog()....
for (int i = 0; i < count /* number of elements in combo-box */; i++)
{
this->value.Add(0);
this->str_value.Add(_T(""));
}
// Set your values...if you did not while adding elements....
this->value.SetAt(0, number1);
this->value.SetAt(1, number2);
....
....
// Set the equivalent string values...
for(int k = 0; k < value.GetCount(); k++)
{
int num = this->value.GetAt(k);
str.Format(_T("%i"), num);
this->str_value.SetAt(k, str);
str.Empty();
}
// Or you can just set the numbers in the Add function as well...
// Fill up the combo-box...
int num = 0;
CString str;
for (int j = 0; j < count; j++)
{
// num = this->value.GetAt(j);
// str.Format(_T("%i"), num);
// Instead of the above two statements, use the following....
str = this->str_value.GetAt(j);
this->m_Combo1.AddString(str);
str.Empty();
}
//---------------------------------------------------------------------------------------------
// Handle the CBN_SELCHANGE event...
const int i = m_combo1.GetCurSel();
// CString str;
// m_combo1.GetLBText(value,str);
int value1 = this->str_value.GetAt(i);
//---------------------------------------------------------------------------------------------
// Now do what ever you want with value1.....
I hope that helps a bit. The idea is to sychronize the two separate arrays which helps eliminate the use of atoi() function.....and can be dealt with easily.
P.S.: My code may not be flawless. Regardless of that you would get a clue about how to approach your problem....
Regards,
Bhushan.
Last edited by bhushan1980; December 12th, 2007 at 02:32 PM.
-
December 12th, 2007, 02:40 PM
#14
Re: On Combo Box
 Originally Posted by bhushan1980
You can use small arrays of int and CString to do this:
Code:
//------------------------------------------------------------------------------------------
// In the class header...
CArray <int, int> value;
CStringArray str_value;
CComboBox m_Combo1;
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// In the class implementation....
//------------------------------------------------------------------------------------------
// in OnInitialUpdate() OR OnInitDialog()....
for (int i = 0; i < count /* number of elements in combo-box */; i++)
{
this->value.Add(0);
this->str_value.Add(_T(""));
}
// Set your values...if you did not while adding elements....
this->value.SetAt(0, number1);
this->value.SetAt(1, number2);
....
....
// Set the equivalent string values...
for(int k = 0; k < value.GetCount(); k++)
{
int num = this->value.GetAt(k);
str.Format(_T("%i"), num);
this->str_value.SetAt(k, str);
str.Empty();
}
// Or you can just set the numbers in the Add function as well...
// Fill up the combo-box...
int num = 0;
CString str;
for (int j = 0; j < count; j++)
{
// num = this->value.GetAt(j);
// str.Format(_T("%i"), num);
// Instead of the above two statements, use the following....
str = this->str_value.GetAt(j);
this->m_Combo1.AddString(str);
str.Empty();
}
//---------------------------------------------------------------------------------------------
// Handle the CBN_SELCHANGE event...
const int i = m_combo1.GetCurSel();
// CString str;
// m_combo1.GetLBText(value,str);
int value1 = this->str_value.GetAt(i);
//---------------------------------------------------------------------------------------------
// Now do what ever you want with value1.....
I hope that helps a bit. The idea is to sychronize the two separate arrays which helps eliminate the use of atoi() function.....and can be dealt with easily.
P.S.: My code may not be flawless. Regardless of that you would get a clue about how to approach your problem....
Regards,
Bhushan.
How is that better or easier? If you're going to do something like that a map would be better, but until the OP explains why what should be simple code isn't working, it's a bit premature to propose a kludgey work around, IMHO of course.
Last edited by GCDEF; December 12th, 2007 at 02:42 PM.
-
December 12th, 2007, 02:54 PM
#15
Re: On Combo Box
There are two relatively easy ways to solve your problem.
First, once you get the index from the ComboBox, use the GetLBText to retrieve the text for that item (e.g. "100"). Then convert that to an int using atoi or _ttoi. That int will be the value you want.
The second method involves changing the way you add data to the ComboBox. I assume you're using something like:
Code:
myCombo.AddString(_T("100"));
You could change that to
Code:
int iIndex = -1;
iIndex = myCombo.AddString(_T("100"));
myCombo.SetItemData(iIndex, 100); // this sets the "ItemData" to the integer value of 100
Next, to retrieve the value 100 as an integer instead of the index, you can use
Code:
int myActualValue = 0;
myActualValue = myCombo.GetItemData(myCombo.GetCurSel());
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
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
|