重写TextBox,让它显示成一条线,请给出例子。

解决方案 »

  1.   

    why don't you use Label?
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace JcsControlLibrary
    {
        public partial class JcsLineTextBox : TextBox 
        {
            public JcsLineTextBox()
            {
                InitializeComponent();
                this.Width = 100;
                this.BorderStyle = BorderStyle.None;
            }
            private Color _linecolor = Color.Black ;
            /// <summary>
            /// 线条颜色
            /// </summary>
            public Color LineColor
            {
                get
                {
                    return this._linecolor;
                }
                set
                {
                    this._linecolor = value;
                    this.Invalidate();
                }
            }
            private const int WM_PAINT = 0xF;
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_PAINT)
                {
                    DrawLine();
                }
            }
            private void DrawLine()
            {
                Graphics g = this.CreateGraphics();
                using(Pen p = new Pen(this._linecolor ))
                {
                    g.DrawLine(p,0,this.Height -1 ,this.Width ,this.Height -1);
                }
            }
        }
    }
    顺便顶一下
      

  3.   

    不用重写, 搞成没边框的, 下面加个做成线的label
      

  4.   

    WndProc(ref Message m)有什么用
    ,什么时候需要重写
      

  5.   

    为什么重写OnPaint不行呢?
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;namespace MyTextBox
    {
         public class LineTextBox:TextBox
        {
             public LineTextBox()
                
             {             this.BorderStyle = BorderStyle.None;
             }
             protected override void OnPaint(PaintEventArgs e)
             {
                 e.Graphics.DrawLine(Pens.Black, new Point(0, this.Height - 1), new Point(this.Width, this.Height - 1));
                 base.OnPaint(e);
             }
        }
    }
      

  6.   

    4楼大侠,protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_PAINT)
                {
                    DrawLine();
                }
            }
    这块代码搞不懂,以前没有用过这个WndProc方法
      

  7.   

    显示成线            textBox1.BorderStyle = BorderStyle.None;
                textBox1.BackColor = this.BackColor;
                textBox1.Refresh();
                Graphics gph = textBox1.CreateGraphics();
                Rectangle Rec = textBox1.ClientRectangle;
                gph.DrawLine(new Pen(Color.Black), new Point(0, Rec.Height-1), new Point(Rec.Width, Rec.Height-1));恢复textBox默认格式            textBox1.BorderStyle = BorderStyle.Fixed3D;
                textBox1.BackColor =SystemColors.Window;
                textBox1.Text = "";
      

  8.   

    楼主其实不用重写,只要定义好他的CSS样式就行了!
    例如:
    .txt
    {


    border-width: 1px;
    border-style: none none solid none;
    height:20px;
    text-align:left;
    }
      

  9.   

    textbox 引用的时候这样<asp:TextBox ID="tbUserName" runat="server" CssClass="txt" Width="200px"></asp:TextBox>
      

  10.   

    第一步:把把所有textbox的borderstyle 设置成none;
    第二步:添加From_Paint事件;
    第三步:看代码;
     private void From_Paint(object sender, PaintEventArgs e)
            {
               Graphics g = e.Graphics;
               Pen pen = new Pen(Color.Black, 1.0f);
               foreach (Control ctr in this.Controls)
               {               if (ctr is TextBox)
                   {
                       
                       g.DrawLine(pen, ctr.Location.X, ctr.Location.Y + 14, ctr.Location.X + ctr.Width, ctr.Location.Y + 14);
                   }
               }
               pen.Dispose();        }