C#列表框悬停函数
我有一个列表框,我想知道如何在其中添加一个悬停函数来告诉一个人是否通过了某个单元。这是我的密码:C#列表框悬停函数,c#,listbox,mousehover,C#,Listbox,Mousehover,我有一个列表框,我想知道如何在其中添加一个悬停函数来告诉一个人是否通过了某个单元。这是我的密码: form1 public partial class Form1 : Form { int counter; int placeHolder; //create list List<Student> listStudent = new List<Student>(); public Form1() { I
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)
{
}
}
}您可以使用
鼠标悬停
事件,但在您离开控件并重新输入之前,它只会被调用一次
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() );
}
private void listBoxStudent\u MouseMove(对象发送方,MouseEventArgs e)
{
showItemData(如位置);
}
工具提示tt=新工具提示();
void showItemData()
{
Point Point=listBoxStudent.PointToClient(游标位置);
int index=listBoxStudent.IndexFromPoint(点);
if(index)你所说的悬停函数到底是什么意思?比如当鼠标悬停在列表框中的某个项目上时,它会显示文本,表示工具提示类的通过或失败检查MSDN。为什么不构造LB内容,以便它不需要进一步的解释,例如重写ToString()
在课堂上?删除了我的答案。你的答案更完整、更有用:)+1Ahh我可能会放弃这太复杂了,有很多错误。这取决于你想做什么;我担心我可能没有完全理解你的任务。我和其他人都认为列表框显示的是男性学生。是这样还是总是一个?
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() );
}