zoukankan
html css js c++ java
自定义控件提示“请输入...”的Textbox
结果:
代码:
Code
using
System;
using
System.Windows.Forms;
using
System.Diagnostics;
using
System.Drawing;
using
System.ComponentModel;
public
partial
class
TextBoxWithPrompt : TextBox
{
protected
override
void
OnGotFocus(EventArgs e)
{
base
.OnGotFocus(e);
if
(
this
.UsePrompt)
{
this
.UsePrompt
=
false
;
this
.Text
=
string
.Empty;
}
}
protected
override
void
OnLostFocus(EventArgs e)
{
if
(
this
.TextLength
==
0
||
this
.Text
==
this
.TextPrompt)
{
this
.UsePrompt
=
true
;
this
.Text
=
this
.TextPrompt;
}
base
.OnLostFocus(e);
}
private
string
textPrompt
=
"
请输入
"
;
public
string
TextPrompt
{
get
{
return
textPrompt; }
set
{
textPrompt
=
value;
if
(
this
.UsePrompt
&&
!
string
.IsNullOrEmpty(
this
.textPrompt))
{
this
.Text
=
value;
}
}
}
private
bool
usePrompt;
private
bool
UsePrompt
{
get
{
return
usePrompt; }
set
{
usePrompt
=
value;
if
(usePrompt)
{
this
.Font
=
new
Font(
this
.Font.Name,
this
.Font.Size, FontStyle.Italic);
this
.ForeColor
=
Color.Gray;
}
else
{
//
TODO don't hardcode the user given values.
this
.Font
=
new
Font(
this
.Font.Name,
this
.Font.Size, FontStyle.Regular);
this
.ForeColor
=
Color.Black;
}
}
}
protected
override
void
OnParentChanged(EventArgs e)
{
if
(
string
.IsNullOrEmpty(
this
.Text))
{
this
.UsePrompt
=
true
;
this
.Text
=
this
.TextPrompt;
}
base
.OnParentChanged(e);
}
public
override
string
Text
{
get
{
if
(
this
.UsePrompt)
{
return
string
.Empty;
}
return
base
.Text;
}
set
{
if
(
this
.UsePrompt
&&
(
!
string
.IsNullOrEmpty(value)
&&
value
!=
this
.TextPrompt))
{
this
.UsePrompt
=
false
;
}
if
(
string
.IsNullOrEmpty(value)
&&
!
this
.Focused
&&
!
string
.IsNullOrEmpty(
this
.textPrompt))
{
this
.UsePrompt
=
true
;
this
.Text
=
this
.TextPrompt;
return
;
}
base
.Text
=
value;
}
}
}
查看全文
相关阅读:
TreeSet类的排序问题
TreeSet()详解
css中vertical-align垂直居中的认识
CSS3 float深入理解浮动资料整理
层叠顺序与层叠上下文
jquery.zclip轻量级复制失效问题
文章转载利用border、transparent实现微风
转载利用线性渐变实现晴天、多云特效
转载利用伪元素单个颜色实现 hover 和 active 时的明暗变化效果
MUI-最接近原生App体验的前端框架
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
让javascript加载速度倍增的方法(解决JS加载速度慢的问题)
JSTL截取字符串以及格式化时间
drds 广播表的创建以及使用
sql ROUND() 函数三个参数的含义
sql server 查询性能最差的sql语句
sql server 查看数据库编码格式
sql 修改字段长度以及其他属性
大数据量的csv文件如何导入到 sql 数据库
sql set xact_abort on 用例
Union与union all区别
热门文章
HTTP 错误 404.3
sql查询所有表以及表名的模糊查询
数据建模
JAVA反射系列之Field,java.lang.reflect.Field使用获取方法
Mysql联合查询UNION和UNION ALL的使用介绍
经典:十步完全理解 SQL
设计模式
入门基础
冒泡排序高逼格
好博客分享 go需要运行容器? 不需要
Copyright © 2011-2022 走看看