Reputation:
How can I get and set the 'read-only' property of an edit box?
Upvotes: 10
Views: 16802
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:
CEdit
, you can call its CEdit::SetReadOnly()
member.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.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
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 ofFALSE
sets the state to read/write.
Upvotes: 9
Reputation: 41
GetDlgItem(blah)->SendMessage(EM_SETREADONLY ,1 ,0);
This will set it to read only.
Upvotes: 4
Reputation: 3434
From the design window: right-click the edit box, select properties. Its the last option on the Styles tab.
Upvotes: 0