Thread: Edit Control

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

    Edit Control

    I need to develop an edit control that will only highlight one character in a string. For instance, the string "TestIng" would be displayed in the control and I only wish to highlight the "I" in "TestIng". Anybody have any suggestions on how to do this?

    Thanx

    Bob

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!

    You can send the EM_SETSEL message to your edit control to highlight one or more characters. Is that what you are after?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I've tried the EM_SETSEL message and it highlights the whole string. I'm trying to highlight just one character in a string. For instance the character in the fifth position of a string should be highlighted and all the other characters in the string should NOT be highlighted.

    I've used the following but it highlights the whole string

    SetFocus(GetDlgItem(hDlg, IDC_EDIT2));
    SendMessage( GetDlgItem(hDlg, IDC_EDIT2), EM_SETSEL, 0, MAKELONG(5,5) );

    Bob

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If you read the msdn page that anonytmouse helpfully provided a link to, you should notice that, generally, it's:
    Code:
    SendMessage(wnd_handle,EM_SETSEL,start position,end position);
    or, for your specific example:
    Code:
     SendMessage( GetDlgItem(hDlg, IDC_EDIT2), EM_SETSEL, 4,5 );
    Last edited by Ken Fitlike; 03-25-2005 at 01:17 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The Makelong is just a macro that does essentially the same thing. I've reviewed a lot of MSDN docs and have seen it done both ways on MSDN. Please refer to MSDN technical docs on edit controls listed below. But anyway, I've tried the other approach and I still get the same total string highlight.

    Bob


    MSDN technical docs

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Thankyou for providing the link.

    The article you have linked to is actually out of date with respect to the EM_SETSEL message (it may be in other respects; I haven't checked); if EM_SETSEL is used as described in that page then the behaviour you observe occurs because the text selection start position is zero and the end position is the result of the MAKELONG/MAKELPARAM macro.

    I can only assure you that if you use the message as I have previously described then it will highlight the single character as you have requested.

    BTW, a more up-to-date reference for edit controls can be found here:

    http://msdn.microsoft.com/library/de...itcontrols.asp
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by BobS0327
    The Makelong is just a macro that does essentially the same thing.
    No it dosen't.

    Code:
    EM_SETSEL 
    wParam = (WPARAM) (INT) nStart;    // starting position 
    lParam = (LPARAM) (INT) nEnd;      // ending position
    EM_SETSEL expects the starting address (a long value) to be the wParam argument and the ending address to be the lParam argument.
    MAKELONG packs the 2 values into one long value and you have used them as the lParam argument.
    Last edited by Quantum1024; 03-25-2005 at 02:21 PM.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    For completeness, here's a small example that displays 0123456789 in an edit and highlights the fifth character, '4':
    Code:
    #include <windows.h>
    #include <tchar.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    static HWND hEdit;
    switch (uMsg)
      {
      case WM_CREATE:
        hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T("0123456789"),
                             WS_CHILD|WS_VISIBLE,
                             10,10,200,30,hwnd,0,GetModuleHandle(0),0);
        /*highlight 5th character*/
        SendMessage(hEdit,EM_SETSEL,4,5); 
        SetFocus(hEdit);
        return 0;
      case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
      default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
      }
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR pStr,int nCmd)
    {
    HWND       hwnd;
    MSG        msg;
    TCHAR      classname[]=_T("kenf's_wnd");
    WNDCLASSEX wcx={0};
    
    wcx.cbSize       =sizeof(wcx); 
    wcx.lpfnWndProc  =WndProc; 
    wcx.hInstance    =hInst; 
    wcx.hIcon        =(HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0, 0,LR_SHARED); 
    wcx.hCursor      =(HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0, LR_SHARED);
    wcx.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1); 
    wcx.lpszClassName=classname; 
    
    if (RegisterClassEx(&wcx))
      {
      hwnd=CreateWindowEx(0,classname,
                          _T("Highlight Single Character in Edit Control"),
                          WS_OVERLAPPEDWINDOW,
                          GetSystemMetrics(SM_CXSCREEN)/4,
                          GetSystemMetrics(SM_CYSCREEN)/4,
                          GetSystemMetrics(SM_CXSCREEN)/2,
                          GetSystemMetrics(SM_CYSCREEN)/2,
                          0,0,hInst,0);
      if (hwnd)
        {
        ShowWindow(hwnd,nCmd);
        UpdateWindow(hwnd);
        
        while (GetMessage(&msg,0,0,0)>0)
          {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
          }
        return msg.wParam;
        }
      else
        {
        MessageBox(0,_T("Wnd creation failure"),_T("Error"),MB_OK|MB_ICONERROR);
        return 0;
        }
      }
    MessageBox(0,_T("Wnd registration failure"),_T("Error"),MB_OK|MB_ICONERROR);
    return 0;
    }
    Hope that's of some use to you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Ken,

    You and the others are correct. Please accept my apologies. My edit control works fine now.

    Thanx

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I send a text string to an edit control
    By marc74 in forum Windows Programming
    Replies: 5
    Last Post: 01-06-2005, 10:14 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. Restricting input to an edit control
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2003, 01:10 AM
  4. endless edit control
    By ZerOrDie in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2003, 02:51 AM
  5. Keeping focus on an edit control ...
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 02-19-2002, 02:12 AM