无法更改 C# Windows 窗体应用程序项目中的启动窗体
在 Windows 应用程序项目(C#、Visual Studio 2010)中,我只想更改为新的启动形式(保留已测试过的旧启动形式)。在program.cs中,我注释掉“旧”表单并介绍我的新“克隆”:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1()); // this working form uses ListBox
Application.Run(new GridForm()); // want this new form which uses DataGridView
}
Form1和GridForm都在同一个项目中,并且都具有相同的using指令。 (片段如下):
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Reflection;
using CBMI.Common;
using log4net;
using log4net.Config;
namespace CBMI.WinFormsUI
{
public partial class GridForm : Form
{
private string defaultRootFolder;
public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));
public GridForm()
{
InitializeComponent();
}
似乎是一个极其简单的问题/问题,但我被难住了。
编辑更新:
不知何故,在克隆/“重构”过程中,我通过引入大小写更改搞乱了新表单上的命名空间。这是通过尝试您的建议发现的,如下所示:
private void Form1_Load(object sender, EventArgs e)
{
CBMI.WinFormsUI.GridForm improvedForm = new CBMI.WinFormsUI.GridForm();
improvedForm.Show();
return;
Form1.cs 的命名空间编码如下:
namespace CBMI.WinformsUI
GridForm.cs 的命名空间编码如下(注意 CamelCase):
namespace CBMI.WinFormsUI
EDIT-UPDATE :克隆/重构新表单的最佳实践是什么?
我忘记了如何破解新表单。我想我是从“添加新项目”到项目开始的,给它起了新名称并保存。然后,我手动复制了原始代码中的一些代码,并引入了新的 datagridview 等,并从新表单中删除了先前的用户界面控件。我一直觉得试图让“版本 1”继续工作并引入从“旧版本”构建的新扩展东西有点尴尬。 “专业人士”是如何做到这一点的?
In a windows application project (C#, Visual Studio 2010) I simply want to change to a new startup form (leaving the older one that has already been tested as it is). In program.cs, I comment out the "old" form and and introduce my new "clone":
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1()); // this working form uses ListBox
Application.Run(new GridForm()); // want this new form which uses DataGridView
}
Both Form1 and GridForm are in the same project and both have the same using directives. (snippet follows):
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Reflection;
using CBMI.Common;
using log4net;
using log4net.Config;
namespace CBMI.WinFormsUI
{
public partial class GridForm : Form
{
private string defaultRootFolder;
public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));
public GridForm()
{
InitializeComponent();
}
Seems like a ridiculously simple question/issue but I am stumped.
EDIT-UPDATE:
Somehow in the cloning/"refactoring" process, I messed up the namespace on the newer form by introducing a case change. This was discovered by trying your suggestion as follows:
private void Form1_Load(object sender, EventArgs e)
{
CBMI.WinFormsUI.GridForm improvedForm = new CBMI.WinFormsUI.GridForm();
improvedForm.Show();
return;
Form1.cs has namespace coded as follows:
namespace CBMI.WinformsUI
GridForm.cs has namespace coded as follows (note the CamelCase):
namespace CBMI.WinFormsUI
EDIT-UPDATE: What is best practice for cloning/refactoring new form?
I forget how I hacked out the new form. I think I started by "add new item" to the project, gave it new name and saved. Then I manually copied over some code from the original and introduced new datagridview, etc. and removed the prior user interface controls from the new form. I've always found this a bit awkward trying to leave "version 1" working and introduce a new extended thing built up from the "old". How do the "pros" do this sort of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是运行您想要的表单的正确方法。因此,Main() 与新表单的关系一定存在问题。就像它位于错误的命名空间中,或者类似的东西。
通过尝试使用以下命令从现有启动表单运行表单实例来检查它:
That is the proper way to run the form you want. So there has to be an issue with how Main() relates to your new form. Like it being in the wrong namespace, or something to that effect.
Check it by trying to run an instance of you form from the existing startup form using:
我假设您复制了
Form1
的所有文件(最多可能有三个:Form1.cs Form1.Designer.cs 和 Form1.resx
)。那么你是否更改了 new*.Designer.cs
中的类名?到 GridForm ?除了这个问题之外,
Main
中的代码应该没有问题。您只需向项目添加一个全新的表单并像您所做的那样引用它即可对其进行测试。I assume you copied all files for
Form1
(there may be up to three of them:Form1.cs Form1.Designer.cs and Form1.resx
). Then did you change the class name in new*.Designer.cs
? ToGridForm
?Other than this issue, there should be no problem with your code in
Main
. And you can test it just by adding a brand new form to the project and referencing it as you did.