zoukankan      html  css  js  c++  java
  • .Net标准控件与自定义控件(2) ToolTipButton

    继昨天的NoCopyTextBox(http://bearocean.cnblogs.com/archive/2006/04/26/385413.html
    以后,客户又提出了新的要求:

                客户需要一套机票定位系统,用一个小方框来代表一个机位,点击以后弹出对话框,将由系统的操作人员填写预定信息。
                所以我们考虑用Button来实现这个功能。
                
                但是客户有一个附加要求,就是当鼠标移动到机位的方框上是,尽管不点击也能够显示定位信息:           
                但是具我所知默认的Button并没有这个功能。(如果有就白忙了)
                所以要自己做一个新的Button控件。

                最好是这样,MyButton myButton =new Button();
                                          myButton.Title ="This is a test";

                这样就能自动的显示Title的内容。
                之所以把这个属性取名为Title是因为这个跟Html的Title如此相象。

                其后想到了.Net 提供的ToolTip
                于是利用这个控件,做了一个ToolTipButton.
                原代码如下:

                   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;


    namespace Uestc_15_UI
    {
        
    /// <summary>
        
    /// HelperButton 的摘要说明。
        
    /// </summary>

        public class HelpTipButton :System.Windows.Forms.Button
        
    {
            
    private System.Windows.Forms.ToolTip HelpTip;
            
    private System.ComponentModel.IContainer components;

            
    public delegate void OnTitleChanged();
            
    public event OnTitleChanged TitleChaneged;

            
    private string strTitle ="";
            
    public string Title
            
    {
                
    get
                
    {
                    
    return this.strTitle;
                }

                
    set
                
    {
                    
    this.strTitle =value;
                    
    if(this.TitleChaneged !=null)
                    
    {
                        
    this.TitleChaneged();
                    }

                }

            }

        
            
    public HelpTipButton()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
                this.components = new System.ComponentModel.Container();
                
    this.HelpTip = new System.Windows.Forms.ToolTip(this.components);
                
    this.TitleChaneged +=new OnTitleChanged(HelpTipButton_TitleChaneged);
            }


            
    private void InitializeComponent()
            
    {
                
    // 
                
    // HelpTipButton
                
    // 

            }


            
    private void HelpTipButton_TitleChaneged()
            
    {
                
    if(this.strTitle !="")
                
    {
                    
    this.HelpTip.SetToolTip(this,this.strTitle);
                }

            }

        }

    }

    其实实现很简单,只是继承自Button,然后添加了一个私有成员ToolTip,和一个属性string Title
    并定义一个事件去检测Title.一旦Title变化,就重新利用HelpTip.SetToolTip(this,this.strTitle)将字符串信息
    设置到ToolTip上。
    同时SetToopTip将ToolTip与Button绑定。

    最后,只要重新作一个Surface就差不多了。
    其实真正的实现可能还要复杂一点,如果要实现上述定机位功能,最好把string Title换成一个实际的类。
    初始化ToolTipButton时是将一个ClientInfo实例附给他。而不是将一串名为Title的字符串赋给它。

  • 相关阅读:
    Highcharts之饼图
    设计模式学习之原型模式
    jQuery学习之结构解析
    JS学习之闭包的理解
    JS学习之prototype属性
    JS学习之事件冒泡
    Mybatis学习之JDBC缺陷
    Spring学习之Aop的基本概念
    Struts学习之值栈的理解
    Struts学习之自定义结果集
  • 原文地址:https://www.cnblogs.com/BearOcean/p/386826.html
Copyright © 2011-2022 走看看