Click here to Skip to main content
11,922,641 members (54,418 online)
Rate this:
Please Sign up or sign in to vote.
See more: MFC

Env: Win 7 MS Visual Studio 11

I need to select a ribbon category programmatically ...

I use: pMainFrame->m_wndRibbonBar.SetActiveCategory( pMainFrame->m_wndRibbonBar.GetCategory( 3), TRUE);
and this is selecting the right ribbon (index 3, the fourth category of the ribbon) when I run the exe in XP....

But this code selects the second category ( index 2) when used in Windows 7 !!!

Any idea what is the right way to select a given ribbon category ?

Thanks for your help

Posted 19-Nov-12 12:38pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hello All

Problem Solved.... The trick is not to use the index but the name of the category...

Here is a function that do that:

int CYourClassName::ActivateCategoryByName( CString strCategoryName)
      // Grab Pointer to MainFrame 
  CMainFrame* pMainFrame = ( CMainFrame*) AfxGetMainWnd();
  // Grab Pointer to RibbonBar

  CMFCRibbonBar* pmrb = &pMainFrame->m_wndRibbonBar;
  // Get Category Count
  int nCategoryCount = pmrb->GetCategoryCount();
  // Scan Category

  for ( int nCategoryNdx = 0; nCategoryNdx < nCategoryCount; nCategoryNdx++)
    // Grab Pointer to Category

    CMFCRibbonCategory* pmrc = pmrb->GetCategory( nCategoryNdx);
    // Get Category Name

    CString strName = pmrc->GetName();
    // Check for Requested Category

    if ( strName == strCategoryName)
      pmrb->ShowCategory( nCategoryNdx, TRUE);
      pmrb->SetActiveCategory( pmrc, TRUE);		
      return nCategoryNdx;
  return -1;

Hesam Gholami 9-Sep-13 4:48am
I also have this problem,thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100