zoukankan      html  css  js  c++  java
  • 列表框控件,每个项目都有工具提示

    Image 1 Introduction  本文描述了在Windows窗体列表框控件中显示项的工具提示的方法。内置的工具提示控件允许您为整个列表框显示单个工具提示。但是,如果您想为列表框中的每个项显示单独的工具提示,那么您就不走运了。幸运的是,自己模仿工具提示行为并不太难。在这里,我们将了解如何创建和使用一个名为ToolTipListBox的自定义列表框控件。 描述的代码 本文包含为每个项目创建具有不同工具提示的列表框所需的完整代码。我们将在下面几节中详细介绍这些代码。代码由以下部分组成: 添加ListBoxItem对象到ToolTipListBox控件ListBoxItem classIToolTipDisplayer接口ToolTipListBox类 向ToolTipListBox添加ListBoxItem对象 隐藏,复制Code

    // Create list of items
    ListBoxItem[] items = new ListBoxItem[]
    {
        new ListBoxItem("Apple",     "Malus pumila"),
        new ListBoxItem("Banana",    "Porcelia macrocarpa"),
        new ListBoxItem("Kiwi",      "Actinidia deliciosa"),
        new ListBoxItem("Papaya",    "Carica papaya"),
        new ListBoxItem("Mango",     "Mangifera indica"),
        new ListBoxItem("Tomato",    "Lycopersicon esculentum"),
        new ListBoxItem("Lychee",    "Litchi chinensis"),
        new ListBoxItem("Coconut",   "Cocos nucifera"),
        new ListBoxItem("Tangerine", "Citrus reticulata"),
        new ListBoxItem("Avocado",   "Persea americana"),
    };
     
    // Populate list box
    toolTipListBox.Items.AddRange(items);

    上面列出的代码创建了一个ListBoxItem对象数组,并将这些项添加到ToolTipListBox控件中。每个ListBoxItem的构造函数接受要在listbox和工具提示文本中显示的文本。 ListBoxItem类 隐藏,复制Code

    internal class ListBoxItem : IToolTipDisplayer
    {
        public string DisplayText { get; private set; }
        public string ToolTipText { get; private set; }
     
        // Constructor
        public ListBoxItem(string displayText, string toolTipText)
        {
            DisplayText = displayText;
            ToolTipText = toolTipText;
        }
     
        // Returns the display text of this item.
        public override string ToString()
        {
            return DisplayText;
        }
        // Returns the tooltip text of this item.
        public string GetToolTipText()
        {
            return ToolTipText;
        }
    }

    ListBoxItem是一个简单的类,用于表示列表框中的每一项。ToString()方法被Object覆盖,它返回列表框中显示的文本。GetToolTipText()方法返回为该项显示的工具提示文本。它实现了下面描述的IToolTipDisplayer接口。 IToolTipDisplayer接口 隐藏,复制Code

    /// Interface used by listbox items in ToolTipListBox.
    internal interface IToolTipDisplayer
    {
        string GetToolTipText();
    } 

    IToolTipDisplayer界面被ToolTipListBox用来获得列表框项的工具提示文本。该接口有一个方法,该方法返回要为该项显示的工具提示文本。添加到列表框中的任何对象都应该实现这个接口来显示工具提示文本。正如您在上面看到的,ListBoxItem类实现了IToolTipDisplayer。 ToolTipListBox类 现在我们将进入本文的核心部分,描述ToolTipListBox的实现。首先,我们声明类和一些成员变量: 隐藏,复制Code

    /// ListBox that displays item-specific tooltips.
    internal partial class ToolTipListBox : ListBox
    {
        // The item index that the mouse is currently over
        private int _currentItem;
     
        // A value indicating if the current item has been set
        private bool _currentItemSet;
     
        // A value indicating if a tooltip is currently being displayed
        private bool _toolTipDisplayed;
     
        // Timer that is used to wait for the mouse to hover over an item
        private Timer _toolTipDisplayTimer;
     
        // Tooltip control
        private ToolTip _toolTip;

    到目前为止都很简单。这个类扩展了内置的列表框控件并添加了工具提示功能。现在,让我们定义构造函数: 隐藏,复制Code

    public ToolTipListBox()
    {
        InitializeComponent();
     
        this.MouseMove += listBox_MouseMove;
        this.MouseLeave += listBox_MouseLeave;
                
        _currentItemSet = false;
        _toolTipDisplayed = false;
        _toolTipDisplayTimer = new Timer();
        _toolTip = new ToolTip();
     
        // Set the timer interval to the system time that it takes for a tooltip to appear
        _toolTipDisplayTimer.Interval = SystemInformation.MouseHoverTime;
        _toolTipDisplayTimer.Tick += _toolTipDisplayTimer_Tick;
    } 

    InitializeComponent()在创建新用户控件时由Visual Studio自动添加。我们为listbox的MouseMove和MouseLeave事件添加处理程序。然后我们初始化成员变量。然后我们设置计时器来在工具提示的标准系统时间之后触发Tick事件(systeminfo . mousehovertime)。现在,让我们定义listbox的MouseMove事件处理程序: 隐藏,收缩,复制Code

    private void listBox_MouseMove(object sender, MouseEventArgs e)
    {
        // Get the item that the mouse is currently over
        Point cursorPoint = Cursor.Position;
        cursorPoint = this.PointToClient(cursorPoint);
        int itemIndex = this.IndexFromPoint(cursorPoint);
     
        if (itemIndex == ListBox.NoMatches)
        {
            // Mouse is over empty space in the listbox so hide tooltip
            _toolTip.Hide(this);
            _currentItemSet = false;
            _toolTipDisplayed = false;
            _toolTipDisplayTimer.Stop();
        }
        else if (!_currentItemSet)
        {
            // Mouse is over a new item so start timer to display tooltip
            _currentItem = itemIndex;
            _currentItemSet = true;
            _toolTipDisplayTimer.Start();
        }
        else if (itemIndex != _currentItem)
        {
            // Mouse is over a different item so hide tooltip and restart timer
            _currentItem = itemIndex;
            _toolTipDisplayTimer.Stop();
            _toolTipDisplayTimer.Start();
            _toolTip.Hide(this);
            _toolTipDisplayed = false;
        }
    }

    每当鼠标在列表框上时(无论它是否移动),都会引发此事件。首先,我们确定鼠标结束的项索引。我们可以使用listbox的PointToClient()和IndexFromPoint()方法来获得这个值。现在,我们有几种情况取决于鼠标的索引结束: 隐藏,复制Code

    itemIndex == ListBox.NoMatches

    当鼠标停留在列表框的空白区域上时,这个条件就满足了。这意味着我们应该隐藏工具提示并停止工具提示显示计时器。 隐藏,复制Code

    !_currentItemSet

    当鼠标刚刚开始移动到列表框项上时,这个条件就满足了。在本例中,我们希望设置当前项并启动计时器以显示工具提示。 隐藏,复制Code

    itemIndex != _currentItem

    当鼠标移动到与当前设置的项不同的新项上时,就满足此条件。在这种情况下,我们应该将当前项设置为新项,隐藏当前显示的工具提示,并重新启动工具提示显示计时器。 隐藏,复制Code

    private void listBox_MouseLeave(object sender, EventArgs e)
    {
        // Mouse has left listbox so stop timer (tooltip is automatically hidden)
        _currentItemSet = false;
        _toolTipDisplayed = false;
        _toolTipDisplayTimer.Stop();
    } 

    当鼠标不在列表框的上方时引发MouseLeave事件。在本例中,我们希望重置当前项并停止工具提示显示计时器。 隐藏,复制Code

    void _toolTipDisplayTimer_Tick(object sender, EventArgs e)
    {
        // Display tooltip text since the mouse has hovered over an item
        if (!_toolTipDisplayed && _currentItem != ListBox.NoMatches && 
    				_currentItem < this.Items.Count)
        {
            IToolTipDisplayer toolTipDisplayer = this.Items[_currentItem] as IToolTipDisplayer;
            if (toolTipDisplayer != null)
            {
                _toolTip.SetToolTip(this, toolTipDisplayer.GetToolTipText());
                _toolTipDisplayed = true;
            }
        }
    } 

    定时器的滴答事件是在标准鼠标悬停时间经过后升起的。当这种情况发生时,我们知道鼠标已经在同一项上一段时间了,所以我们应该显示该项的工具提示。项目的工具提示文本是通过调用当前项目的GetToolTipText()方法获得的。当然,这只有在项目实现了IToolTipDisplayer界面时才能工作。 结论 就是这样!这里列出的完整代码可在附加的.zip文件中获得。该项目是使用Visual c# 2010 Express创建的。还有其他几种显示工具提示的方法,但我发现这种方法是干净的,因为它在自定义控件中隐藏了实现细节。ToolTipListBox控件可以添加到窗体中,就像添加任何内置控件一样,IToolTipDisplayer界面可以添加到添加到列表框中的任何现有对象中。 本文转载于:http://www.diyabc.com/frontweb/news329.html

  • 相关阅读:
    85--spring cloud (Ribbon-Eureka注册中心)
    85--spring cloud 入门(springcloud简介)
    84--spring cloud 入门(微服务注册中心介绍)
    83--spring cloud 入门(Eureka注册中心)
    82--JT项目20(订单模块实现/ThreadLocal本地线程变量/Quartz框架)
    81--JT项目19(商品购物车/详情/用户退出)
    80--JT项目18(Dubbo负载均衡/单点登录/注册业务)
    Ajax中post与get的区别
    Process
    Java实现CURL,与把字符串结果写到json文件
  • 原文地址:https://www.cnblogs.com/Dincat/p/13437560.html
Copyright © 2011-2022 走看看