winform空间批量控制

时间:2023-03-09 04:57:32
winform空间批量控制

第一版:

private void RefreshControl(PanelEx panel, bool enabled, bool isClear)
{
for (int i = ; i < panel.Controls.Count; i++)
{
if (panel.Controls[i] is PanelEx)
{
PanelEx panel1 = panel.Controls[i] as PanelEx;
RefreshControl(panel1, enabled, isClear);
}
else if (panel.Controls[i] is DevComponents.DotNetBar.TabControl)
{
DevComponents.DotNetBar.TabControl tabControl = panel.Controls[i] as DevComponents.DotNetBar.TabControl;
RefreshControl(tabControl, enabled, isClear);
}
else if (panel.Controls[i] is TextBoxX)
{
TextBox textbox = panel.Controls[i] as TextBoxX;
textbox.Enabled = enabled;
if (isClear)
textbox.Clear();
}
else if (panel.Controls[i] is DateTimeInput)
{
DateTimeInput dtInput = panel.Controls[i] as DateTimeInput;
dtInput.Enabled = enabled;
if (isClear)
dtInput.Value = DateTime.Parse("2999/1/1");
}
else if (panel.Controls[i] is ButtonX)
{
ButtonX btn = panel.Controls[i] as ButtonX;
btn.Enabled = enabled;
}
else if (panel.Controls[i] is CheckBoxX)
{
CheckBoxX checkBoxX = panel.Controls[i] as CheckBoxX;
checkBoxX.Enabled = enabled;
if (isClear)
checkBoxX.Checked = false;
}
else if (panel.Controls[i] is BarcodeControl)
{
BarcodeControl barcodeControl = panel.Controls[i] as BarcodeControl;
barcodeControl.Enabled = enabled;
if (isClear)
barcodeControl.Data = "";
}
else if (panel.Controls[i] is ComboBoxEx)
{
ComboBoxEx comboBoxEx = panel.Controls[i] as ComboBoxEx;
comboBoxEx.Enabled = enabled;
if (isClear)
comboBoxEx.Text = "";
}
}
} private void RefreshControl(DevComponents.DotNetBar.TabControl tabControl, bool enabled, bool isClear)
{
int index = tabControl.SelectedTabIndex;
for (int i = ; i < ; i++)
{
TabControlPanel panel = tabControl.SelectedPanel;
foreach (Control item in panel.Controls)
{
if (item is TextBoxX)
{
TextBox textbox = item as TextBoxX;
textbox.Enabled = enabled;
if (isClear)
textbox.Clear();
}
else if (item is CheckBoxX)
{
CheckBoxX checkBoxX = item as CheckBoxX;
checkBoxX.Enabled = enabled;
if (isClear)
checkBoxX.Checked = false;
}
else if (item is ButtonX)
{
ButtonX btn = item as ButtonX;
btn.Enabled = enabled;
}
else if (item is DataGridViewX)
{
DataGridViewX grid = item as DataGridViewX;
if (isClear)
{
if (grid.DataSource != null && grid.Rows.Count != )
{
DataTable dt = (DataTable)grid.DataSource;
dt.Rows.Clear();
grid.DataSource = dt;
//grid.Rows.Clear();
}
}
}
else if (item is DateTimeInput)
{
DateTimeInput dtInput = item as DateTimeInput;
dtInput.Enabled = enabled;
if (isClear)
{
dtInput.Value = DateTime.Parse("2999/1/1");
}
}
else if (item is PictureBox)
{
PictureBox pic = item as PictureBox;
pic.Enabled = enabled;
if (isClear)
{
pic.Image = null;
}
}
else if (item is PanelEx)
{
PanelEx panel1 = item as PanelEx;
RefreshControl(panel1, enabled, isClear);
}
}
if (!tabControl.SelectNextTab())
{
tabControl.SelectedTabIndex = ;
if (tabControl.SelectedTabIndex == index)
break;
}
}
tabControl.SelectedTabIndex = index;
}

第二版

private void RefreshControl(Control baseControl,bool enabled, bool isClear)
{
foreach (Control Control in baseControl.Controls)
{
if (Control is PanelEx)
{
PanelEx panel = Control as PanelEx;
RefreshControl(panel, enabled, isClear);
}
else if (Control is DevComponents.DotNetBar.TabControl)
{
DevComponents.DotNetBar.TabControl tabControl = Control as DevComponents.DotNetBar.TabControl;
RefreshControl(tabControl, enabled, isClear);
}
else if (Control is TabControlPanel)
{
TabControlPanel tabPanel = Control as TabControlPanel;
RefreshControl(tabPanel, enabled, isClear);
}
else if (Control is TextBoxX)
{
TextBox textbox = Control as TextBoxX;
textbox.Enabled = enabled;
if (isClear)
textbox.Clear();
}
else if (Control is ButtonX)
{
ButtonX btn = Control as ButtonX;
btn.Enabled = enabled;
}
else if (Control is DateTimeInput)
{
DateTimeInput dtInput = Control as DateTimeInput;
dtInput.Enabled = enabled;
if (isClear)
dtInput.Value = DateTime.Parse("2999/1/1");
}
else if (Control is CheckBoxX)
{
CheckBoxX checkBoxX = Control as CheckBoxX;
checkBoxX.Enabled = enabled;
if (isClear)
checkBoxX.Checked = false;
}
else if (Control is BarcodeControl)
{
BarcodeControl barcodeControl = Control as BarcodeControl;
barcodeControl.Enabled = enabled;
if (isClear)
barcodeControl.Data = "";
}
else if (Control is ComboBoxEx)
{
ComboBoxEx comboBoxEx = Control as ComboBoxEx;
comboBoxEx.Enabled = enabled;
if (isClear)
comboBoxEx.Text = "";
}
else if (Control is DataGridViewX)
{
DataGridViewX grid = Control as DataGridViewX;
if (isClear)
{
if (grid.DataSource != null && grid.Rows.Count != )
{
DataTable dt = (DataTable)grid.DataSource;
dt.Rows.Clear();
grid.DataSource = dt;
//grid.Rows.Clear();
}
}
}
else if (Control is PictureBox)
{
PictureBox pic = Control as PictureBox;
pic.Enabled = enabled;
if (isClear)
{
pic.Image = null;
}
}
else if (Control is GroupPanel)
{
RefreshControl(Control, enabled, isClear);
}
}
}