简体   繁体   中英

Efficient way of hiding multiple objects from C# form

is there a way to hide multiple objects from one form in a more efficient way than hiding them individually. I have a form I want to have 4 functions on; add, edit, delete and view data in a database. When I click one i want the objects for each function to become visible and the ones not needed will be hidden.

Create collections for the controls you want to show/hide and add the respective controls to them. Then loop over these collections.

var controlsToHide = new List<Control>();
controlsToHide.Add(myButton);
controlsToHide.Add(otherButton);
HideAll(controlsToHide);

public void HideAll(List<Control> controls) {
    foreach(var ctrl in controls) {
        ctrl.Visisble = false;
    }
}

Put the controls in Panels. Turn the Panels on and off.
IIRC a group box does the same.
You can be more snazzy and use UserControls or CustomControls
but start with Panels to get the hang of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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