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;
}
}
}
查看全文
相关阅读:
PHPStrom 设置终端字体大小
PostgreSQL 9 夸库查询
弹性布局
sql中的 where 、group by 和 having 用法解析
数据库面试中常问的几个问题
SQL中 UNION 和 UNION ALL 操作符小结
SQL里 inner JOIN、 left (OUTER) JOIN 、right (OUTER) JOIN、 full (OUTER) JOIN 之间的区别
MYSQL中LIMIT用法
Java集合框架小结
jQuery$.each循环遍历详解,各种取值对比,$.each遍历数组、对象、Dom元素、二维数组、双层循坏、类json数据等等
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
微信分享功能不可用+不能产生回调
GPS坐标互转:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图)
Adobe Flash 无法正常使用
火星坐标、百度坐标、WGS84坐标转换代码(JS)
微信服务器 IP 及长链接转短链接接口调用
高度已知,左右定宽,中间自适应三栏布局的五种写法
CSS仿艺龙首页鼠标移入图片放大
小小手风琴效果
webpack-dev-server
jQuery放大镜
热门文章
项目中用到的几个关于正则使用的例子
360度旋转相册
根据当前日期获取昨天和明天的日期
nginx tp6 通过composer加载验证码后显示404 解决方案
explode and implode
安装卸载Vue npm 以及 ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! vuecli3@0.1.0 dev: `vue-cli-service serve` npm ERR! Exit status 1
PostgreSQL 获取记录序号
MySQL 8.0.11 Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
MySQL 8.0.11 初始安装修改root密码
postgreSQl 夸库视图
Copyright © 2011-2022 走看看