Make TextBox uneditable
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Making a text box uneditable prevents users from modifying its content while still displaying the value. There are two main approaches across platforms: readonly (the field is not editable but its value is still submitted with forms) and disabled (the field is completely non-interactive and its value is not submitted). The correct choice depends on whether you need the value included in form submissions and whether the field should be focusable. This article covers HTML, WPF, WinForms, Android, and iOS implementations.
HTML: readonly Attribute
readonly keeps the field focusable (you can tab to it and select text) and includes its value in form data. disabled grays out the field and excludes its value from form submissions.
HTML: Setting readonly with JavaScript
In JavaScript, use element.readOnly = true (camelCase) for the readonly property. For disabled, use element.disabled = true or setAttribute('disabled', 'disabled').
CSS: Styling Readonly Fields
Browsers apply default disabled styling (grayed out), but readonly fields look identical to editable fields by default. Add custom CSS to visually distinguish read-only fields.
React
In React, use the readOnly prop (camelCase). Combine with state to toggle editability dynamically.
WPF (C#)
WPF's IsReadOnly allows text selection and copying but prevents editing. IsEnabled = false grays out the control completely.
WinForms (C#)
In WinForms, setting ReadOnly = true changes the background to gray by default. Override BackColor if you want a different appearance.
Android (Kotlin)
Android's EditText does not have a direct readonly property. Disable it with enabled="false" or prevent focus with focusable="false".
iOS (Swift)
UITextView.isEditable = false is the closest to HTML's readonly — the user can scroll and select text but cannot edit. isEnabled = false disables all interaction.
Common Pitfalls
- Using disabled when readonly is needed:
disabledfields do not submit their values with HTML forms. If the server needs the value, usereadonlyinstead. - Forgetting visual feedback:
readonlyfields look identical to editable fields by default in HTML. Users cannot tell the field is locked unless you add CSS styling (gray background, different cursor). - Readonly does not prevent JavaScript modification:
readonlyonly prevents user input. JavaScript can still changeelement.value. For security, always validate on the server — client-side readonly is a UX hint, not a security measure. - contenteditable divs ignore readonly: HTML
readonlyanddisabledonly work oninputandtextareaelements. Forcontenteditabledivs, setcontenteditable="false"or usepointer-events: nonein CSS. - Android EditText focus issues: Setting
enabled="false"changes the visual style. If you only want to prevent editing while keeping the normal appearance, usefocusable="false"andfocusableInTouchMode="false"instead.
Summary
- Use
readonlyin HTML when the value should be submitted with the form but not editable - Use
disabledwhen the field should be completely non-interactive and excluded from submissions - In WPF, use
IsReadOnly="True"; in WinForms, useReadOnly = true - In Android, set
enabled="false"orfocusable="false"on EditText - In iOS, set
isEditable = falseon UITextView orisEnabled = falseon UITextField - Always add visual styling to indicate that a field is read-only — browsers do not style
readonlyby default

