简体   繁体   English

同时为C#Winform中的多个动态控件触发事件

[英]Fire an event simultaneously for multiple dynamic controls in c# winform

I have N number of dynamically added PictureBoxes in FlowLayoutPanel . 我在FlowLayoutPanelN个动态添加的PictureBoxes When I create them I attach event handlers to them. 创建它们时,我将事件处理程序附加到它们。 For example: 例如:

for(int i=0;i<x;i++) {    
    var pe= new PictureBox();
    pe.MouseUp+=mouseup;
    pe.MouseDown+=mouseDown;
    pe.MouseMove+=mouseMove;
    pe.Paint+=paint;
}

My goal is to fire those events for all picture boxes whenever I work with any one of them. 我的目标是在与任何一个图片框一起工作时为所有图片框触发这些事件。 For example, if I move one picturebox (1st/2nd/3rd/.../n ) all others will move automatically, if I zoom any box, others will zoom automatically. 例如,如果我移动一个图片picturebox (1st / 2nd / 3rd /.../ n),所有其他图片框都会自动移动,如果我缩放任何图片框,其他图片框也会自动缩放。 How can I fire events simultaneously for all pictureboxes when I work with anyone. 与任何人一起工作时,如何为所有图片框同时触发事件。

If I try for example: 如果我尝试例如:

void mouseWheel(object sender, MouseEventArgs e) {
    var control=(PictureBox)sender;
    var parent=control.parent;
    var pictureBoxes=parent.ofType<PictureBox>();

    foreach(pb in pictureBoxes) {
                //do something
    }
}

It only works for the picture box I am working with. 它仅适用于正在使用的图片框。

You need to call a method instead of raising the event. 您需要调用一个方法而不是引发事件。

Create some methods and put logic on the methods, then in the event handler, first extract information that you need, then call suitable method with parameters. 创建一些方法并将逻辑放在这些方法上,然后在事件处理程序中,首先提取所需的信息,然后使用参数调用合适的方法。

For example: 例如:

void pictureBox_MouseWheel(object sender, MouseEventArgs e)
{
    //Some parameter that you extract from eventArgs or somewhere else
    int zoomFactor = e.Delta;

    //Call the method on your picture boxes
    foreach (var p in pictureBoxes)
    {
        Zoom(p, zoomFactor);
    }
}

//The method that contains logic of zoom on a picture box
public void Zoom(PictureBox p, int zoomFactor)
{
    //It is just an example, not a real logic
    p.SizeMode = PictureBoxSizeMode.Zoom;
    p.Width += (zoomFactor * 10);
    p.Height += (zoomFactor * 10);
}

I supposed you have added all your pictureboxes in a List<PictureBox> when you created them. 我以为您在创建它们时已将所有List<PictureBox>添加到List<PictureBox>

Also if you have added your picture boxes to a Controls collection of a control, for example theControl , then you can find them later this way: 同样,如果您已将图片框添加到Controls集合中(例如theControl ,则可以稍后通过以下方式找到它们:

var pictureBoxes = theControl.Controls.OfType<PictureBox>().ToList();

It looks like you already have a list of picture boxes. 您似乎已经有了一个图片框列表。 So, try modifying your functions (for example your Zoom function) to work for all picture boxes in the list, rather than just the one picture box. 因此,请尝试修改您的功能(例如您的“缩放”功能)以使其适用于列表中的所有图片框,而不只是一个图片框。

In other words, don't try to call the event handler for each picture box, make each event handler call a function, which modifies all picture boxes. 换句话说,不要尝试为每个图片框调用事件处理程序,而要让每个事件处理程序调用一个函数来修改所有图片框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM