使用按钮将表单重置为其原始状态

时间:2015-03-11 10:36:02

标签: c# winforms controls reset

enter image description here

我创建了一个新按钮来重置由
组成的表单 组合框,
文本框,
RichTextBox的,
禁用按钮

所以,我创建了一个utilities.cs类,但是它没有工作并且出错了。

错误:类型或命名空间名称' ComboBox'无法找到。

namespace example
{
class Utilities
{
    public static void ResetAllControls(Mainform form)
    {
        foreach (Mainform control in form.Controls)
        {
            if (form is ComboBox)
            {
                ComboBox comboBox = (ComboBox)control;
                if (comboBox.Items.Count > 0)
                    comboBox.SelectedIndex = 0;
            }
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

您正试图访问Form类的ComboBoxComboBoxSystem.Windows.Forms命名空间中定义,因此您需要使用System.Windows.Form添加

using System.Windows.Forms; //You need this statement.
namespace example
{
 class Utilities
 {
    public static void ResetAllControls(Mainform form)
    {
        foreach (Mainform control in form.Controls)
        {
            if (form is ComboBox)
            {
                ComboBox comboBox = (ComboBox)control;
                if (comboBox.Items.Count > 0)
                    comboBox.SelectedIndex = 0;
            }
        }
    }    
 }
}

修改

如果Utilities类不在WinForms项目中,那么您需要添加对System.Windows.Forms的引用,本文Add or Remove References By Using the Add Reference Dialog Box将指导您添加引用。

相关问题