Giardino
Giardino

Reputation: 1387

Can't add method to windows form to be called during InitializeComponent

I am creating a GUI with Visual Studio 2013 in C#. I am using the built-in designer and when creating my GUI I added a listView object that I want to contain 2 columns. I have the following code:

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

As you can see, I made the method initRecipeListView but when I try to run the code I get the following error in the design window:

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

I want to keep that method inside that partial class for cleanliness and readability, but It doesn't appear that it will let me. Is there a way to do this?

Upvotes: 2

Views: 1045

Answers (2)

Chris Ballard
Chris Ballard

Reputation: 3769

As far as I can see you are putting your code in EmailSenderGUI.Designer.cs - it should be in EmailSenderGUI.cs eg:

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

And then to call that method on initialisation, you need to handle the form OnLoad event and call the method from that handler.

Upvotes: 1

bastos.sergio
bastos.sergio

Reputation: 6764

You should try reading the method comments...

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

Upvotes: 0

Related Questions