MFC Highlighting any text editing box when clicked - mfc

I need to highlight all the text in the edit box by clicking the edit box
As the browser's edit box Site address
This button code works.
m_text.SetFocus();
m_text.SetSel(0, -1);
But the editing box event not working
void CMFCApplication99Dlg::OnEnSetfocusEdit1()
{
m_text.SetFocus();
m_text.SetSel(0, -1);
}

The problem is that this is the standard behavior. When you click into an edit control, the location is selected were you point to.
To overcome this behavior, it should be possible to post EM_SETSEL as a window message in your EN_SETCOCUS handler.
void CMFCApplication99Dlg::OnEnSetfocusEdit1()
{
m_text.PostMessage(EM_SETSEL,0,-1);
}
PS: In your code the additional SetFocus is not needed when are inside the EN_SETFOCUS handler.

Related

C++ MFC Button on a second Dialog does nothing

I created a C++ MFC Program with the Visual Studio Wizard. There I set the application type to "Dialog Based".
I have a button on the first dialog, which opens another dialog. I created this second dialog by right clicking on the project -> Add -> Resource -> Dialog -> New.
Then I added a MFC class to the new dialog by double clicking it in resource view.
On the second Dialog I also created a button. I double clicked the button and added some code which should be executed.
When I run the program and click the button on the second dialog, nothing happens and the code is not executed. I did everything like with the button on the first dialog. That one works fine. Why is the second button not working and what do I need to do?
First Dialog
void CMFCApplication1Dlg::OnBnClickedButton1()
{
CDialogEx secondDialog(IDD_DIALOG1);
secondDialog.DoModal();
}
Second Dialog
void SettingsDlg::OnBnClickedButton1()
{
MessageBox(L"Button clicked", L"Button clicked", MB_OK);
}
#andrew-truckle, your side node was the answer! I changed it to:
void CMFCApplication1Dlg::OnBnClickedButton1()
{
SettingsDlg settingsDialog;
settingsDialog.DoModal();
}
Now the button works just as expected. Thank you very much!
Further Info (from #andrew-truckle)
For the benefit of others the issue here was that the original code declared the dialog like this:
CDialogEx secondDialog(IDD_DIALOG1);
That was wrong because the dialog was actually associated with the class SettingsDlg. This is the class that had the message map and event handlers etc. CDialogEx was the base class.
I added this update to the answer to save the reader from locating my comment to the question.

C++ CListControl - check if selected

I've got a ListControl box and I want to enable a textbox if the user clicks on a button there.
I've allready tried to update the dialog via an "OnLeftButtonUp" function which works quite well if I click outside the ListControl but when I click INSIDE the box nothing happens...
Any ideas why?
Cheers

CMFCToolBarComboBoxButton has focus but does not let me edit the contents

I have a CMFCToolBarComboBoxButton in my dialog. The issue is that I am not able to edit the text field of the combo box by selecting it. I know the box has focus, and I know it is hitting the piece of code where I do a SetText to confirm it.
I also do a EnableWindow(true) before that.
However, when I try and edit the box manually, it does not allow me to. No user input is possible on the box. Some other times, the edit window only gets activated if I actually click on the drop down button. I am not setting the EnableWindow() to FALSEanywhere.
What could be the likely cause?

MFC Ribbon edit control needs double click to activate

I created a simple MFC ribbon app using the App Wizard. Then, I removed all the controls in the ribbon and added a simple button, and two edit boxes, and added event handlers for each.
The event handler for the button is a simple message loop as followed :
void CRibbon2Doc::OnButton(){
MSG msg;
while (1){
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
And the message handlers for the two Edit Boxes are also simple as followed:
void CRibbon2Doc::OnEdit1(){
OutputDebugString(_T("Box1"));
}
void CRibbon2Doc::OnEdit2(){
OutputDebugString(_T("Box2"));
}
Here is my problem :
Case : Button not clicked, i.e. my message loop not running
In this case, when I click on an Edit Box, I immediately see the blinking cursor that lets me write inside the Edit box. No problem here.
Case : Button clicked, i.e. my message loop is running
In this case, when I click an Edit Box, I do not see a blinking cursor that will let me write inside the edit box. I need to double click for the blinking cursor to show up. This is a problem, I would like to avoid making the users double click inside a box just to activate it.
Can you please tell me what could be the reason, and how to make it such that I can obtain a blinking cursor inside the edit boxes on a single click in the case where my message loop is running in the background.
I am basically struggling with a real life problem, and this is a simplified version of the problem, and a lot is on the line, please help.

GetFocus on a ComboBox in a dialog

I'm trying to use GetFocus() on a ComboBox control in a dialog box, but for some reason it isn't working.
Even if I set focus with the SetFocus() function it doesn't work, but it looks like it has keyboard focus.
SetFocus(hKeysComboBox);
if (GetFocus() == hKeysComboBox) // This is false
Maybe because it's in a dialog box? I don't know, this seems so simple.
Is this an editable combo? Those actually contain a child Edit control, and when you focus the combo, the combo in turn focuses the child edit control (so that it can receive manage the keyboard input). You can use Spy++ to see this parent/child structure.
So to check if the combo has focus, you could check if the parent of GetFocus is the Combo.
if (VC++)
{
use `tag order` property;
}
else
{
use WM_NEXTDLGCTL with SendMessage;
}
See How to set focus in a dialog box for details.