簡體   English   中英

Visual Studio設計器視圖無法獲取正確的表單

[英]Visual studio designer view cannot get correct form

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

問題是:當我嘗試在Visual Studio 2010的設計視圖中查看FormView.cs時,為什么我得到的表單的尺寸不正確且沒有任何元素?

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

該表格必須是public類。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM