2012-04-18 101 views
2

我在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 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 
    } 
} 

問題是:爲什麼我得到一個窗體,大小錯誤,沒有元素,當我試圖查看FormView。 CS在設計視圖在Visual Studio 2010?

+0

我強烈建議你從一個「正常」的形式開始,然後一點一點地把它變成了什麼你需要,每次檢查你是否打破了設計師。 – 2012-04-18 14:44:39

+0

是的,這聽起來像我的InitializeComponet類沒有正確設置您的窗體及其子控件的正確值(聽起來像你甚至錯過了代碼,將您的元素添加到您的窗體)。設計師可以爲你做很多這項工作。 – 2012-04-18 18:06:33

+0

代碼,它爲表單設置了正確的值,並且它的子項設置正確。我可以運行應用程序,並按預期工作。我唯一不能在設計器視圖中查看我的應用程序。 – cheshie 2012-04-18 22:52:45

回答

0

通過將元素定義移動到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

1

你的FormView中有一個構造函數嗎?如果是,是否調用InitializeComponent()方法?

+0

類FormView具有公共構造函數,我正在調用方法InitializeComponent()。 – cheshie 2012-04-18 14:40:49

+0

它是一個默認的構造函數嗎? '公共FormView(){...}'? – 2012-04-18 14:43:38

+0

它看起來像公共FormView(){/ *這裏有一些邏輯*/this.InialialComponent(); } – cheshie 2012-04-18 15:06:30

0

該表格需要是public類。

+0

我在所有3個文件中都添加了public關鍵字,但這並沒有幫助。 – cheshie 2012-04-18 14:42:23