带有空值提示的TextBox

  看到很多网站上的输入框都有空值提示,即:输入框中没有内容且没有焦点时,输入框中显示的是提示文字;如果有内容或者拥有焦点,则正常显示。我觉得这东西很有意思,在某些应用中,可以减少界面排版上的麻烦,可惜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是新增的,用来存储提示文字和提示文字的颜色
posted @ 2007-09-19 14:04 AndyHai 阅读(305) 评论(2)  编辑 收藏 网摘 所属分类: Others

  回复  引用  查看    
#1楼 [楼主]2007-09-19 14:06 | AndyHai      
哎~~本月总算是有一篇内容了,快忙晕咯~~
  回复  引用    
#2楼 2007-09-19 17:03 | 缘来如此 [未注册用户]
呵呵,收藏啦

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-09-22 09:38 编辑过
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接:


 
QQ: 2369537