Computing.Net > Forums > Programming > Problem with text selection in edit

Problem with text selection in edit

Reply to Message Icon

Original Message
Name: Fozzie
Date: September 9, 2003 at 19:49:08 Pacific
Subject: Problem with text selection in edit
OS: Win98 SE
CPU/Ram: Athlon 1.13Ghz
Comment:

I am having yet another problem! This one has me stumped. I am trying to select text in a multi-line edit control with the EM_SETSEL message. It just plain doesn't work at all when I try. No text is selected, no cursor (or caret) anywhere. I am positive that I am using the correct handle because I have sent an EM_REPLACESEL message using the same handle and that message works like it should. (See the code below) I have checked for the EN_ERRSPACE message to see if EM_SETSEL was causing an error message to be generated but there is never any error message. Here's the code:

    if(GetOpenFileName(&ofn))
     {
          HWND hEdit;
          hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);

          LoadTextFileToEdit(hEdit, szFileName);
          SetWindowText(hwnd, szFileName);
          SendMessage(GetDlgItem(g_hMainWindow, IDC_MAIN_STATUS), 
SB_SETTEXT, 0, (LPARAM)szFileName);
        
          SendMessage(hEdit, EM_REPLACESEL, TRUE, (LONG)"Man 
this sucks");
          SendMessage(hEdit, EM_SETSEL, 0, -1);
          return TRUE;

     }

I can't find anything in the code that creates the window that would prevent EM_SETSEL from working.

               hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
"", 
                    WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL 
| ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                    0, 0, 100, 100, hwnd, (HMENU)IDC_CHILD_EDIT, GetModuleHandle(NULL), 
NULL);


Does the edit control have to be a rich edit control for EM_SETSEL to work? The API programmers ref makes it sound like it doesn't.

If I can get this to work then I can set up my search function and my program will be able to do everything that I wanted it to, but this problem is driving me nuts! Any ideas?

(I'm using Borland C++ Builder 5.5)


Report Offensive Message For Removal


Response Number 1
Name: Fozzie
Date: September 10, 2003 at 21:07:31 Pacific
Reply: (edit)

Ok, I'm gonna answer my own question again after HOURS of reading, searching the internet (to no avail), and experimenting.

It turns out that the EM_SETSEL message was working but it did not give any visible confirmation of the selection, and the highlighted text is exactly what I was tring to acheive. I found this out by setting some different text to the window after using EM_SETSEL, 0, -1 (which selects all text) and instead of just adding the new text to the original text, the new text completely replaced the old text.

Now I still had a problem, the selection was not highlighted! I read carefully again through all of the edit control messages in the API reference and decided to try sending the WM_SETFOCUS message after setting the selection. I had a really good feeling about it as I compiled the app once again for probably the 120th time and lo and behold after only wasting probably half a day or more all told since first trying to use this incredibly straight-forward API function which should have been immediately accessible, ....IT WORKED!

Mind you, it never states, to the best of my knowledge in the API reference that one might have to send the WM_SETFOCUS message to your edit control to make an application defined selection appear highlighted. A troubleshooting guide would be an enormous help to me as I learn this stuff, but alas there is and will be no such thing.

Done ranting
Fozzie


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Problem with text selection in edit

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge