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;
}
}
}
查看全文
相关阅读:
在JAVA应用中远程提交MapReduce程序至Hadoop集群运行
ImpalaJDBCDriver 500051
在Linux上安装Python3
MySQL导入之mysqlimport
hadoop mapreduce的本地模式及yarn模式
MySQL之mysqldump的使用
MapReduce的本地运行模式(debug调试)
09 MySQL触发器
P3168 [CQOI2015]任务查询系统
P4124[CQOI]手机号码
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
7. Spring
6. Spring相关API
Spring配置文件基本要点总结
5. Spring配置文件
CSS visibility 属性
JS清除网页历史记录,屏蔽后退按钮
24. bootstrap组件#折叠菜单
JavaScript常用的正则表达式实例 -- 转载
【洛谷 2384】最短路
【UOJ 216】最小花费最短路
热门文章
【洛谷 1969】积木大赛
【洛谷 1164】小A点菜
【UOJ 457】括号匹配2
【UOJ 456】括号匹配
【洛谷 1880】石子合并
【洛谷 2782】友好城市
【UOJ 92】有向图的强连通分量
【洛谷 1455】搭配购买
Datax执行命令后控制台出现中文乱码
datax安装 及 springboot整合 datax
Copyright © 2011-2022 走看看