Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The typical approach here is a UserControl in which you put both the label and the text box. It is painful though, you have to add a lot of the properties and events of the text box to the user control so it at least resembles a text box. Ugly boilerplate code.</p> <p>Another way to do it is to make a custom text box that sneaks in a label control on the parent. That completely behaves like a TextBox without having to do any work. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Set the Description property to the text you want to see appear in the label.</p> <pre><code>using System; using System.Drawing; using System.Windows.Forms; class MyTextBox : TextBox { private Label label; public MyTextBox() { label = new Label(); label.AutoSize = true; label.Font = this.Font; label.Location = this.Location; label.Resize += new EventHandler(label_Resize); } protected override void OnParentChanged(EventArgs e) { // Keeps label on the same parent as the text box base.OnParentChanged(e); label.Parent = this.Parent; // NOTE: no dispose necessary } private void moveLabel() { // Keep label right-aligned to the left of the text box label.Location = new Point(this.Left - label.Width - 10, this.Top); } private void label_Resize(object sender, EventArgs e) { moveLabel(); } protected override void OnLocationChanged(EventArgs e) { base.OnLocationChanged(e); moveLabel(); } public string Description { get { return label.Text; } set { label.Text = value; } } public override Font Font { get { return base.Font; } set { base.Font = label.Font = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload