C# 列表框悬停函数

本文关键字:函数 悬停 列表 | 更新日期: 2023-09-27 18:32:23

我有一个列表框,我想知道如何向它添加一个悬停功能来告诉天气一个人通过或失败了一个单位。这是我的代码:

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# 列表框悬停函数

您可以使用

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..

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