Re-exposing tag management

I need to reinvent / recreate Label Control from scratch in order to add my modjoness to it. Yes, I know what you are thinking about (and if you are not thinking about it, right?).

Can someone point me in the right direction?

Thank.

The whole purpose of recreating the shortcut is that I want to get full control over how it is drawn on the screen, and that I also have KeyDown event handlers. For example, a user can edit the contents of a label in the same way as edit the contents of a TextBox control.

Also, I just can't just use the TextBox control, as it will require almost, if not more work to get the desired result.

+5
source share
5 answers

Creating your own label control is easy, you just need to start with Control and override OnPaint (). What happens with a byte is to turn it into a control that also has focusing behavior. And allows the user to edit the text. When you're done, you reinvented the TextBox control. It is much more complicated than it seems.

, . , . , - , . , TextBox . , , Text . , .

, :

using System;
using System.Windows.Forms;

class MyLabel : Label {
  private TextBox mEditor;
  protected override void OnDoubleClick(EventArgs e) {
    if (mEditor == null) {
      mEditor = new TextBox();
      mEditor.Location = this.Location;
      mEditor.Width = this.Width;
      mEditor.Font = this.Font;
      mEditor.Text = this.Text;
      mEditor.SelectionLength = this.Text.Length;
      mEditor.Leave += new EventHandler(mEditor_Leave);
      this.Parent.Controls.Add(mEditor);
      this.Parent.Controls.SetChildIndex(mEditor, 0);
      mEditor.Focus();
    }
    base.OnDoubleClick(e);
  }
  void mEditor_Leave(object sender, EventArgs e) {
    this.Text = mEditor.Text;
    mEditor.Dispose();
    mEditor = null;
  }
  protected override void Dispose(bool disposing) {
    if (disposing && mEditor != null) mEditor.Dispose();
    base.Dispose(disposing);
  }
}
+1

?

class MyMojoLabel : Label // Kind of thing

+5

, Microsoft .

, Reflector Label.

+3
source
public class MyLabel : System.Windows.Forms.Label
{
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        // or leave base out

        // you would determine these values by the length of the text
        e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Red), 
                               0, 0, 50, 12);

    }

    protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        base.OnKeyDown(e);

        // although a label has a KeyDown event I don't know how it would 
        // receive focus, maybe you should create a text box that looks
        // like a label
    }
}

How about this?

+2
source

Create a class that inherits from the control. Use the SetStyle () call to allow drawing and double buffering of the user, as well as overriding OnPaint () and any other methods you need.

class MyLabel : System.Windows.Forms.Control
{
    public MyLabel()
    {
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        ControlPaint.DrawBorder3D(  e.Graphics, this.ClientRectangle, Border3DStyle.Etched, Border3DSide.All);
        e.Graphics.DrawString(this.Text, this.Font, Brushes.Red, 0, 0);
    }
}
+2
source

All Articles