[英]How to disable all on the forms except for the panel
我想禁用除了下面顯示的面板之外的所有內容
這個面板的觸發是當我點擊我的datagridview上的編輯按鈕並且面板將是可見的。所以基本上面板在默認情況下是不可見的(這里是datagrid中編輯按鈕的下面的代碼)
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
panel1.Visible = true;
selectedItenOutputOrderTabGrid.Rows.RemoveAt(selectedItenOutputOrderTabGrid.CurrentRow.Index);
// HERE I WANT TO ADD A FUNCTION THAT WOULD DISABLE ALL IN THE FORM EXCEPT THE PANEL
}
}
要禁用除面板panel1
之外的所有表單子控件,請嘗試以下操作:
foreach (var control in Controls.Cast<Control>().Where(ctrl => ctrl.Name != "panel1"))
{
control.Enabled = false;
}
我對此進行了測試,它禁用了所有其他控件,同時使 panel1 和其中的控件仍處於啟用狀態。 這是從https://stackoverflow.com/a/13446223/3472690修改的代碼
foreach (Control c in this.Controls)
{
c.Enabled = false;
}
panel1.Parent.Parent.Enabled = true;
foreach (Control c in panel1.Parent.Parent.Controls)
{
c.Enabled = false;
}
panel1.Parent.Enabled = true;
foreach (Control c in panel1.Parent.Controls)
{
c.Enabled = false;
}
panel1.Enabled = true;
這段代碼的作用非常簡單:
首先,第一個 foreach 禁用表單中的所有控件,但不禁用表單本身。 然后,它啟用面板的父級的父級,也就是 tabcontrol 本身,然后禁用 tab 控件中的所有內容。
然后它啟用面板所在的標簽頁,並禁用標簽頁中的所有其他內容。
最后,它啟用面板本身。 這不是一段非常優化的代碼,但它有效。
這意味着,根據上圖,tabControl1 本身已啟用,但其中的所有內容都被禁用,除了 panel1 所在的 tabPage1,而 tabPage1 中除了 panel1 之外的所有內容都被禁用。
編輯:
要反轉所有其他控件的禁用,只需執行以下操作:
foreach (Control c in this.Controls)
{
c.Enabled = true;
}
panel1.Enabled = false;
private void ShowFilter(Control parentControl, Control childControl, bool enable)
{
foreach (Control control in parentControl.Controls.Cast<Control>().Where(ctrl => ctrl.Name != childControl.Name))
{
control.Enabled = !enable;
}
childControl.Visible = enable;
}
private void Drucken_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if(!c.HasChildren)
{
c.Enabled = false;
}
foreach(Control d in tabPage1.Controls)
if(!d.HasChildren)
{
d.Enabled = false;
}
foreach(Control t in panel1.Controls)
{
if( t is TextBox)
{
t.Enabled = false;
}
Drucken.Enabled = false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.