Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

DFL ToolTips and ListBox having Object entries

Part of DflCategory

Description

ListBox comprised of objects (rather than text strings). When onHover handler reached, the selected object from the ListBox is determined and its toString confirms which entry has the mouse over it.

Example

import std.string;

import dfl.all;

class Birthday: Object
{
  private int _year, _month, _day;

  this(int y, int m, int d) {
    _year = y;
    _month = m;
    _day = d;
  }
  char[] toString() {
    return (monthNames[_month] 
            ~ " " ~ std.string.toString(_day)
            ~ "," ~ std.string.toString(_year));
  }
  const char[][] monthNames = ["", "January", "February", "March", 
          "April", "May", "June", "July", "August", "September", 
          "October", "November", "December"];
}


class MainForm: Form 
{ 
  ToolTip _ttip; 
  ListBox _myList; 
   
  this() 
  { 
    if(!Control.supportsMouseTracking) {
      MessageBox.show("Please install Internet Explorer 5.5 or higher."); 
    }
    size = Size(350, 200);
    _ttip = new ToolTip; 
    
    _myList = new ListBox; 
    _myList.items.add(new Birthday(1776,  7,  4)); 
    _myList.items.add(new Birthday(1926,  5, 16)); 
    _myList.items.add(new Birthday(1927, 12, 31)); 
    _myList.items.add(new Birthday(1951,  5, 19)); 
    _myList.items.add(new Birthday(1952, 10, 18)); 
    _myList.items.add(new Birthday(1952,  7, 24)); 
    _myList.items.add(new Birthday(1967,  2, 16)); 
    _myList.items.add(new Birthday(1977, 11, 14)); 
    _myList.parent = this; 
    _myList.mouseHover ~= &myList_hover; 
  } 
   
  void myList_hover(Object sender, MouseEventArgs ea) 
  { 
    int index = _myList.indexFromPoint(ea.x, ea.y); 
    if(index != -1) {
      Object o = _myList.items.opIndex(index);
      Birthday bd = cast(Birthday)o;
      text = "Hovering over item: " ~ bd.toString(); 
      _ttip.setToolTip(_myList, "Item: " ~ bd.toString()); 
      _ttip.active = true; // Show the tip now. 
    } 
    else { 
      _ttip.setToolTip(_myList, null); 
    } 
  } 
}

void main()
{
  Application.run(new MainForm);
}

Update

In order to compile this code with more recent DFL versions, you'll need to change "MessageBox.show" to "msgBox".

Source

Link http://www.dsource.org/tutorials/index.php?show_example=139
Posted by Lynn
Date/Time Fri Dec 31, 2004 12:59 pm