Forearch loop how to use with controler

1

I have 6 PictureEdit in a PanelControl , and I need to run a haul on all PictureEdit so I made the following code:

 foreach (PictureEdit Pic in panel1.Controls)
        {
            //habilita o meno de zoom
            Pic.Properties.ShowZoomSubMenu = DevExpress.Utils.DefaultBoolean.True;
            //habilita mexer a imagem via mouse
            Pic.Properties.AllowScrollViaMouseDrag = false;
            // habilita zoom com a roda do mouse
            Pic.Properties.AllowZoomOnMouseWheel = DefaultBoolean.False;
            // habilita zoom sem o control
            Pic.Properties.ZoomingOperationMode = DevExpress.XtraEditors.Repository.ZoomingOperationMode.ControlMouseWheel;
            //definie o sizemode da imagem como clip, obrigatorio para funcionar o zzom
            Pic.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
            // defini o tamanho de zoom que a imagem é exibida
            Pic.Properties.ZoomPercent = 15;

            // defini o evento mouse wheel, necessario para controlar a velocidade da roda do mouse
            Pic.MouseWheel += new MouseEventHandler(this.pictureEdit1_MouseWheel);
            // defini o evento imagechange, ao editar uma imagem é adicionado a lista de edições
            Pic.ImageChanged += new EventHandler(this.Pic_Change_Edit);
        }

However, running the following error appears:

    
asked by anonymous 21.05.2017 / 13:08

1 answer

2

Windows Forms does not have controllers .

The class Panel is a containter for various controls of all types. One of the members of this class is Controls , as used in the code. It is a specialized slick that has objects derived from Control .

Any type derived from Control can be automatically interpreted as a Control , after all by subtype, a PictureEdit is a Control . As he inherited everything from Control , there is certainly all that is needed in it.

The opposite is not true. If an object is Control it can contain any derived from it. It can have a Label for example. So if you take one of the elements of panel1.Controls and it's a Label , how are you going to save a label in PictureEdit ? They are different. Not completely because everything that came from Control they have in common, but the specific part is different, it would be a mess.

This is called covariance .

The solution is to read all the controls and filter them to get what you want.

foreach (var control in panel1.Controls) {
    if (control is PictureEdit) {
        var picture = (PictureEdit)control;
        picture.Properties.ShowZoomSubMenu = DevExpress.Utils.DefaultBoolean.True;
        picture.Properties.AllowScrollViaMouseDrag = false;
        picture.Properties.AllowZoomOnMouseWheel = DefaultBoolean.False;
        picture.Properties.ZoomingOperationMode = DevExpress.XtraEditors.Repository.ZoomingOperationMode.ControlMouseWheel;
        picture.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
        picture.Properties.ZoomPercent = 15;
        picture.MouseWheel += new MouseEventHandler(this.pictureEdit1_MouseWheel);
        picture.ImageChanged += new EventHandler(this.Pic_Change_Edit);
    }
}

If you are using C # 7 you can avoid cast :

foreach (var control in panel1.Controls) {
    if (control is PictureEdit picture) {
        picture.Properties.ShowZoomSubMenu = DevExpress.Utils.DefaultBoolean.True;
        picture.Properties.AllowScrollViaMouseDrag = false;
        picture.Properties.AllowZoomOnMouseWheel = DefaultBoolean.False;
        picture.Properties.ZoomingOperationMode = DevExpress.XtraEditors.Repository.ZoomingOperationMode.ControlMouseWheel;
        picture.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
        picture.Properties.ZoomPercent = 15;
        picture.MouseWheel += new MouseEventHandler(this.pictureEdit1_MouseWheel);
        picture.ImageChanged += new EventHandler(this.Pic_Change_Edit);
    }
}
    
21.05.2017 / 14:14