CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: On Combo Box

  1. #1
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    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.

  2. #2
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    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

  3. #3
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    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. ?

  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: On Combo Box

    create a new member variable of type control for the combobox.

    Then call GetItemData on the control.

    Laitinen

  5. #5
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    Re: On Combo Box

    i am still getting the retuen value as 0.

  6. #6
    Join Date
    Dec 2007
    Posts
    13

    Re: On Combo Box

    yes ,i think you are right.

  7. #7
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    Re: On Combo Box

    need a solution for this.. i know its right. that is why i send a message like that.

  8. #8
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: On Combo Box

    Quote 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:
    Quote 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

  9. #9
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    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?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: On Combo Box

    Quote 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 []

  11. #11
    Join Date
    Oct 2007
    Location
    Bangalore, India
    Posts
    33

    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?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: On Combo Box

    Quote 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?

  13. #13
    Join Date
    Jun 2006
    Posts
    645

    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.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: On Combo Box

    Quote 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.

  15. #15
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    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

Page 1 of 2 12 LastLast

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

Featured