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;
}
}
}
查看全文
相关阅读:
Acwing 164 可达性统计 (拓扑排序+bitset)
STL-bitset的基本用法
Acwing 115 给树染色 (贪心)
Acwing 112 雷达设备 (贪心)
Acwing 110 畜栏预定 (贪心+stl)
Acwing 110 防晒 (贪心算法)
Acwing 七夕祭 (前缀和+中位数+思维)
Acwing 103 电影 (map)
USACO 最佳牛围栏 (二分答案+前缀和+双指针)
Acwing 101 最高的牛 (差分数组)
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
RAM river auto meterial使用教程
SimpleLOD
SubsurfaceScattering 自发光shader
RimAlhpa 透明shader
Unity粒子路径动画
Mega-Fiers Unity顶点变形动画插件
unity-De-Lighter和Agisoft De-Lighter免费去光影软件
Unity高清晰渲染管线HDRP入门指南
Unity高清渲染管线HDRP
Unity积雪shader
热门文章
快时钟域到慢时钟域
有符号位和无符号为的加法运算
基于FPGA的千兆以太网的实现
基于FPGA的任意分频实现
自动售货机问题
cyclone 芯片内部结构
Moore型状态机和Mealy型状态机
基于FPGA的DDS信号发生器的设计与实现
如何用Matlab处理.wfm格式的数据
基于FPGA的百兆以太网通信
Copyright © 2011-2022 走看看