无法将方法添加到要在初始化组件期间调用的窗口窗体



我正在用c#创建Visual Studio 2013的GUI。我使用内置的设计器,当创建我的GUI时,我添加了一个listView对象,我想包含2列。我有以下代码:

partial class EmailSenderGUI
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    //My method that I made
    private void initRecipListView()
    {
        System.Console.WriteLine("Test");
        this.recipList.Columns.Add("Recipient", -2, System.Windows.Forms.HorizontalAlignment.Left);
        this.recipList.Columns.Add("Number of Reports", -2, System.Windows.Forms.HorizontalAlignment.Left);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.recipList = new System.Windows.Forms.ListView();
        this.SuspendLayout();
        // 
        // recipList
        // 
        this.recipList.Location = new System.Drawing.Point(16, 32);
        this.recipList.Name = "recipList";
        this.recipList.Size = new System.Drawing.Size(376, 296);
        this.recipList.TabIndex = 1;
        this.recipList.UseCompatibleStateImageBehavior = false;
        this.recipList.View = System.Windows.Forms.View.Details;
        initRecipListView();
        // 
        // EmailSenderGUI
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(405, 400);
        this.Controls.Add(this.recipList);
        this.Name = "EmailSenderGUI";
        this.Text = "EmailSenderGUI";
        this.Load += new System.EventHandler(this.EmailSenderGUI_Load);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    #endregion

正如你所看到的,我做了方法initRecipeListView,但当我试图运行代码时,我在设计窗口得到以下错误:

Method 'System.Windows.Forms.Form.initRecipListView' not found.

我想把这个方法保存在那个局部类中,以保持整洁和可读性,但它似乎不允许我。有办法做到这一点吗?

据我所知,你把你的代码放在EmailSenderGUI.Designer.cs -它应该在EmailSenderGUI.cs,例如:

partial class EmailSenderGUI
{
    //My method that I made
    private void initRecipListView()
    {
        System.Console.WriteLine("Test");
        this.recipList.Columns.Add("Recipient", -2, System.Windows.Forms.HorizontalAlignment.Left);
        this.recipList.Columns.Add("Number of Reports", -2, System.Windows.Forms.HorizontalAlignment.Left);
    }
}

然后在初始化时调用该方法,您需要处理表单OnLoad事件并从该处理程序调用该方法。

您应该尝试阅读方法注释…

/// <summary>
/// Required method for Designer support - **do not modify**
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
...
}

相关内容

  • 没有找到相关文章

最新更新