【问题标题】:Display caption in input field C# [duplicate]在输入字段C#中显示标题[重复]
【发布时间】:2012-07-21 02:00:05
【问题描述】:

可能重复:
Watermark TextBox in WinForms

我目前正在编写 C# 应用程序的设置对话框。 输入字段应如下所示:

实现这一点的最佳方法是什么?我曾想过创建一个背景图像,但我想知道是否有更好的方法来做到这一点(动态的)......

【问题讨论】:

  • 地铁?表格? WPF?银光?视窗电话? ASP.NET?单触?
  • sry 忘了说; Windows 窗体
  • 我不认为这是 KeithS 想要完成的事情,因为我的标题不应该在点击事件触发时消失......

标签: c# winforms


【解决方案1】:

创建一个3个控件的组合,并将其放入另一个UserControl。三个控件是:

  • 文本框,无边框
  • 标签,在文本框的右侧
  • 面板,带有两个边框。

从 Hassan 那里获取食谱,然后将其放在 UserControl 上并将其用作一个新控件。

【讨论】:

    【解决方案2】:

    我认为这几乎可以满足您的需求:

    public class MyTextBox : TextBox
    {
        public const int WM_PAINT = 0x000F;
    
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PAINT:
                    Invalidate();
                    base.WndProc(ref m);
                    if (!ContainsFocus && string.IsNullOrEmpty(Text))
                    {
                        Graphics gr = CreateGraphics();
                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Far;
    
                        gr.DrawString("Enter your name", Font, new SolidBrush(Color.FromArgb(70, ForeColor)), ClientRectangle, format);
                    }
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
    }
    

    在 TextBox 上覆盖 OnPaint 通常不是一个好主意,因为插入符号的位置会被计算错误。

    请注意,标签仅在 TextBox 为空且没有焦点时显示。但这就是大多数此类输入框的行为方式。

    如果提示应该一直可见,您可以将其添加为标签:

    public class MyTextBox : TextBox
    {
        private Label cueLabel;
    
        public TextBoxWithLabel()
        {
            SuspendLayout();
    
            cueLabel = new Label();
            cueLabel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            cueLabel.AutoSize = true;
            cueLabel.Text = "Enter your name";
            Controls.Add(cueLabel);
            cueLabel.Location = new Point(Width - cueLabel.Width, 0);
    
            ResumeLayout(false);
            PerformLayout();
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用设置为白色的面板作为BackColor
      在面板控件中,在左侧插入一个TextBox,其中BorderStyle 设置为None
      在面板控件中,在右侧插入一个 Label,BackColor 设置为 Transparent,并将 Text 设置为“Firstname”。

      【讨论】:

      • 这绝对是一个简单的解决方案——谢谢!但实际上我一直在寻找可以作为整体使用的东西。就像您之前建议我的教程中的 Forms.TextBox 的子类:link
      【解决方案4】:

      只需创建一个新的文本框类

       public class MyTextBox : TextBox
          {
      
              public MyTextBox()
              {
                  SetStyle(ControlStyles.UserPaint, true);
              }
      
           protected override void OnTextChanged(EventArgs e)
          {
              base.OnTextChanged(e);
              this.Invalidate();
          }
      
              protected override void OnPaint(PaintEventArgs e)
              {
      
       e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new System.Drawing.RectangleF(0, 0, this.Width , this.Height ), System.Drawing.StringFormat.GenericDefault);
      
      
                  e.Graphics.DrawString("Lloyd", this.Font, new SolidBrush(Color.Red), new System.Drawing.RectangleF(0, 0, 100, 100), System.Drawing.StringFormat.GenericTypographic);
                  base.OnPaint(e);
              }
          }
      

      对 Draw String 参数进行适当的更改

      【讨论】:

      • 虽然这是一个非常简单的解决方案,但它非常有问题...一旦选择文本或触发双击等,标题就会消失。还有光标和最后一个字母之间的空间不是恒定的。它随着字母的数量而充分增长。
      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 2018-11-06
      • 2015-05-23
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多