Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello

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

Thierry
Posted

1 solution

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:

C++
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;
}


Thierry
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900