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;
}
}
}
查看全文
相关阅读:
Artifact Project3:war exploded: Error during artifact deployment. See server log for details.
Struts2的OGNL表达式语言
java空指针异常:java.lang.NullPointException
两天撸一个天气应用微信小程序
Graves of the Internet
JavaScript并非“按值传递”
js实现黑客帝国二进制雨
JavaScript“并非”一切皆对象
纯CSS打造银色MacBook Air(完整版)
element-ui中轮播图自适应图片高度
原文地址:https://www.cnblogs.com/wmz/p/1268875.html
最新文章
docker部署nacos
lombok 使用及技巧
脚本编写相关的Python库
Python基础语法
2020年终扯淡
忆往昔峥嵘岁月稠——OI回忆录
[LOJ6569] 仙人掌计数
[WC2019] 数树
自然数幂和
Min_25筛
热门文章
为什么要用消息队列
Java集合框架
Java泛型
JVM内存区域
idea构建spring源码阅读环境
Tomcat手动部署Web项目详细步骤
IntelliJ IDEA 2016.2 配置Tomcat 运行Web项目
github错误:fatal: remote origin already exists.
通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明。
URI is not registered ( Setting | Project Settings | Schemas and DTDs )
Copyright © 2011-2022 走看看