How to disable Edit control's focus on Dialog first launch?
c++, controls, edit, focus, winapi
Solution
You have to return FALSE to WM_INITDIALOG message and set the correct focus by yourself.
Problem
Hello everybody reading this. Thanks in advance for your time. One thing before question: I DO NOT use neither MFC nor Windows Forms, just WinApi in C++. Well, I am making a polynomial calculator in Visual C++. I added a Dialog to it, which was created in resources (`.rc` file) using drag'n'drop method. I suppose there would be no such a problem if i created my Dialog with `CreateWindowEx` (but I don't want to). My Dialog has a few of Edit Controls. Everything is fine except that when the Dialog is launched, one of Edit controls takes focus to be ready to take keyboard input. I have included management of `EN_KILLFOCUS` (Edit sends it to parent when loses focus due to selecting another control). Here I read from control to `wstring` (string of wide characters - `_UNICODE` is defined), use some kind of parser to verify this `wstring` and remove bad characters, and then put correct string into the same edit control. It works fine, but here is the source of my problem: When there was no input, parser returns `string "0"` (not the `NULL`, string is just set to "0"), as if control had focus and then lost it even before I clicked anything in Dialog. Due to that, and something else (this is what I have to figure out), at the Dialog launch parser puts this `string "0"` to edit. I want to make my edit not be able to take input from keyboard until i click one of the Edits (including this one). If it is not possible, I want to clear the whole text at the beginning of dialog (being able to take input is not a problem, I just want to prevent parser from entering `string "0"` at the beginning) My code: In DlgProc I have: ``` //up here is switch to manage all controls case MyEditID: // here is ID of one of my edits from resources switch (HIWORD(wParam)) { case EN_KILLFOCUS: // edit lost focus - another control selected if (LOWORD(wParam)==MyEditID) //necessary to determine if // one of allowed Edits sent this message // because I have also other Edits { GetDlgItemText(hPanel, LOWORD(wParam), MyTempWcharArray, 100); MyTempString.assign(MyTempWcharArray); w1 = polynomial(MyTempWcharArray); // parser takes the string // and removes bad chars in constructor // polynomial is my class - you don't have to care of it // w1 is declared before as object of polynomial class MyTempString = w1.ConversionToString(); SetDlgItemText(hDialog, LOWORD(wParam), sw1); } break; } break; ``` does it matter what integer number is set to Edit's ID? I know `SetFocus()`, and `WM_SETFOCUS` message. In this case I just can't get this working. If i haven't included something important to make you see my point please let me know. I'm sorry I'm just a newbie in `WinAPI` world. EDIT: For those with a similar problem: Do not do this: I made an workaround with global variable ProcessKillFocus set to false indicating that instructions in message management should not be processed, except that at the end (just before break;) I am changing it to true, so next time and later it will be processed: ``` case EN_KILLFOCUS: if (ProcessKillFocus && LOWORD(wParam)==MyEditID) { // first time global ProcessKillFocus is false so all this is skipped // 2nd time and later do all the stuff } ProcessKillFocus = true; break; ``` Huge thanx to Sheyros Adikari for making my question easy to understand!!! Huge thanx to patriiice for simple answer on a huge messing question!!! ANSWER: BTW: patriiice, I tried this: ``` case WM_INITDIALOG: SetFocus(GetDlgItem(hDialog, Desired_Control_ID)); return (INT_PTR)FALSE; break; ``` IT JUST WORKS!!!