Build a Windows Forms Text Field with an Action Button
A text field with a nearby Search, Browse or Clear action can be a reusable composite control. Put a normal TextBox and Button inside a shared bordered container, let the layout system reserve their space, and expose the value and action through a small public API.
The resulting field behaves like two standard keyboard-accessible controls. It does not subclass the native edit window or inject a button into the edit control's client area. That distinction avoids a dependency on text-margin messages, handle recreation and manually calculated reserved widths.
Create the composite field
Add ActionTextField.cs to a .NET 10 Windows Forms project. The first column receives the remaining width, while the action column sizes to its text. The text box has no separate border because the shared container supplies the outer border.
TableLayoutPanel behavior
,
automatic sizing
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
/// <summary>
/// Combines an editable value with a separately focusable action button.
/// </summary>
public class ActionTextField : UserControl {
public event EventHandler? ActionRequested;
private TextBox Input { get; } = new() {
BorderStyle = BorderStyle.None,
Anchor = AnchorStyles.Left | AnchorStyles.Right,
AccessibleName = "Search text",
TabIndex = 0
};
private Button ActionButton { get; } = new() {
Text = "&Search",
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
AccessibleName = "Search",
TabIndex = 1
};
public ActionTextField() {
AutoScaleMode = AutoScaleMode.Font;
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
TabStop = false;
var layout = new TableLayoutPanel {
ColumnCount = 2,
RowCount = 1,
Dock = DockStyle.Fill,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
BorderStyle = BorderStyle.FixedSingle,
Padding = new Padding(6),
Margin = Padding.Empty
};
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.Controls.Add(Input, 0, 0);
layout.Controls.Add(ActionButton, 1, 0);
Controls.Add(layout);
ActionButton.Click += HandleAction;
}
[DefaultValue("")]
public string Value {
get => Input.Text;
set => Input.Text = value;
}
public void FocusInput() => Input.Focus();
protected virtual void OnActionRequested(EventArgs args) => ActionRequested?.Invoke(this, args);
private void HandleAction(object? sender, EventArgs args) => OnActionRequested(args);
}
The button is a sibling of the text box, so typed characters cannot flow underneath it. Standard container disposal owns the child controls. The click publisher is a child with the same composite lifetime. The DefaultValue attribute declares the designer's serialization default for Value, matching the initially empty text box.
Windows Forms property serialization
Use it in a form
This form adds the field and reports its submitted value. It assumes the normal generated Windows Forms entry point calls ApplicationConfiguration.Initialize() and then runs SearchForm.
using System.Drawing;
using System.Windows.Forms;
/// <summary>
/// Demonstrates the field's value and action contract.
/// </summary>
public sealed class SearchForm : Form {
public SearchForm() {
Text = "Search example";
ClientSize = new Size(520, 140);
Padding = new Padding(12);
var search = new ActionTextField { Dock = DockStyle.Top };
search.ActionRequested += (_, _) => Text = $"Search: {search.Value}";
Controls.Add(search);
Shown += (_, _) => search.FocusInput();
}
}
Enter a value, press Tab to reach Search, and press Space. The form title should show the submitted text. The access key in &Search also provides a standard mnemonic. The sample does not assign a form-wide Enter action, because that policy can conflict with another form's default button.
Check the actual layout
Test narrow and wide forms, longer localized button labels, increased font sizes and multiple display scaling settings. Use the application's normal high-DPI initialization. Padding and margins separate the content without pretending that a fixed pixel offset will fit every font. Padding and AutoSize
When adapting the component for another purpose, edit its accessible names to describe that field and action. Verify tab order and screen-reader announcements, including right-to-left UI where applicable. If the action changes from Search to Browse, update the button initializer's visible label and accessibility information together. Windows Forms accessibility
This is a fresh composition-based treatment of Herman Schoenfeld's embedded-button answer , recorded under CC BY-SA 3.0 . It deliberately presents a normal composite field rather than claiming to preserve the historical native-edit implementation. Compilation can check the API surface, while interaction and DPI behavior still require a rendered Windows Forms review.
Author: Herman Schoenfeld