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;
}
}
}
查看全文
相关阅读:
第十四周学习进度
二阶段冲刺(七)
二阶段冲刺(六)
二阶段冲刺(五)
二阶段冲刺(四)
二阶段冲刺(三)
二阶段冲刺(二)
二阶段冲刺(一)
第十三周学习进度
linux初级学习笔记二:linux操作系统及常用命令,文件的创建与删除和命名规则,命令行展开以及linux中部分目录的作用!(视频序号:02_3)
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
JUnit 单元测试 配置
JDBC操作Oracle数据库——实际操作过程中的小总结
Hibernate——基础及XML配置
JDBC操作Oracle数据库
Web开发——Tomcat的配置
学习笔记(二):javascript之dom操作
学习笔记(一):offset
js无缝滚动原理及详解[转自刹那芳华]
使用OpenOffice实现文档预览
pytorch model(torch.nn.Module)笔记
热门文章
python 一些工具库笔记
PyTorch 训练前对数据加载、预处理
优化Ubuntu电脑 开机时间长越用越慢
python图片处理(去重,缩放)
Ubuntu电脑死机重启
在浏览器中输入url到页面显示出来的过程发生了什么?
vue dom延迟更新?
js 笔记
charles 笔记
找到“水王”
Copyright © 2011-2022 走看看