C# 自定义控件之TextBox

此篇来自网络,在此感谢原作者

问题:如何让TextBox显示只有下边的一条线?like this :

解决方案:自定义TextBox控件

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Test
{
    public class MyTextBox:TextBox
    {
        private bool m_underLine;

        public bool UnderLine
        {
            get { return m_underLine; }
            set
            {
                if (this.m_underLine != value)
                {
                    if (value)
                    {
                        this.BorderStyle = BorderStyle.None;
                    }
                    m_underLine = value;
                }
            }
        }
        protected override void WndProc(ref   Message m)
        {
            base.WndProc(ref   m);
            if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85)
            {
                if (this.BorderStyle == BorderStyle.None)
                {
                    if (m_underLine)
                    {
                        using (Graphics g = Graphics.FromHwnd(this.Handle))
                        {
                            g.DrawLine(SystemPens.ControlText, 0, this.Height - 1, this.Width - 1, this.Height - 1);
                        }
                    }
                }
            }
        }
    }

使用帮助:

1.把自定义的控件放在你要放的地方。ok

2.修改其属性:BorderStyle :None

UnderLine:true

3.同喜你。自定义控件成功!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值