且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

此代码禁用窗体中的所有控件,但我想禁用窗体中的文本框

更新时间:2023-10-12 23:05:10

代码相同,但您需要检查这是一个文本框吗?在你的循环中:

The code is the same, but you need to check "is this a textbox?" inside your loop:
foreach(Control c in con.Controls)
    {
    if (c is TextBox)
        {
        c.Enabled = false;
        }    
    }







引用:

当我的文本框在Panel中时它可以工作



取决于你传递给你的方法的内容:如果它是Form,那么它赢了't - Form.Controls集合包含面板,而Panel.Controls集合包含TextBox,因此无法找到它。

如果是你通过的Panel那么它将会。



如果你想禁用一个文本框在窗体控件的某个地方,那么你需要递归调用你的方法:


Depends on what you pass to your method: if it's the Form, then it won't - the Form.Controls collection contains the panel, and the Panel.Controls collection contains the TextBox so it won't get found.
If it's the Panel you pass then it will.

If you want to disable a textbox "somewhere in a control on the the form", then you need to call your method recursively:

public void DisableControl(Control con)
    {
    foreach(Control c in con.Controls)
        {
        DisableControl(c);
        if (c is TextBox)
            {
            c.Enabled = false;
            }    
        }
    }

如果您将表单传递给表单,那将会有效,并禁用表单上的所有文本框。

That will work if you pass it the form, and disable all textboxes on the form.