0

I'm having trouble trying to update the caption (text element) of an Edit box in MFC.

Below. is what I currently tried. I know I am grabbing the correct element because I can see that the tooltip has changed. The caption text next to the edit box just changes to blank

*Update: After further collaboration with a senior programmer, we've found this is most likely an MFC bug. I'd love to be proven wrong though because the work-around involves splitting the edit box into a label and a box, which is less than ideal due to alignment issues.

CMFCRibbonBaseElement* pEditBox = (CMFCRibbonBaseElement*)m_wndRibbon.FindByID(EDIT_BOX_ID);
pEditBox->SetText(_T("new Text"));
pEditBox->SetToolTipText(_T("tool tip text"));

I can also see using the debugger that m_strText, which in this case is the caption, is being changed. Am I missing a step where I need to re-render the text for it to show up? The only thing I could think of was calling pEditBox->Redraw(); after setting the text, but this did nothing.

The other approach I tried below was to use SetText in an update function, but this just changes the text in the box, not the title. I thought this might work because I was able to change the text next to a checkbox using this approach.

ON_UPDATE_COMMAND_UI(EDIT_BOX_ID, &CMyDoc::OnUpdateEditBoxText);

void CMyDoc::OnUpdateEditBoxText(CCmdUI *pCmdUI)
{
 if (//my condition) 
    pCmdUI->SetText(_T("new Text"));
}

Another approach I could take is just having the box disappear but pEditBox->SetVisible(FALSE); doesn't work for this edit box either.

thePunctuator
  • 13
  • 1
  • 3
  • Investigate what SetText() really does. – Tom Tom Dec 16 '22 at 20:05
  • @TomTom Thank you for responding! From the MFC website in the CMFCRibbonBaseElement class all it says about SetText(): "Sets the text and keytip for the ribbon element." Is there another place or resource where I can get more information on SetText()? Another idea I had to get more information about my EditBox is to use GetElements from BaseElement: `virtual void GetElements( CArray& arElements);` However, I'm fairly new to C++ and I don't know how to create this arElements with the correct type to fill it with these elements – thePunctuator Dec 16 '22 at 21:41
  • Nevermind. I was able to create the array. Didn't tell me much. – thePunctuator Dec 16 '22 at 22:04
  • The best way is to look in the MFC source code itself. In 'afxbaseribbonelement.cpp' we see it will only set & trim the text. You might to use `Invalidate` & `RedrawWindow` to reender the text. – Tom Tom Dec 20 '22 at 18:30
  • @TomTom Thank you again for responding! Being new, I'm not sure where to find this source code and cpp file. I've found the header file and tried doing Ctrl + K + O to switch to the cpp. Doing a google search brought up a mention of it in the /atlmfc/src/mfc directory, but the file structure is different in the code I am using. – thePunctuator Dec 23 '22 at 16:37
  • If you install VS2019 Professional with the option MFC, then the source code of MFC is usually also installed. You may need also a good filefinder (like everything) to find the correct path of cpp file. – Tom Tom Dec 26 '22 at 23:46

0 Answers0