问题描述
我已完成以下代码,以在第一个组合框中显示与选择相对应的列表。 但是我的第二个列表没有加载第一个列表中的项目:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedindexChanged(object sender,EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedItem == "weekdays")
{
comboBox2.Items.Add("Sunday");
comboBox2.Items.Add("Monday");
comboBox2.Items.Add("Tuesday");
}
else if (comboBox1.SelectedItem == "year")
{
comboBox2.Items.Add("2012");
comboBox2.Items.Add("2013");
comboBox2.Items.Add("2014");
}
comboBox2.Show();
}
private void Form1_Load(object sender,EventArgs e)
{
comboBox1.Items.Add("weekdays");
comboBox1.Items.Add("year");
}
private void Form2_Load(object sender,EventArgs e)
{
comboBox2.Show();
}
}
}
我做错了什么?
解决方法
我不确定为什么会有两种形式,请尝试使用以下代码,看看是否适合您。
if (comboBox1.Text == "weekdays")
{
comboBox2.Items.Add("Sunday");
comboBox2.Items.Add("Monday");
comboBox2.Items.Add("Tuesday");
}
else if (comboBox1.Text == "year")
{
comboBox2.Items.Add("2012");
comboBox2.Items.Add("2013");
comboBox2.Items.Add("2014");
}