gpt4 book ai didi

c# - 无法将方法添加到要在 InitializeComponent 期间调用的 Windows 窗体

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:51 26 4
gpt4 key购买 nike

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

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 事件并从该处理程序调用该方法。

关于c# - 无法将方法添加到要在 InitializeComponent 期间调用的 Windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com