我正在开发 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/