Cannot change startup form in c# windows form application project
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?
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:
Formname frm1 = new Formname();
frm1.show();
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
? To GridForm
?
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.
精彩评论