我正在C#中创建一个用户控件,但我不知道如何处理事件。我想在鼠标悬停时更改面板的backcolor属性,但是它不起作用。
代码:
public partial class QuestionList : UserControl
{
public QuestionList()
{
InitializeComponent();
}
public struct QuestionListItem
{
public string Question { get; set; }
public string Answer { get; set; }
public QuestionListItem(string question, string answer)
{
Question = question;
Answer = answer;
}
}
public void Add(QuestionListItem questionlistItem)
{
Panel panel = new Panel();
panel.Dock = DockStyle.Top;
Label label = new Label();
label.MouseHover += Label_MouseHover;
label.Dock = DockStyle.Fill;
label.Text = questionlistItem.Question;
panel.Controls.Add(label);
Controls.Add(panel);
}
//Here (no idea what I just did..)
private void Label_MouseHover(Object sender, EventArgs e)
{
Label label = (Label)sender;
Panel panel = (Panel)label.Container;
panel.BackColor = Color.Red;
}
}
我认为您已经添加了事件处理程序。问题出在事件处理程序中:
Panel panel = (Panel)label.Container;
应该
Panel panel = (Panel)label.Parent;
将容器更改为父级。
另外,我认为最好使用VS设计器来测试事件处理程序的强类型签名是什么。在签名中,使用EventArgs。我相信应该改为MouseEventArgs。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句