我有三个相关的复选框列表。 1.国家 2.国家 3.城市 如果在国家/地区核对清单中选择了特定国家/地区,我想列出所有州。同样,如果我选择任何州,则应在城市复选框列表中填写相应的城市。
我为每个国家/地区创建了单独的函数,并使用以下代码调用它们:
private void Country_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string s in Country.CheckedItems)
{
if (Country.CheckedItems.Contains("US"))
{
US_States_Checked();
}
if (Country.CheckedItems.Contains("Canada"))
{
Canada_States_Checked();
}
}
}
public void Canada_States_Checked()
{
string[] canada_states = new string[12];
canada_states[0] = "Alberta";
canada_states[1] = "British Columbia";
canada_states[2] = "Manitoba";
canada_states[3] = "New Brunswick";
canada_states[4] = "Newfoundland and Labrador";
canada_states[5] = "Northwest Territories";
canada_states[6] = "Nova Scotia";
canada_states[7] = "Ontario";
canada_states[8] = "Prince Edward Island";
canada_states[9] = "Quebec";
canada_states[10] = "Saskatchewan";
canada_states[11] = "Yukon Territory";
State.Items.AddRange(canada_states);
}
private void Country_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string s in Country.CheckedItems)
{
if (Country.CheckedItems.Contains("US"))
{
US_States_Checked();
}
if (Country.CheckedItems.Contains("Canada"))
{
Canada_States_Checked();
}
}
}
public void Canada_States_Checked()
{
string[] canada_states = new string[12];
canada_states[0] = "Alberta";
canada_states[1] = "British Columbia";
canada_states[2] = "Manitoba";
canada_states[3] = "New Brunswick";
canada_states[4] = "Newfoundland and Labrador";
canada_states[5] = "Northwest Territories";
canada_states[6] = "Nova Scotia";
canada_states[7] = "Ontario";
canada_states[8] = "Prince Edward Island";
canada_states[9] = "Quebec";
canada_states[10] = "Saskatchewan";
canada_states[11] = "Yukon Territory";
State.Items.AddRange(canada_states);
}
我有以下问题: 1.当复选框为UnChecked时,使用哪个属性进行检测? 2.如何检查所选州/国家的名称,检查是否已检查?类似的东西:
问题很简单,但有点棘手。
由于
答案 0 :(得分:0)
每个国家/地区的单独功能都是非常糟糕的主意。您应该在代码之外存储数据。考虑使用XML或数据库或JSON来存储数据。您可以阅读有关XML序列化here和here的信息,例如
将数据从文件加载到内存后,您可以将其放在字典中,如
var states = data[country.selectedItem].Keys;
最后一部分 - 将所选项目传递到您的词典:
var cities = data[country.selectedItem][state.selectedItem];
和
country
我认为state
和CheckBoxList
是CheckBoxList
个对象的名称。
至于SelectedIndices
用法 - 只需实现你的逻辑,所有需要的方法都被描述为here。 (我仍然不确定,你在使用什么:WPF或WinForms或者什么,但你可以找到合适的文档)。
SelectedItems
和var selectedItems = country.SelectedItems;
if (selectedItems.Contains("Canada")
{
//Do what you need
}
返回checkBoxList的所选元素。
您可以这样检查:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
.
.
.
app.UseCors(_CorsOptions.Value);
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
map.RunSignalR(new HubConfiguration { EnableDetailedErrors = true });
});
}
private static Lazy<CorsOptions> _CorsOptions = new Lazy<CorsOptions>(() =>
{
return new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context =>
{
var policy = new CorsPolicy();
foreach (string allowedCOrsOrigin in ConfigurationManager.AppSettings["AllowedCORS-Origins"].Split(',').ToArray())
{
if(!string.IsNullOrEmpty(allowedCOrsOrigin) && !policy.Origins.Contains(allowedCOrsOrigin))
{
policy.Origins.Add(allowedCOrsOrigin);
}
}
policy.AllowAnyMethod = true;
policy.AllowAnyHeader = true;
policy.SupportsCredentials = true;
return Task.FromResult(policy);
}
}
};
});
}