我决定将form1设计器中的listBox1项更改为public static而不是private,以便在另一个表单上访问它。一旦我做了它,它就会出错并且不允许我改变表单上的内容,只是在顶部告诉我这个错误
The variable 'listBox1' is either undeclared or was never assigned.
错误显示在以下行
this.panel1.Controls.Add(listBox1);
我的设计师的表格如下:
namespace Mercury.Forms
{
partial class Inside
{
/// <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);
}
#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.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
Inside.listBox1 = new System.Windows.Forms.ListBox();
this.label3 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Image = global::Mercury.Properties.Resources.pooltablegodass_500x750;
this.pictureBox1.Location = new System.Drawing.Point(12, 11);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(54, 48);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label1.Location = new System.Drawing.Point(74, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 20);
this.label1.TabIndex = 1;
this.label1.Text = "Ash Smith";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label2.Location = new System.Drawing.Point(76, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 13);
this.label2.TabIndex = 2;
this.label2.Text = "ashleytechie";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(listBox1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 348);
this.panel1.TabIndex = 3;
//
// listBox1
//
listBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
listBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
listBox1.ForeColor = System.Drawing.SystemColors.Window;
listBox1.FormattingEnabled = true;
listBox1.ItemHeight = 15;
listBox1.Items.AddRange(new object[] {
"Jake Simpson",
"Adam Jones",
"Jane Peters",
"Ashley Smith",
"Sam Smith",
"Joe Phelan",
"Peter Black",
"Tracy Bratford",
"Jamie Harrison",
"Peter Slims"});
listBox1.Location = new System.Drawing.Point(16, 107);
listBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
listBox1.Name = "listBox1";
listBox1.Size = new System.Drawing.Size(144, 180);
listBox1.TabIndex = 3;
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.ForeColor = System.Drawing.SystemColors.Window;
this.label3.Location = new System.Drawing.Point(12, 79);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(105, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Contact List";
//
// panel2
//
this.panel2.Location = new System.Drawing.Point(205, 7);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(518, 335);
this.panel2.TabIndex = 4;
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.button1.Location = new System.Drawing.Point(16, 304);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(170, 31);
this.button1.TabIndex = 5;
this.button1.Text = "Edit Profile";
this.button1.UseVisualStyleBackColor = true;
//
// Inside
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.ClientSize = new System.Drawing.Size(730, 347);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "Inside";
this.Text = "Inside";
this.Load += new System.EventHandler(this.Inside_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Panel panel1;
public static System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
}
}
答案 0 :(得分:2)
将此更改回原先的版本:(公开私有,删除静态)
public static System.Windows.Forms.ListBox listBox1;
在你的表单中的代码隐藏中添加它:(你不需要一个setter,因为你不会从其他表单创建一个新的ListBox ...我希望) < / p>
public ListBox MyListBox
{
get { return listBox1; }
}
现在在其他表单中(假设它们是由 first 表单中的某个进程创建的),您将传递对表单的当前实例的引用,其中包含ListBox,因此您实际上可以访问刚创建的公共财产:
public class SecondForm : Form
{
public SecondForm(FirstForm firstForm)
{
// do something with firstForm.MyListBox
}
}
最后,我要小心你做了多少。能够访问彼此控件的表单对我来说似乎很麻烦。如果您添加了有关您要完成的具体内容的更多详细信息,我们可能会提供更好的替代方案。
答案 1 :(得分:1)
您的问题是您更改了设计器文件中的代码,因此listBox1
现在是静态的。您无法执行此操作,因为设计人员必须能够实例化ListBox
的实例。
您尝试做的事情是尝试从其他表单修改列表框。你应该这样做的正确方法是通过属性。在您的代码中(不是设计器代码,而是代码背后的代码),请使用以下代码:
public ListBox MyListBox
{
get
{
return listBox1;
}
}
答案 2 :(得分:0)
您不应该更改.Designer.cs
文件中的任何内容。如果要更改访问修饰符,则应通过“属性”窗口进行更改。
话虽如此,您public
的更改很好,但您无法做到static
。对于设计者来说,表单中的所有控件必须独立于每个实例。
// what the code will look like when you change it from the Properties window
public System.Windows.Forms.ListBox listBox1;
如果您要制作ListBox
static
,则可以像
ListBox listbox = Inside.listBox1;
如果您有多个Inside
形式浮动,那么这没有多大意义。如果您将其设为非static
,只要您拥有Inside
类的实例,您仍然可以从任意位置访问它。
Inside inside = new Inside();
ListBox listbox = inside.listBox1;