c# - 我可以使继承自 System.Windows.Forms.Form 类的类始终在代码 View 中打开吗?

标签 c# winforms visual-studio

我正在开发 Windows 窗体项目。其中我需要一个继承 System.Windows.Forms.Form 的类,我将其命名为FormBase.cs并继承System.Windows.Forms.Form类(class)。但在解决方案资源管理器中FormBase.cs获得像 Windows 窗体一样的 View 。现在,当我尝试从解决方案资源管理器打开文件时,它会在设计模式下打开。既然简单class我希望它必须在代码 View 中打开,而不是在设计 View 中打开。为什么会发生这种情况?如果我想要怎么办FormBase.cs总是在代码 View 中打开并在解决方案资源管理器中重新获得其类 View ? FormBase.cs看起来像:

public class FormBase : System.Windows.Forms.Form
{
    public virtual Dictionary<string, string> NonSaveableReasons()
    {
        Dictionary<string, string> _nonSavebleReasons = new Dictionary<string, string>();

        //MaskedTextBox.MaskedTextBox and MaskedTextBox.MyCombo are Custom Components
        //which are of type TextBox and ComboBox respectively
        //having 2 more properties name as "IsMandatory" and "LabelName"

        foreach (MaskedTextBox.MaskedTextBox maskTextBox in this.Controls.OfType<MaskedTextBox.MaskedTextBox>())
        {
            if (maskTextBox.IsMandatory && string.IsNullOrEmpty(maskTextBox.Text) && !_nonSavebleReasons.ContainsKey(maskTextBox.Name))
                _nonSavebleReasons.Add(maskTextBox.Name, maskTextBox.LabelName + " is mandatory.");
        }

        foreach (MaskedTextBox.MyCombo myCombo in this.Controls.OfType<MaskedTextBox.MyCombo>())
        {
            if (myCombo.IsMandatory && string.IsNullOrEmpty(myCombo.Text) && !_nonSavebleReasons.ContainsKey(myCombo.Name))
            {
                if (!_nonSavebleReasons.ContainsKey(myCombo.Name))
                    _nonSavebleReasons.Add(myCombo.Name, myCombo.LabelName + " is mandatory.");
            }
        }

        return _nonSavebleReasons;
    }

    public string GetValidationStringMsg(Dictionary<string, string> nonSavableResons)
    {
        return nonSavableResons != null ? String.Join(Environment.NewLine, nonSavableResons.Select(a => a.Value).ToArray()) : string.Empty;
    }
}

最佳答案

您可以使用 System.ComponentModel.DesignerCategoryAttribute阻止 Visual Studio 在设计器中打开某一特定文件。有两种方法可以应用此属性。

选项A

步骤 1. 将属性应用到 FormBase,指定 "" 作为类别:

[System.ComponentModel.DesignerCategory("")]
public class FormBase : System.Windows.Forms.Form

步骤 2. 将属性应用于从 FormBase 派生的每个表单,指定 “Form” 作为类别:

[System.ComponentModel.DesignerCategory("Form")]
public partial class MainForm : FormBase

请注意,您必须使用属性的完全限定类型名称。这不起作用:

// BAD CODE - DON'T USE
using System.ComponentModel;

[DesignerCategory("")]
public class FormBase : System.Windows.Forms.Form

选项B

在 FormBase.cs 的上方 FormBase 中,添加一个虚拟类并向其应用属性,并将 "" 指定为类别:

[System.ComponentModel.DesignerCategory("")]
internal class Unused
{
}

public class FormBase : System.Windows.Forms.Form
{
    // ...
}

通过这种方法,您不需要将该属性应用于从 FormBase 派生的每个表单,但代价是未使用类。

关于c# - 我可以使继承自 System.Windows.Forms.Form 类的类始终在代码 View 中打开吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18266910/

相关文章:

c# - 如何在 Windows 中获取当前用户登录 session 的唯一 ID - c#

c# - iFrame 在 asp AjaxToolkit TabContainer 中加载两次

c# - 由 json 私钥文件(ServiceAccount)创建的 GoogleCredential - 如何将用户设置为模拟?

c# - 删除和/或排序 Usings 的值(value)是什么?

c++ - 如何将 Visual Studio 项目导出到 Qt?

c# - 使用动态连接一个 Action 和一个字符串

c# - 将一组数据行绑定(bind)到 datagridview

c# - 仅使用单个搜索BackgroundWorker的自动完成文本框 - 代码示例?

vb.net - 尝试删除以前在 Visual Basic 中创建的边框

c++ - 编译器为 Project Euler #22 给出了不同的答案