Forearch loop how to use with controler

Asked

Viewed 50 times

1

I’m 6 PictureEdit in a PanelControl, and I need to make a run at all the PictureEdit then 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, when running the following error is displayed:

Erro Visual Studio

1 answer

2


Windows Forms do not have controllers.

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

Any type derived from Control can be automatically interpreted as if it were a Control, after all by the subtype, a PictureEdit is a Control. How he inherited everything from Control, surely there is all that is needed in it.

The opposite is not true. If an object is Control it may contain whichever derived from it. May have a Label for example. So if you take one of the elements of panel1.Controls and he’s a Label, how will you keep a label in PictureEdit? They are different. Not completely because all 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 the 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);
    }
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.