我正在使用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.
我希望将该方法保留在该部分类中以保持清洁和可读性,但它似乎并不会让我感到满意。有没有办法做到这一点?
答案 0 :(得分:1)
据我所知,您将代码放在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
事件并从该处理程序调用该方法。
答案 1 :(得分:0)
您应该尝试阅读方法评论......
/// <summary>
/// Required method for Designer support - **do not modify**
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
...
}