重新发明Label控件 [英] Re-Inventing The Label Control

查看:156
本文介绍了重新发明Label控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重塑/从头开始重建Label控件添加自己的mojoness它。是的,我知道你在想什么(如果你不认为,你不应该呢?)。

I need to reinvent/recreate the Label Control from scratch to add my own mojoness to it. Yes, I know what you're thinking (and if you're not thinking that, shouldn't you be?).

有人能指出我朝着正确的方向?

Can somebody point me in the right direction?

感谢你。

重新创建标签的整个目的是,我想完全控制它的绘制到屏幕上,所以我可以有keydown事件处理程序这一点。例如,用户可以将他们编辑TextBox控件的内容相同的方式修改标签的内容。

The whole purpose for recreating the label is that I want full control over how it is drawn onto screen, and so that I can have KeyDown Event Handlers for it, too. For example, the user can edit the contents of a label the same way they would edit the contents of a TextBox control.

另外,我不能只是简单地用一个TextBox控件,因为这需要差不多,如果没有更多的工作,让我期望的结果。

Also, I cannot just simply use a TextBox control, as it would require almost, if not more work to get my desired result.

推荐答案

创建自己的标签控制是很简单的,你只需要开始控制和重写OnPaint()方法。这是怎么回事,以字节是把它变成一个控制,也注重行为。并允许用户编辑文本。通过你做的时候,你就必须重新发明了TextBox控件。这是很多比它看起来更难。

Creating your own label control is simple enough, you just need to start with Control and override OnPaint(). What is going to byte is to turn it into a control that also has focusing behavior. And allows the user to edit the text. By the time you're done, you'll have re-invented the TextBox control. Which is a lot harder than it looks.

重点聚焦第一,这是最棘手的问题。不是很可能用户将要经常集中控制。也许某种秘密的握手,就像一个双击。当你发现一个,你可以创建一个TextBox控件,并把它在标签的正面。和处置它时,它失去焦点,更新标签的Text属性。或者说,显示一个小编辑对话框。一个简单的上下文菜单

Focus on the focusing first, that's the trickiest problem. It isn't very likely that the user will want to focus the control frequently. Maybe some kind of secret handshake, like a double-click. When you detect one, you could create a TextBox control and put it in front of the label. And dispose it when it loses focus, updating the label's Text property. Or a simple context menu that displays a small editing dialog.

这是使用的一个例子双击编辑方式:

An example that uses the double-click to edit approach:

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);
  }
}

这篇关于重新发明Label控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆