C# listbox hover function

776 views Asked by At

I have a listbox and I was wondering how I can add a hover function to it to tell weather a person has passed or failed a unit. Here is my code:

form1

public partial class Form1 : Form
{
    int counter;
    int placeHolder;

    //create list

    List<Student> listStudent = new List<Student>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Unit unitnetworking = new Unit("Networking", 6);
        Unit unitsoftwaredev = new Unit("Software Development", 6);
        Unit unitproject = new Unit("Project", 12);
        Unit unitdatabases = new Unit("Databases", 6);
        Unit unittheory = new Unit("Information Theory", 6);
        Unit unitsystemsupport = new Unit("PC System Support", 6);

        Course programming = new Course("Programming", 2, "Abbey Park", unitsoftwaredev, unitdatabases, unittheory, unitproject);
        Course networking = new Course("Networking", 2, "Freeman's Park", unitnetworking, unitsystemsupport, unittheory, unitproject);
        Course sandwichNetworking = new Sandwich("Sandwich Networking", 2, "Freeman's Park Campus", "", 1, unitnetworking, unitsystemsupport, unittheory, unitproject);

        listStudent.Add(new Student("Grant Rigby", 567846, 2015, programming, false, false, false, false));
        listStudent.Add(new Student("Norman Beige", 531453, 2015, programming, false, false, false, false));
        listStudent.Add(new Student("John Lennon", 678347, 2014, networking, false, false, false, false));
        listStudent.Add(new Student("Kim Richards", 689453, 2015, programming, false, false, false, false));
        listStudent.Add(new Student("Paul Mcartney", 456843, 2014, networking, false, false, false, false));
        listStudent.Add(new Student("Richard Starkey", 678544, 2014, networking, false, false, false, false));
        listStudent.Add(new Student("Alan Sugar", 673359, 2015, networking, false, false, false, false));
        listStudent.Add(new Student("George Harrison", 534281, 2014, sandwichNetworking, false, false, false, false));
        listStudent.Add(new Student("Markus Persson", 366539, 2015, sandwichNetworking, false, false, false, false));
        listStudent.Add(new Student("Gabe Newell", 694534, 2014, programming, false, false, false, false));   

        placeHolder = 0;
        counter = 1;

        DisplayList();
    }

    public void DisplayList()
    {
        listBoxStudent.Items.Clear();
        listBoxStudent.Items.Add("Name: " + listStudent[placeHolder].Name);
        listBoxStudent.Items.Add("ID: " + listStudent[placeHolder].Id);
        listBoxStudent.Items.Add("Year: " + listStudent[placeHolder].Year);
        listBoxStudent.Items.Add("Course: " + listStudent[placeHolder].Course.Name);
        listBoxStudent.Items.Add("Unit 1: " + listStudent[placeHolder].Course.Unit0.Name);
        listBoxStudent.Items.Add("Unit 2: " + listStudent[placeHolder].Course.Unit1.Name);
        listBoxStudent.Items.Add("Unit 3: " + listStudent[placeHolder].Course.Unit2.Name);
        listBoxStudent.Items.Add("Unit 4: " + listStudent[placeHolder].Course.Unit3.Name);
    }

    //controls

    private void buttonNext_Click(object sender, EventArgs e)
    {
        if (counter == listStudent.Count)
        {
            placeHolder = 0;
            DisplayList();
            counter = 1;
        }
        else
        {
            placeHolder++;
            DisplayList();
            counter++;
        }

    }

    private void buttonPrevious_Click(object sender, EventArgs e)
    {
        if (counter == 1)
        {
            placeHolder = listStudent.Count - 1;
            DisplayList();
            counter = listStudent.Count;
        }
        else
        {
            placeHolder--;
            DisplayList();
            counter--;
        }

    }

    private void listBoxStudent_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

And this is my student class with the variables for passing or failing a unit

class Student
{
    private string name;
    private int id;
    private int year;
    private Course course;
    private bool passedUnit0;
    private bool passedUnit1;
    private bool passedUnit2;
    private bool passedUnit3;

    public Student(string name, int id, int year, Course course, bool passedUnit0, 
                   bool passedUnit1,  bool passedUnit2, bool passedUnit3)
    {
        this.name = name;
        this.id = id;
        this.year = year;
        this.course = course;
        this.passedUnit0 = passedUnit0;
        this.passedUnit1 = passedUnit1;
        this.passedUnit2 = passedUnit2;
        this.passedUnit3 = passedUnit3;
    }

    public string Name
    {
        get { return name; }

        set { name = value; }
    }

    public int Id
    {
        get { return id; }

        set { id = value; }
    }

    public int Year
    {
        get { return year; }

        set { year = value; }
    }

    public Course Course
    {
        get { return course; }

        set { course = value; }
    }

    public bool PassedUnit0
    {
        get { return passedUnit0; }

        set { passedUnit0 = value; }
    }

    public bool PassedUnit1
    {
        get { return passedUnit1; }

        set { passedUnit1 = value; }
    }

    public bool PassedUnit2
    {
        get { return passedUnit2; }

        set { passedUnit2 = value; }
    }

    public bool PassedUnit3
    {
        get { return passedUnit3; }

        set { passedUnit3 = value; }
    }

}

}

1

There are 1 answers

3
TaW On BEST ANSWER

You can use the MouseHover event but it is only called once until you leave the Control and reenter.

The MouseMove will respond directly, maybe with code like this:

private void listBoxStudent_MouseMove(object sender, MouseEventArgs e)
{
    showItemData(e.Location);
}

ToolTip tt = new ToolTip();

void showItemData()
{

    Point point = listBoxStudent.PointToClient(Cursor.Position);
    int index = listBoxStudent.IndexFromPoint(point);
    if (index <= 0) return;

    //Do your thing with the item:
    tt.Show(listBoxStudent.Items[index].ToString(), listBoxStudent, pt);
    Console.WritLine(  listBoxStudent.Items[index].ToString()  );

}

If you want a delay you could use a Timer..

BTW: I don't see a ToString() method in your Student class; this would help display it when you add the objects to the ListBox.Items directly..! Then you can cast an Item to Student and access all data in the Student object! Also I wonder: Do you really display totally different things in each item? You can do that, but wouldn't you see the passed values this way anyway?? Also consider using a ListView which can display several columns!