Button inside a WinForms textbox
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
WinForms does not have a built-in TextBox with an embedded button like many modern search fields. If you want a clear button, browse button, or password-toggle button inside the text area, the practical solution is to build a small composite control.
The core idea is simple: host a borderless TextBox and a narrow Button inside a parent control that draws the outer border. That keeps the behavior predictable and works better than trying to paint a clickable button directly inside a standard TextBox.
Why a Composite Control Is the Usual Solution
A standard WinForms TextBox is a wrapped native control. It does not expose a supported API for placing child controls inside its client area. You can fake the appearance with custom painting, but handling focus, clicks, resizing, keyboard navigation, and DPI scaling becomes brittle.
A UserControl or custom Control is more maintainable because you control the layout yourself.
A Minimal Working Example
The following custom control places a borderless TextBox and a button side by side inside a reusable UserControl.
Use it from a form like this:
This gives you a reusable control with normal WinForms event handling and predictable layout.
Design Choices That Matter
A borderless inner TextBox is the trick that makes the control look like one unified field. The outer control draws the border, so the text area and the button appear visually integrated.
You should also decide whether the button belongs in the tab order. For a search box, many developers keep the button reachable by mouse only and trigger the action on Enter from the text box. For accessibility-focused workflows, keeping the button tabbable may be better.
If the button is meant to clear the text, you can wire it directly:
For password reveal, you would toggle the inner text box behavior instead of launching a separate action.
Alternatives
You can also use a Panel containing a TextBox and Button without creating a custom class. That is fine for one form, but a dedicated control is better if you need the same pattern more than once.
Another option is subclassing and heavy custom painting, but that is usually more work than the problem deserves in WinForms.
Common Pitfalls
The most common mistake is trying to add a real Button as a child of a standard TextBox. Native text boxes are not designed for that, and the result usually breaks on resize or focus changes.
Another mistake is forgetting DPI and font scaling. Hard-coded positions that look correct at one scaling level can drift badly on another monitor.
Developers also forget to expose the inner text box properties they actually need, such as Text, ReadOnly, or UseSystemPasswordChar. If the composite control is meant to be reused, surface the important properties cleanly.
Finally, watch keyboard behavior. If pressing Enter should activate the embedded button, handle the text box key event intentionally instead of assuming the form’s default button will do the right thing.
Summary
- A standard WinForms
TextBoxdoes not support a true embedded button by itself. - The maintainable solution is a composite control containing a borderless
TextBoxand aButton. - Drawing the outer border on the parent control makes the field look like one integrated widget.
- Expose only the inner properties and events you actually need for reuse.
- Pay attention to resizing, keyboard behavior, and DPI scaling so the control stays usable in real forms.

