【问题标题】:Visual studio designer view cannot get correct formVisual Studio 设计器视图无法获得正确的形式
【发布时间】:2012-04-29 23:20:45
【问题描述】:

我有 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 时,为什么我得到一个尺寸错误且没有元素的表单?

【问题讨论】:

  • 我强烈建议你从一个“正常”的表格开始,然后一点一点的改成你需要的样子,每次都检查一下你是否破坏了设计师。
  • 是的,在我看来,您的 InitializeComponet 类没有为您的表单及其子控件正确设置正确的值(听起来您甚至错过了将元素添加到表单的代码) .设计师可以为您做很多这样的工作。
  • 代码,为表单设置正确的值,它的孩子设置正确。我可以运行该应用程序,它按预期工作。我唯一无法在设计器视图中查看我的应用程序。

标签: c# winforms visual-studio-2010 windows-forms-designer


【解决方案1】:

您的 FormView 中有构造函数吗?如果是,是否调用了 InitializeComponent() 方法?

【讨论】:

  • 类 FormView 有公共构造函数,我正在调用方法 InitializeComponent()。
  • 它是默认构造函数吗? public FormView(){...}?
  • 看起来像 public FormView() { /* 这里有一些逻辑 */ this.InitializeComponent(); }
【解决方案2】:

表单必须是public 类。

【讨论】:

  • 我在所有 3 个文件中都添加了 public 关键字,但这没有帮助。
【解决方案3】:

通过将元素定义移动到 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多