C#列表框悬停功能

问题描述 投票:0回答:1

我有一个列表框,我想知道如何向其中添加一个悬停功能,以告诉天气某人已经通过或失败了一个单元。这是我的代码:

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)
    {

    }
}

}

这是我的学生班,带有用于通过或不通过单元的变量

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; }
    }

}

}

c# listbox mousehover
1个回答
2
投票

您可以使用MouseHover事件,但只有离开Control并重新输入后,它才被调用一次。

MouseMove将直接响应,也许使用如下代码:

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()  );

}

如果需要延迟,可以使用Timer

BTW:在您的ToString()类中没有看到Student方法;当您直接将对象添加到ListBox.Items时,这将有助于显示它。然后,您可以[[cast ItemStudent,并访问Student对象中的所有数据!我也想知道:在每个项目中您真的显示完全不同的东西吗?您可以这样做,但是无论如何您不会看到传递的值吗?还可以考虑使用可以显示多列的ListView

© www.soinside.com 2019 - 2024. All rights reserved.