Introduction
Below Code will help you to set the panel visibility based upon the parent control and Panel object ID. We just need to pass those two as a parameter to the method.
Background
Few days back,I got requirement to put many panel controls to the application. So as on , the application grows by putting more panel controls i faced the difficulty . Below i have mentioned the difficulty i have faced and Provided the solution i got.
What problem the below code solves?
As we know , Panel control can be set to visible true or false as per requirement . And when compared to the View , two or more panels can be set to true whereas in view we could show only one at a time.But the problem is, once we set the required panel to true,it is also developer duty to set the other panels visible to false ,otherwise everything will show together. So the code will be very large and it will take plenty of time for the developers. so i thought to get solution for this and finally i wrote the below code:
- public void SetPanelVisibility(Panel pnlobjid, Panel parentformpanel)
- {
- try
- {
- foreach(Control cntrl in parentformpanel.Controls)
- {
- if (cntrl is Panel)
- {
- Panel pnl = cntrl as Panel;
- pnl.Visible = false;
- }
- }
- pnlobjid.Visible = true;
- }
- catch (Exception ex)
- {
- string test = ex.Message;
- }
- }
Hope the above was easily understandable.