我可以使从System.Windows.Forms.Form类继承的类始终在代码视图中打开吗
本文关键字:代码 视图 System 可以使 Windows Forms 继承 Form 我可以 | 更新日期: 2023-09-27 18:27:57
我正在进行Windows窗体项目。其中我需要一个继承System.Windows.Forms.Form
的类,我将其命名为FormBase.cs
并继承System.Windows.Forms.Form
类。但在解决方案资源管理器中,FormBase.cs
的视图与Windows窗体类似。现在,当我尝试从解决方案资源管器打开文件时,它会在设计模式下打开。由于它是简单的class
,我希望它必须在代码视图中打开,而不是在设计视图中打开。为什么会发生这种情况?如果我希望FormBase.cs
始终在代码视图中打开,并在解决方案资源管理器中重新获得其类视图,该怎么办?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
派生的每个表单,而需要以一个未使用的类为代价。
在解决方案资源管理器中,用右手单击表单,用左手按F7。表单现在每次都会在代码视图中打开。