Thread: SetWindowText(...) in richedit controls

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    SetWindowText(...) in richedit controls

    I have a really annoying problem... When I set the text in a richedit or edit control, using SetWindowText("blah") the text appears selected (marked) by default, and I just cant find a way of unmarking it without clicking in the edit window. Anyone but me who have (has?) had problems with this? Im using MFC, Visual C++ .NET.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can control the selection in both Edit and RichEdit controls using the EM_SETSEL message.
    For MFC objects, you can use CEdit::SetSel or CRichEditCtrl::SetSel.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Yeah, I tried that, but neither of my controls (same with both Edit and Richedit) responds to SetSel messages. Someone must have experienced this, and there must be a way to get rid of it...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Time to show some code........starting with your SetSel() calls.

    gg

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Alright, heres the part were I set the text wich results in marked text...

    pRichEditCtrl = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);
    pRichEditCtrl->SetWindowText("texttexttext");
    pRichEditCtrl->SetSel(-1,0);

    I've tried other combinations with SetSel()... like 0, 5.
    I also tried adding a event handler for EM_SELCHANGE but the function doesn't get called... i've run out of ideas.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Actually, SetWindowText() will clear the selection (as it should).

    If you can zip up and attach a very small, wizard-generated dialog app that demonstrates the issue, I'll take a look at it.

    I created a simple dialog app and both CEdit and CRichEditCtrl work as expected.

    gg

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    A really small example of my problem can be found here:
    ftp://213.114.40.164/

    Thank you for helping me out with this! Hope you find something.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Ok.
    Since the Edit control is the first control in the dialog and you are returning TRUE from OnInitDialog(), the system sets the keyboard focus to the Edit control. Apparently, this also means that everything in the control is selected.

    To fix the problem, set the focus explicitly and return FALSE from OnInitDialog().
    Code:
        CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
        pEdit->SetWindowText(_T("Test"));
        pEdit->SetFocus();
        return FALSE;  // return TRUE  unless you set the focus to a control
    gg

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    My hero

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subclassing controls
    By filler_bunny in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2004, 05:43 PM
  2. Problems with my richedit...
    By tyouk in forum Windows Programming
    Replies: 2
    Last Post: 11-02-2003, 04:57 AM
  3. Catching a control's messages
    By DominicTrix in forum Windows Programming
    Replies: 1
    Last Post: 09-06-2003, 03:03 PM
  4. RichEdit Problem
    By dirkduck in forum Windows Programming
    Replies: 0
    Last Post: 07-24-2003, 05:50 PM
  5. I need help disabling Keyboard and Mouse input on Edit controls
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2003, 12:59 AM