C# 以其他形式c获取combobox的值#
我试图以另一种形式获取组合框的值,但我不会得到任何值 当我使用C# 以其他形式c获取combobox的值#,c#,combobox,C#,Combobox,我试图以另一种形式获取组合框的值,但我不会得到任何值 当我使用Form1上的MessageBox和所选值时,我得到的确实是以前的值 当我在Form2上尝试它时,我会得到一个空白的消息框,因此这样我就无法使用“IF”语句来获取所需的数据 表格1: namespace ScoreDuizenden { public partial class AantalSpelers : Form { public AantalSpelers() {
Form1
上的MessageBox
和所选值时,我得到的确实是以前的值
当我在Form2
上尝试它时,我会得到一个空白的消息框
,因此这样我就无法使用“IF”语句来获取所需的数据
表格1:
namespace ScoreDuizenden
{
public partial class AantalSpelers : Form
{
public AantalSpelers()
{
InitializeComponent();
}
public string si;
public void AantalSpelers_Load(object sender, EventArgs e)
{
NaamSpelers ns = new NaamSpelers();
cbAantalSpelers.Items.Add("2");
cbAantalSpelers.Items.Add("3");
cbAantalSpelers.Items.Add("4");
}
public void btnDoorgaan_Click(object sender, EventArgs e)
{
si = cbAantalSpelers.Text;
NaamSpelers ns = new NaamSpelers();
ns.Show();
this.Hide();
}
}
}
表格2:
namespace ScoreDuizenden
{
public partial class NaamSpelers : Form
{
public NaamSpelers()
{
InitializeComponent();
}
AantalSpelers asp = new AantalSpelers();
private void NaamSpelers_Load(object sender, EventArgs e)
{
if (asp.cbAantalSpelers.Text == "2")
{
label3.Hide();
label4.Hide();
txtNaam3.Hide();
txtNaam4.Hide();
btnAsDoorgaan.Location = new Point(16, 62);
this.Size = new Size(198, 130);
}
if (asp.cbAantalSpelers.Text == "3")
{
label4.Hide();
txtNaam4.Hide();
btnAsDoorgaan.Location = new Point(16, 88);
this.Size = new Size(198, 157);
}
}
private void NaamSpelers_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void btnAsDoorgaan_Click(object sender, EventArgs e)
{
MessageBox.Show(asp.si);
}
}
}
希望你能帮我找出我做错了什么。请看下面的代码
表格1:
namespace ScoreDuizenden
{
public partial class AantalSpelers : Form
{
public AantalSpelers()
{
InitializeComponent();
}
public string si;
public void AantalSpelers_Load(object sender, EventArgs e)
{
NaamSpelers ns = new NaamSpelers();
cbAantalSpelers.Items.Add("2");
cbAantalSpelers.Items.Add("3");
cbAantalSpelers.Items.Add("4");
}
public void btnDoorgaan_Click(object sender, EventArgs e)
{
si = cbAantalSpelers.Text;
NaamSpelers ns = new NaamSpelers(si); // passing value to form through constructor
ns.Show();
this.Hide();
}
}
}
表格2:
namespace ScoreDuizenden
{
public partial class NaamSpelers : Form
{
private string cbAntalSpelers = string.Empty; // class level field to store combo value
public NaamSpelers(string cboValue) // combo value need to be passed to this form whenever a new instance is created
{
InitializeComponent();
this.cbAntalSpelers = cboValue;
}
....
}
}
因为组合框是emtpy
AantalSpelers\U Load
不会被调用,因为您不调用show。所有这些代码都非常糟糕。您可以将第一个表单作为参数传递并使用它,而不是创建一个没有任何值的新表单Show I do this?public NaamSpelers(AantalSpelers表单)
和NaamSpelers ns=new NaamSpelers(this)代码>它不起作用:(我认为在玩winforms之前,你应该先学习c的基础知识。你需要知道什么是方法、构造函数、参数、引用等…@EmielvanLeeuwen,谢谢伙计,请在空闲时间看看链接