[英]User Control Form Click event not clicking on label C#
I have a problem with User form Click
that I am trying to make in C# using a usercontrol
.我尝试使用usercontrol
在 C# 中创建的 User form Click
有问题。 It consists of a picturebox
and a label
.它由一个label
picturebox
I want to call the click
event but the picturebox and the label don't do anything when I click them.我想调用click
事件,但是当我点击它们时,图片框和 label 没有做任何事情。 Only the background area of the usercontrol
does what I want it to do.只有usercontrol
的背景区域执行我希望它执行的操作。 Any ideas?有任何想法吗?
here's my code这是我的代码
for (int i = 0; i < listitems2.Length; i++)
{
listitems2[i] = new declined();
//adding sample data to each dynamic user
listitems2[i].dicon = dicon[i];
listitems2[i].did = did[i];
listitems2[i].dname = dname[i];
//adding data to flow layout panel
flowLayoutPanel2.Controls.Add(listitems2[i]);
// below line will assing this (usercontrolclick) event to every user control created dynamically
listitems2[i].Click += new System.EventHandler(this.UserControl_Click);
}
for click function点击 function
void UserControl_Click(object sender, EventArgs e)
{
string ctrlName = ((UserControl)sender).Name;
applicable obj = (applicable)sender;
studid.Text = obj.id;
studname.Text = obj.name;
pictureBox1.Image = obj.icon;
}
You added OnClick
handler to your UserControl
, so only clicks made on UserControl
will be registered.您向UserControl
添加了OnClick
处理程序,因此只会注册对UserControl
的点击。 To enable Click
event for all the controls on your form, you need to add your click handler to all other controls.要为窗体上的所有控件启用Click
事件,您需要将点击处理程序添加到所有其他控件。
But, according to your code, you're adding click handling on form where your control is located.但是,根据您的代码,您要在控件所在的表单上添加点击处理。 In this case, you cannot register clicks on control from "outside" (you can by making Label
and PictureBox
controls public and adding handlers for their OnClick
events but that is wrong way to do it)在这种情况下,您不能从“外部”注册对控件的点击(您可以通过公开Label
和PictureBox
控件并为其OnClick
事件添加处理程序,但这是错误的方法)
My suggestion is to make custom event on your form and raise it on Form
, Label
and PictureBox
click, and then make handler for that event on form which holds your UserControl
.我的建议是在您的表单上创建自定义事件并在Form
、 Label
和PictureBox
click 上引发它,然后在包含您的UserControl
的表单上为该事件创建处理程序。
Something like this:是这样的:
//make custom eventHandler on your UserControl
[Browsable(true)]
public event EventHandler UserControlClicked;
//constructor
public UserControl1()
{
InitializeComponent();
//after intialize compoment add same handler for all three controls
this.Click += ControlClicked;
this.pictureBox1.Click += ControlClicked;
this.label1.Click += ControlClicked;
}
//this method will "catch" all clicks
private void ControlClicked(object sender, EventArgs e)
{
//raise event
UserControlClicked?.Invoke(this, e);
}
and on form where your custom control is, add handler for UserControlClicked (maybe with cast, I don't know what listItems2[i] contains):并在您的自定义控件所在的表单上,为 UserControlClicked 添加处理程序(可能使用 cast,我不知道 listItems2[i] 包含什么):
listitems2[i].UserControlClicked+= new System.EventHandler(this.UserControl_Click);
or maybe like this (with casting)或者可能像这样(铸造)
(listitems2[i] as UserControl1).UserControlClicked+= new System.EventHandler(this.UserControl_Click);
and handle the rest in your UserControl_Click
method like before并像以前一样在UserControl_Click
方法中处理 rest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.