CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2011
    Posts
    9

    Question Moving to next item in combobox

    I am creating a combo box dynamically in excel sheet. Suppose say i am having 5 items in my combo box {a,b,c,d,e}. Now after pressing the drop down and if i press down arrow, 'b' will come in the combo box and combo drop down will disappear. Also if it displays 'c' initially and if I press up arrow, 'b' will be placed in the combo box and the dropdown will disappear.
    But this is not the behaviour I am looking for.. If i press down arrow, it should move from 'a to e', also if i press up arrow, it should move from 'e to a'. only after i click the value or press enter, the selected value should come in the combo box.. I have attached the code which we written for creating the combobox, dropdown and key down events.
    Please provide some solution.
    RefCountedPtr<TerShape> CExcelSheet::CreateDropDownShape(RangePtr range)
    {
    try
    {
    static CComBSTR ACTIONMETHOD(L"tl_cpphlp_DropDownAction");

    double top = 0;
    double width = 0;
    double height = 0;
    double left = 0;


    CShapeInfo::GetShapeSize( xlDropDown, range, &top, &left, &width, &height );

    CComPtr<IDispatch> pObjDisp;
    m_worksheet->raw_OLEObjects(vtMissing, 0, &pObjDisp);
    CComQIPtr<OLEObjects> pOLEObjs(pObjDisp);

    CComPtr<_OLEObject> pComboBox;
    CComVariant varClassType("Forms.ComboBox.1");
    CComVariant varLeft(left);
    CComVariant varTop(top);
    CComVariant varWidth(width);
    CComVariant varHeight(height);

    TemporarilyActiveSheet active(m_worksheet);

    HRESULT hr = pOLEObjs->raw_Add (
    varClassType,
    vtMissing,
    vtMissing,
    vtMissing,
    vtMissing,
    vtMissing,
    vtMissing,
    varLeft,
    varTop,
    varWidth,
    varHeight,
    &pComboBox );

    if (FAILED(hr))
    {
    throw hr;
    }

    TerOLEShape* tempShape = new TerOLEShape();
    RefCountedPtr<TerShape> tlShape = tempShape;

    tempShape->Initialize(pComboBox);

    return tlShape;
    }
    catch (HRESULT hr)
    {
    throw hr;
    }
    catch(...)
    {
    return NULL;
    }
    }

    STDMETHODIMP TerOLEShape:ropButtonClick ( )
    {
    if(IsVisible() && (0 != m_ComboBox.p))
    {
    switch(clickBit)
    {
    case 0:
    {
    clickBit = 1;
    m_ComboBox->PutMatchEntry(MSForms::fmMatchEntryComplete);
    // The code below is for the case that the combobox has same string but the case is different.
    // Because the combobox doesn't care the case, it could mark wrong text.
    // Before showing the combobox,
    // compare the current text with the marked text in the box honoring their case,
    // if it's not same, find it from the rest of the list. It should be below.
    CComBSTR text;
    long lcount;
    CComVariant var, varValue;
    CComVariant vmiss = vtMissing;
    m_ComboBox->get_Text(&text);
    m_ComboBox->get_ListIndex(&var);
    m_ComboBox->get_List(&var, &vmiss, &varValue); // get the marked text
    if (varValue.vt == VT_NULL)
    { // ChangeType doesn't handle this
    varValue.vt = VT_BSTR;
    varValue.bstrVal = NULL;
    }
    else
    {
    varValue.ChangeType(VT_BSTR);
    }
    if (BstrCompare(varValue.bstrVal, text, false) != 0) // text is different
    {
    m_ComboBox->get_ListCount(&lcount);
    for (long l=var.lVal+1; l < lcount; ++l)
    {
    var = l;
    m_ComboBox->get_List(&var, &vmiss, &varValue);
    varValue.ChangeType(VT_BSTR);
    if (BstrCompare(varValue.bstrVal, text, false) == 0)
    {
    m_ComboBox->put_ListIndex(&var); // found the same one
    break;
    }
    }
    }
    }
    break;
    default:
    clickBit = 0;
    m_ComboBox->PutMatchEntry(MSForms::fmMatchEntryComplete);
    }
    }
    return S_OK;
    }

    STDMETHODIMP TerOLEShape::KeyDown (struct MSForms::IReturnInteger * KeyCode, short Shift )
    {
    if(IsVisible() && (0 != m_OLEObject.p))
    {
    int key;
    KeyCode->get_Value(&key);
    if(key == TAB_KEY || key == ENTER_KEY)
    {
    keydownFlag = true;
    }
    }

    return S_OK;
    }

    // Take action when the user presses the TAB or ENTER key.
    // TAB - Apply change and move to the cell to the right of the current selection.
    // ENTER - Apply change and move to the cell below the current selection.
    STDMETHODIMP TerOLEShape::KeyUp (struct MSForms::IReturnInteger * KeyCode, short Shift )
    {
    if(keydownFlag && IsVisible() && (0 != m_OLEObject.p))
    {
    int key;
    KeyCode->get_Value(&key);
    if(key == TAB_KEY || key == ENTER_KEY)
    {
    keydownFlag = false;
    InternalClick(false);
    // InternalClick may delete m_OLEObject
    if (m_OLEObject.p == NULL)
    return S_OK;
    CComPtr<Range> cell;
    long row=1,col=1;
    m_OLEObject->get_TopLeftCell(&cell);

    if ( 0 == cell.p )
    return S_OK;
    //Move to the cell to unhidden row below the current selection if user pressed the enterkey.
    if(key == ENTER_KEY)
    {
    CComVariant varRow, varCol;
    varCol = col;
    row = 2;
    VARIANT_BOOL isHidden = VARIANT_TRUE;
    while(isHidden)
    {
    CComVariant nextRow;
    varRow = row;
    cell->get_Item(varRow, varCol, &nextRow);
    if(nextRow.vt == VT_DISPATCH)
    {
    CComQIPtr<Range> newcell(nextRow.pdispVal);
    nextRow.Clear();
    CComPtr<Range> entireRowRange ;
    newcell->get_EntireRow(&entireRowRange) ;
    if ( entireRowRange.p != 0 )
    {
    CComVariant isRowHidden;
    entireRowRange->get_Hidden(&isRowHidden);
    isRowHidden.ChangeType( VT_BOOL );
    if ( isRowHidden.vt == VT_BOOL )
    {
    if(isRowHidden.boolVal== VARIANT_FALSE) //exitout if found the unhidden row
    break;
    isHidden = isRowHidden.boolVal;
    }

    }
    }
    row = row + 1;
    }
    }
    else
    col = 2; //move to right side cell if user pressed the TAB key.

    CComVariant varrow, varcol, varres;
    varrow = row;
    varcol = col;
    cell->get_Item(varrow, varcol, &varres);
    if(varres.vt == VT_DISPATCH)
    {
    CComQIPtr<Range> newcell(varres.pdispVal);
    varres.Clear();
    if(newcell)
    newcell->raw_Activate(&varres);
    }

    }
    }
    return S_OK;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Moving to next item in combobox

    Quote Originally Posted by Rayner View Post
    I am creating a combo box dynamically in excel sheet. Suppose say i am having 5 items in my combo box {a,b,c,d,e}. Now after pressing the drop down and if i press down arrow, 'b' will come in the combo box and combo drop down will disappear. Also if it displays 'c' initially and if I press up arrow, 'b' will be placed in the combo box and the dropdown will disappear.
    But this is not the behaviour I am looking for.. If i press down arrow, it should move from 'a to e', also if i press up arrow, it should move from 'e to a'.
    What is wrong to use PAGE_DOWN and PAGE_UP instead?

    Quote Originally Posted by Rayner View Post
    ... only after i click the value or press enter, the selected value should come in the combo box..
    Perhaps, you'd try to *not* select an item but set its caret index (but to achieve that you will have to subclass the listbox of your combobox)
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    9

    Re: Moving to next item in combobox

    same behaviour for page up and page down..

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

    Re: Moving to next item in combobox

    I created a simple dialog app with a combo on it and the up and down arrows scroll to the next item without closing the list.

    I don't know what you're doing with all the COM stuff, but what you're describing isn't normal behavior for a combo.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Moving to next item in combobox

    Quote Originally Posted by Rayner View Post
    same behaviour for page up and page down..
    What do you mean by "same behaviour"? The previous/next item is selected?
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2011
    Posts
    9

    Re: Moving to next item in combobox

    page up will take to previous item and page down will take to next item( same as UP/DOWN )

  7. #7
    Join Date
    Nov 2011
    Posts
    9

    Re: Moving to next item in combobox

    we have over-ridden all the properties of excel and make our own excel with com components. That's the reason why we have over-ridden the properties of combobox too..

Tags for this Thread

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