C#中TextBox的横线样式及占位提示详解
作者:Csharp小记 时间:2023-05-17 10:33:27
文章描述
可能我标题描述不太准确,所以还是要稍微解释下:横线样式就是将TextBox以一条底横线的形式展示在页面,占位提示就是Web的Placeholder属性,即在输入框没有内容的时候进行一个输入提示。其实以上两个功能,无论是在Web还是WPF中,实现起来都比较简单,Winform反而比较困难。但是值得庆幸的是,在.Net Core Winform中,Placeholder已经内置进去了,可以直接使用。
为了方便使用,以下代码中还是使用自定义控件来实现这两个功能,只要设置属性即可看到效果。
开发环境
.NET Framework版本:4.5
开发工具
Visual Studio 2013
实现代码
public partial class TextBoxP : TextBox
{
private const int EM_SETCUEBANNER = 0x1501;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);
Panel panel = new Panel();
public TextBoxP()
{
InitializeComponent();
this.BorderStyle = BorderStyle.FixedSingle;
this.Font = new Font("宋体", 12f);
}
private string _Placeholder;
[Browsable(true)]
[Description("设置提示信息")]
public string Placeholder
{
get
{
return _Placeholder;
}
set
{
_Placeholder = value;
SendMessage(Handle, EM_SETCUEBANNER, 0, _Placeholder);
}
}
private bool _IsLineStyle;
[Browsable(true)]
[Description("设置以横线样式显示")]
public bool IsLineStyle
{
get { return _IsLineStyle; }
set
{
_IsLineStyle = value;
SetLineStyle();
}
}
private void SetLineStyle()
{
if (_IsLineStyle && !this.Controls.Contains(panel))
{
this.BorderStyle = BorderStyle.None;
this.SuspendLayout();
panel.Height = 1;
panel.Width = this.Width;
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Location = new Point(0, this.Height - 1);
this.Controls.Add(panel);
this.ResumeLayout();
this.PerformLayout();
this.SizeChanged += TextBoxP_SizeChanged;
this.LocationChanged += TextBoxP_LocationChanged;
}
else if (!_IsLineStyle)
{
if (this.Controls.Contains(panel))
{
this.Controls.Remove(panel);
}
this.BorderStyle = BorderStyle.FixedSingle;
this.SizeChanged -= TextBoxP_SizeChanged;
this.LocationChanged -= TextBoxP_LocationChanged;
}
if (!string.IsNullOrWhiteSpace(_Placeholder))
{
SendMessage(Handle, EM_SETCUEBANNER, 0, _Placeholder);
}
}
void TextBoxP_SizeChanged(object sender, EventArgs e)
{
panel.Width = this.Width;
}
void TextBoxP_LocationChanged(object sender, EventArgs e)
{
panel.Location = new Point(0, this.Height - 1);
}
}
private void button1_Click(object sender, EventArgs e)
{
textBoxP1.IsLineStyle = !textBoxP1.IsLineStyle;
textBoxP1.BackColor = textBoxP1.IsLineStyle ? SystemColors.Control : Color.White;
textBoxP2.IsLineStyle = !textBoxP2.IsLineStyle;
textBoxP2.BackColor = textBoxP2.IsLineStyle ? SystemColors.Control : Color.White;
}
实现效果
代码解析:Placeholder功能是使用Win APi做的,不得不说,这个方式的确是简单。一开始是想着可以用字体颜色以及对应的事件做到,但是效果不太完美,因为用这种方式说到底还是对Text属性的操作,最后获取的时候还是会有问题,即便经过判断过滤之后,仍然感觉不太好用,最重要的是:麻烦!
然后就是横线样式显示,这里是使用增加一个Panel控件来实现,其实我一直觉得处理自定义控件的话,将样式处理放在Paint事件中处理会比较完美,但是TextBox的Paint事件,有点难用。。。所以还是感觉这种方式简单、有效!
来源:https://mp.weixin.qq.com/s/lPRFn-yXylySxF6y5unfxw
标签:C#,TextBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用mybatis-plus-generator进行代码自动生成的方法
2021-09-03 00:50:18
![](https://img.aspxhome.com/file/2023/6/127226_0s.png)
IP查询系统的异步回调案例
2023-11-10 18:22:24
C#图片查看器实现方法
2021-06-17 16:50:03
![](https://img.aspxhome.com/file/2023/1/72491_0s.gif)
JAVA发送HTTP请求的四种方式总结
2023-08-23 20:27:23
Java 生产者/消费者问题实例详解
2023-02-26 01:23:36
![](https://img.aspxhome.com/file/2023/3/62053_0s.gif)
C# JWT权限验证的实现
2022-11-24 00:57:13
Java树形结构数据生成导出excel文件方法记录
2021-08-20 05:19:18
![](https://img.aspxhome.com/file/2023/9/74899_0s.jpg)
详解IDEA多module项目maven依赖的一些说明
2023-07-21 06:09:05
![](https://img.aspxhome.com/file/2023/4/67974_0s.png)
Java系统变量参数获取设置System.getProperties()的方法
2021-07-22 04:52:47
Android自定义View绘图实现拖影动画
2023-04-16 00:06:38
![](https://img.aspxhome.com/file/2023/7/139097_0s.gif)
Android View类与SurfaceView类详解
2022-07-17 14:49:24
Spring中XML schema扩展机制的深入讲解
2022-06-29 07:44:15
![](https://img.aspxhome.com/file/2023/4/77384_0s.png)
springboot使用mybatis一对多的关联查询问题记录
2023-05-25 14:31:03
![](https://img.aspxhome.com/file/2023/0/72650_0s.png)
Java使用Math.random()结合蒙特卡洛方法计算pi值示例
2023-05-10 10:43:13
![](https://img.aspxhome.com/file/2023/6/60606_0s.jpg)
Java 集合的Contains和Remove方法
2022-06-17 05:29:10
![](https://img.aspxhome.com/file/2023/6/132126_0s.jpg)
Java String 字符串常量池解析
2023-01-08 19:44:05
![](https://img.aspxhome.com/file/2023/6/66936_0s.png)
C#语音识别用法实例
2021-09-11 01:16:47
简单介绍三层架构工作原理
2022-10-01 20:28:25
![](https://img.aspxhome.com/file/2023/7/98257_0s.jpg)
C#回收机制之资源回收托管
2022-03-04 13:18:20
在IntelliJ IDEA中为自己设计的类库生成JavaDoc的方法示例
2023-11-25 09:49:02