简体   繁体   中英

Visual studio designer view cannot get correct form

i have GUI project in C#. the main window class' definition looks like this:

FormView.cs file

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 file

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 file

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

the question is: why i'm getting a form, with wrong size and no elements when i'm trying to view FormView.cs in design view in visual studio 2010?

Do you have a constructor in your FormView? If yes, is the method InitializeComponent() called?

该表格必须是public类。

solved by moving elements definition into FormViewInit.cs file (file with InitializeComponent method).

how files looked before look in question. how file looks now:

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);
    }
}

you should view in designer mode FormViewInit.cs file, not FormView.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM