惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - AndyHai

用C#实现的黑客帝国中的字符雨特效 谁动了我的构造函数? 一个PCM音频转换与混音的示例 关心则乱 让ASPX和ASMX脱离IIS运行的例子(ASP.NET宿主程序) SQL 中如何对纪录进行拆分 NAT类型检测方法(转载) 在.NET中探测U盘的插入/拔出 用WebService实现中国移动的Provision反向接口 一个动态加载/卸载DLL的例子 用ASP.NET调用Tuxedo Tuxedo 搞定! 用Multi-Media Library实现的波形音频录制与播放 用Multi-Media Library制作流式音频播放器 研究如何用Multi-Media Library播放波形数据 又多一道面试题 面试 RTP协议
带有空值提示的TextBox
AndyHai · 2007-09-19 · via 博客园 - AndyHai

  看到很多网站上的输入框都有空值提示,即:输入框中没有内容且没有焦点时,输入框中显示的是提示文字;如果有内容或者拥有焦点,则正常显示。我觉得这东西很有意思,在某些应用中,可以减少界面排版上的麻烦,可惜WinForm中的TextBox没有此功能,于是自己做了一个,效果嘛,还算满意的:)

    public class TEditBox : System.Windows.Forms.TextBox
    
{
        
public TEditBox()
            : 
base()
        
{
        }


        
private string _Caption = "Please Input";
        
/// <summary>
        
/// 提示内容
        
/// </summary>

        public string Caption
        
{
            
get
            
{
                
return _Caption;
            }

            
set
            
{
                _Caption 
= value;
                
if (!DesignMode && Nothing)
                    
base.Text = value;
            }

        }


        
private System.Drawing.Color _CaptionColor = System.Drawing.SystemColors.Control;
        
/// <summary>
        
/// 提示字体的颜色
        
/// </summary>

        public System.Drawing.Color CaptionColor
        
{
            
get
            
{
                
return _CaptionColor;
            }

            
set
            
{
                
if (!DesignMode && !Focused && Nothing)
                    
base.ForeColor = value;
                
                _CaptionColor 
= value;
            }

        }


        
private System.Drawing.Color _ForeColor = System.Drawing.SystemColors.WindowText;

        
public new System.Drawing.Color ForeColor
        
{
            
get
            
{
                
return base.ForeColor;
            }

            
set
            
{
                
if (!this.DesignMode)
                
{
                    
if (Focused || !Nothing)
                        
base.ForeColor = value;
                    _ForeColor 
= value;
                }

                
else
                    
base.ForeColor = value;
            }

        }


        
private bool HaveNothing = true;
        
private bool Nothing
        
{
            
get return HaveNothing || this.Text == ""; }
        }


        
public new string Text
        
{
            
get
            
{
                
if (!Focused && HaveNothing)
                    
return "";
                
else
                    
return base.Text;
            }

            
set
            
{
                
base.Text = value;
                HaveNothing 
= value == "";

                
if (Nothing && !Focused)
                
{
                    
base.Text = _Caption;
                    
base.ForeColor = _CaptionColor;
                }

                
else
                
{
                    
if (base.ForeColor != _ForeColor)
                        
base.ForeColor = _ForeColor;
                }

            }

        }


        
protected override void OnGotFocus(EventArgs e)
        
{
            
if (Nothing)
                
base.Text = "";

            
if (base.ForeColor != _ForeColor)
                
base.ForeColor = _ForeColor;

            
base.OnGotFocus(e);
        }


        
protected override void OnLostFocus(EventArgs e)
        
{
            HaveNothing 
= base.Text == "";

            
if (Nothing)
            
{
                
base.Text = _Caption;
                
base.ForeColor = _CaptionColor;
            }

            
else
            
{
                
if (base.ForeColor != _ForeColor)
                    
base.ForeColor = _ForeColor;
            }

            
base.OnLostFocus(e);
        }


        
public new void Clear()
        
{
            
this.Text = "";
        }
        
    }

属性Caption和CaptionColor是新增的,用来存储提示文字和提示文字的颜色