railsmail
railsmail

Reputation:

How can I get and set the 'read-only' property of an edit box?

How can I get and set the 'read-only' property of an edit box?

Upvotes: 10

Views: 16802

Answers (4)

IInspectable
IInspectable

Reputation: 51512

Setting or removing the read-only flag for an edit control amounts to sending it an EM_SETREADONLY message. The wParam is a boolean value that specifies whether the read-only flag should be set.

There are a few different ways to do this that ultimately do the same thing under the hood:

  • If you have a CEdit, you can call its CEdit::SetReadOnly() member.
  • If you have a CWnd, you need to send the message explicitly like so:
    wnd_edit->SendMessage(EM_SETREADONLY, TRUE, 0);
    
    Use FALSE in place of TRUE to remove the read-only flag.
  • If you have a bare HWND use the Win32 API:
    ::SendMessage(hwnd_edit, EM_SETREADONLY, TRUE, 0);
    

Curiously, there is no dedicated API to query whether an edit control has the read-only flag set. Instead, you have to request the control's style (using CWnd::GetStyle()) and manually test for the ES_READONLY style:

auto const is_read_only = (wnd_edit->GetStyle() & ES_READONLY) != 0;

Upvotes: 2

Steve Beedie
Steve Beedie

Reputation: 975

The CEdit class has a SetReadOnly method which can be called at run-time. Latest documentation is here.

To quote:

Specifies whether to set or remove the read-only state of the edit control. A value of TRUE sets the state to read-only; a value of FALSE sets the state to read/write.

Upvotes: 9

kobkob
kobkob

Reputation: 41

GetDlgItem(blah)->SendMessage(EM_SETREADONLY ,1 ,0);

This will set it to read only.

Upvotes: 4

yhw42
yhw42

Reputation: 3434

From the design window: right-click the edit box, select properties. Its the last option on the Styles tab.

Upvotes: 0

Related Questions