我建议你使用Add | New Item | Windows Form创建一个新窗体。 然后,您将获得一个设计界面,您可以添加一个列表框以及生成的代码,以正确地初始化您的窗体和列表框。特别是你的表单和列表框会获得当前没有的默认大小。然后
你的代码(比如Form1.cs中)将与此类似:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.listBox1.DataSource = new List<int> { 1, 2, 3, 4 };
}
public int? SelectedValue
{
get
{
return (int?)this.listBox1.SelectedValue;
}
}
}
另外会有一个代码负载Form1.Designer。CS类似
....
#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.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(30, 37);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
而且你可以用你的表格是这样的:
private void button1_Click(object sender, System.EventArgs e)
{
using (var form = new Form1()) // you should dispose forms used as dialogs
{
if (DialogResult.OK == form.ShowDialog()) // optional (you could have OK/Cancel buttons etc
{
Debug.WriteLine(form.SelectedValue ?? -1);
}
}
}
您正试图显示列表框而不是表单。 – ChrisF 2013-03-03 20:34:45
请参阅我的编辑@ChrisF我认为它对你所说的内容有所了解 – Cemre 2013-03-03 20:38:10
在你的FrmDelivery构造函数中仍然有's.Show();'。这是错误的。您也不会将列表框添加到可视化树中。如果你解释了为什么你不能使用XAML这可能会有所帮助。 – ChrisF 2013-03-03 20:39:30