如何在表单中链接功能?
Posted
技术标签:
【中文标题】如何在表单中链接功能?【英文标题】:How to link functions in form? 【发布时间】:2022-01-07 04:30:17 【问题描述】:我尝试这样做 func Form1_Load 将显示城市列表。 我不知道如何将城市的 3 个函数(JerusalemData()、LondonData()、OttawaData())链接到函数 FillCitiesData(); 问题是它不显示列表,而只显示街道而没有选择城市。另一个问题 - 在 HouseNum_ValueChanged 中,它不显示数字,您还可以输入字母而不仅仅是数字。 这是我的代码 - 如果有人能帮我找到问题,我会很高兴。
namespace PlanYourParty
public partial class Form1 : Form
List <City> cities;
List <string> streets;
public Form1()
InitializeComponent();
FillCitiesData();
public void FillCitiesData()
cities = new List<City>();
cities.Add(new City() Code = "123", Name = "Jerusalem" );
cities.Add(new City() Code = "456", Name = "London" );
cities.Add(new City() Code = "789", Name = "Ottawa" );
JerusalemData();
LondonData();
OttawaData();
private void Form1_Load(object sender, EventArgs e)
FillCitiesData();
cmbCity.DataSource = cities;
cmbCity.DisplayMember = "Name";
cmbCity.ValueMember = "Code";
cmbCity.SelectedIndexChanged += cmbCity_SelectedIndexChanged;
public void JerusalemData()
streets = new List<string>();
cmbStreet.Items.Clear();
cmbStreet.Items.Add("Admond Peleg");
cmbStreet.Items.Add("Pardes");
cmbStreet.Items.Add("Nayman");
public void LondonData()
streets = new List<string>();
cmbStreet.Items.Clear();
cmbStreet.Items.Add("Oxford Street");
cmbStreet.Items.Add("Piccadilly");
cmbStreet.Items.Add("Highfield Ave");
public void OttawaData()
streets = new List<string>();
cmbStreet.Items.Clear();
cmbStreet.Items.Add("Riverside Drive");
cmbStreet.Items.Add("Ontario Highway");
cmbStreet.Items.Add("Moodie Drive");
private void cmbCity_SelectedIndexChanged(object sender, EventArgs e)
if (sender != null && sender as City != null)
string code;
code = (sender as City).Code;
if (code == "123")
JerusalemData();
else if (code == "456")
LondonData();
else OttawaData();
private void cmbStreet_SelectedIndexChanged(object sender, EventArgs e)
private void domainUpDownHouseNumber_SelectedItemChanged(object sender, EventArgs e)
private void Form1_Load_1(object sender, EventArgs e)
【问题讨论】:
该街道数据属于各个城市对象。为每个城市创建实际的命名变量,并在 City 对象定义中添加List<String> Streets
属性。然后,您可以将这些数据存储在那里,而不是将其直接插入组合框中。然后您可以从cmbCity_SelectedIndexChanged
中的选定对象中获取数据。在该函数中,将sender as City
分配给一个变量,这样您就可以更轻松地使用它的属性。
【参考方案1】:
首先,我建议您将数据模型与 UI 分开。在 City
类中提取定义城市和街道所需的一切。我在示例中包含了两种返回城市的方法,以及每个城市的街道。
不需要单独的方法来做同样的事情,但使用不同的数据。
City.cs
public class City
public string Code get; set;
public string Name get; set;
public IList<string> Streets get => GetStreets(this);
public static List<City> GetCities()
var cities = new List<City>();
cities.Add(new City() Code = "123", Name = "Jerusalem" );
cities.Add(new City() Code = "456", Name = "London" );
cities.Add(new City() Code = "789", Name = "Ottawa" );
return cities;
public static List<string> GetStreets(City city)
var streets = new List<string>();
if (city == null) return streets;
switch (city.Code)
case "123":
streets.Add("Admond Peleg");
streets.Add("Pardes");
streets.Add("Nayman");
break;
case "456":
streets.Add("Oxford Street");
streets.Add("Piccadilly");
streets.Add("Highfield Ave");
break;
case "789":
streets.Add("Riverside Drive");
streets.Add("Ontario Highway");
streets.Add("Moodie Drive");
break;
default:
throw new ArgumentException("Unknown city code.");
return streets;
然后编写 UI,使控件包含由 City.GetCities()
和 City.GetSreets()
方法指定的项目列表。
CurrentCity
和CurrentStreet
以及它们在CurrentCityIndex
和CurrentStreetIndex
组合框中的相应索引返回当前选定的城市和街道。
Form1.cs
public partial class Form1 : Form
public Form1()
InitializeComponent();
protected override void OnLoad(EventArgs e)
base.OnLoad(e);
cityComboBox.DataSource = City.GetCities();
cityComboBox.DisplayMember = nameof(CurrentCity.Name);
cityComboBox.ValueMember = nameof(CurrentCity.Code);
cityComboBox.SelectedIndexChanged += CityComboBox_SelectedIndexChanged;
cityComboBox.SelectedIndex = -1; // remove default selection of 1st item.
streetComboBox.DataSource = Array.Empty<string>();
streetComboBox.SelectedIndexChanged += StreetComboBox_SelectedIndexChanged;
private void CityComboBox_SelectedIndexChanged(object sender, EventArgs e)
// TODO: Things when a city is selected.
streetComboBox.DataSource = CurrentCity?.Streets;
streetComboBox.SelectedIndex = -1; // remove default selection of 1st item.
UpdateLabels();
private void StreetComboBox_SelectedIndexChanged(object sender, EventArgs e)
// TODO: Things when a street is selected.
UpdateLabels();
public void UpdateLabels()
cityInfo.Text = CurrentCityIndex >= 0 ? $"#CurrentCity.Code CurrentCity.Name" : "Please select a city.";
streetInfo.Text = CurrentStreetIndex >= 0 ? CurrentStreet : "Please select a street.";
public List<City> AllCities get => cityComboBox.DataSource as List<City>;
public List<string> AllStreets get => streetComboBox.DataSource as List<string>;
public int CurrentCityIndex get => cityComboBox.SelectedIndex;
public int CurrentStreetIndex get => streetComboBox.SelectedIndex;
public City CurrentCity get => cityComboBox.SelectedItem as City;
public string CurrentStreet get => streetComboBox.SelectedItem as string;
屏幕截图
【讨论】:
以上是关于如何在表单中链接功能?的主要内容,如果未能解决你的问题,请参考以下文章