C# Visual studio designer视图无法获取正确的表单
我有一个C#的GUI项目。主窗口类的定义如下所示: FormView.cs文件C# Visual studio designer视图无法获取正确的表单,c#,winforms,visual-studio-2010,windows-forms-designer,C#,Winforms,Visual Studio 2010,Windows Forms Designer,我有一个C#的GUI项目。主窗口类的定义如下所示: FormView.cs文件 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Windows.Forms; namespace RssReader { partial class FormView : Form, IView { private SplitConta
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace RssReader
{
partial class FormView : Form, IView
{
private SplitContainer MainContainer;
private TreeView Items;
private MenuStrip MainMenu;
private ToolStripMenuItem File;
private ToolStripMenuItem AddFeed;
private ToolStripSeparator Separator;
private ToolStripMenuItem Quit;
private WebBrowser Message;
/* some methods here which are implementing some kind of logic */
}
}
FormViewInit.cs文件
namespace RssReader
{
partial class FormView
{
private void InitializeComponent()
{
this.MainContainer = new System.Windows.Forms.SplitContainer();
this.Items = new System.Windows.Forms.TreeView();
this.Message = new System.Windows.Forms.WebBrowser();
this.MainMenu = new System.Windows.Forms.MenuStrip();
this.File = new System.Windows.Forms.ToolStripMenuItem();
this.AddFeed = new System.Windows.Forms.ToolStripMenuItem();
this.Separator = new System.Windows.Forms.ToolStripSeparator();
this.Quit = new System.Windows.Forms.ToolStripMenuItem();
// the only component in this file is InitializeComponent method
// all, what it does is just defining items on the form
// and initializing it, i.e., creating instances, assign names etc.
}
}
}
FormViewEventHandlers.cs文件
using System;
using System.IO;
using System.Windows.Forms;
namespace RssReader
{
partial class FormView
{
private void Quit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to quit?", "Exit", MessageBoxButtons.YesNo)
== DialogResult.Yes)
Application.Exit();
}
// here goes event handler functions
}
}
问题是:当我在visual studio 2010的“设计”视图中查看FormView.cs时,为什么会得到一个大小错误且没有元素的表单?您的FormView中是否有构造函数?如果是,是否调用了InitializeComponent()方法?表单需要是
公共类。通过将元素定义移动到FormViewInit.cs文件(使用InitializeComponent方法的文件)中来解决
文件以前是如何查看的,这是个问题。文件现在的外观:
public partial class FormView
{
private System.ComponentModel.IContainer components = null;
private SplitContainer MainContainer;
private TreeView Items;
private MenuStrip MainMenu;
private ToolStripMenuItem File;
private ToolStripMenuItem AddFeed;
private ToolStripSeparator Separator;
private ToolStripMenuItem Quit;
private ContextMenuStrip ContextMenu;
private ToolStripMenuItem RemoveItem;
private WebBrowser Message;
protected void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.MainContainer = new System.Windows.Forms.SplitContainer();
this.Items = new System.Windows.Forms.TreeView();
this.Message = new System.Windows.Forms.WebBrowser();
this.MainMenu = new System.Windows.Forms.MenuStrip();
this.File = new System.Windows.Forms.ToolStripMenuItem();
this.AddFeed = new System.Windows.Forms.ToolStripMenuItem();
this.Separator = new System.Windows.Forms.ToolStripSeparator();
this.Quit = new System.Windows.Forms.ToolStripMenuItem();
this.ContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
this.RemoveItem = new System.Windows.Forms.ToolStripMenuItem();
this.MainContainer.Panel1.SuspendLayout();
this.MainContainer.Panel2.SuspendLayout();
this.MainContainer.SuspendLayout();
this.MainMenu.SuspendLayout();
this.ContextMenu.SuspendLayout();
this.SuspendLayout();
/* there goes properties initializing, like setting names, sizes etc */
}
// Added just in case
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
您应该以设计器模式查看FormViewInit.cs文件,而不是FormView.cs类FormView具有公共构造函数,我正在调用方法InitializeComponent()。它是默认构造函数吗public FormView(){…}
?它看起来像public FormView(){/*这里有一些逻辑*/this.InitializeComponent();}我在所有3个文件中都添加了public关键字,但这没有帮助。我强烈建议您从“普通”表单开始,然后将它一点一点地更改为您需要的表单,每次检查是否破坏了设计器。是的,在我看来,InitializeComponet类没有正确地为窗体及其子控件设置正确的值(听起来您甚至缺少将元素添加到窗体中的代码)。设计师可以为您做很多这方面的工作。代码,为窗体设置正确的值,它的子项设置正确。我可以运行应用程序,并且它可以按预期工作。我唯一不能在designer视图中查看我的应用程序的东西。