最初我有一个图片框,用户可以在表单上将其从一个地方移动到另一个地方。 我已经处理了图片框的事件并且它移动得很完美。
但是现在用户想要在图片下方显示文字。所以我想动态创建一个自定义控件,并在用户控件中添加那个图片框和一个标签控件。
我还将控件的停靠属性设置为 TOP 和 Bottom。现在我的用户控件完全被子控件覆盖了。
之后我想为用户控件处理鼠标事件。但不幸的是,这对我不起作用。
根据我的理解,现在我无法访问用户控件,而是可以访问用户控件中的子控件,因此用户控件的鼠标事件不起作用。
如有错误请指正,并提供解决方案。
最佳答案
好吧,MouseDown
和 MouseUp
等鼠标事件仅在鼠标在特定控件上执行操作时才会发生。我能给你的最好的办法是捕获控件中的每个鼠标事件并调用 userControl
public UserControl1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(this.UserControl1_MouseDown);
this.comboBox1.MouseDown += new MouseEventHandler(this.comboBox1_MouseDown);
}
private void UserControl1_MouseClick(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void comboBox1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UCMouseDown()
{
// Your code
}
关于c# - 动态处理 UserControl 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18712428/