我有六个组合框放在我的表格上,同时我希望用户可以从最多两个组合框中选择值。 对于这个我有一个计数器变量,每当组合框的selectedindex改变但没有达到解决方案时,它会增加。
你们能解释我怎么能这样做?
答案 0 :(得分:1)
我为你创造了一个例子。我希望它有用。
ASP.Net页面:
<form id="form1" runat="server">
<div>
<div>
<label>Combobox 1</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-1-1</asp:ListItem>
<asp:ListItem>Combo-1-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>Combobox 2</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-2-1</asp:ListItem>
<asp:ListItem>Combo-2-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>Combobox 3</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-3-1</asp:ListItem>
<asp:ListItem>Combo-3-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>Combobox 4</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-4-1</asp:ListItem>
<asp:ListItem>Combo-4-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>Combobox 5</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-5-1</asp:ListItem>
<asp:ListItem>Combo-5-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<label>Combobox 6</label>
<asp:DropDownList runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" AutoPostBack="True" Height="19px">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Combo-6-1</asp:ListItem>
<asp:ListItem>Combo-6-2</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</div>
</form>
ASP.Net Code-Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["DropdownSelectLimit"] = 0;
}
}
protected void Unnamed1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["DropdownSelectLimit"] != null)
{
if ((int)ViewState["DropdownSelectLimit"] < 2)
{
int count = (int)ViewState["DropdownSelectLimit"];
count++;
ViewState["DropdownSelectLimit"] = count;
}
else
{
DropDownList dropDown = (DropDownList)sender;
dropDown.SelectedIndex = 0;
Label1.Text = "You can't select any option anymore";
}
}
}
如果您有任何问题,请在评论时问我:)
答案 1 :(得分:1)
我为Winforms创建了一个设置,因为如果你想要Winforms或ASP.Net,它还没有完全清楚
我不会处理你应该如何填充组合框,我确定你知道怎么做,无论是静态还是动态填充。
我的解决方案(虽然非常粗鲁)使您可以将名为 HandleComboBoxSelectedIndexChanged 的简化处理程序链接到每个组合框SelectionChanged事件。这可以是SelectedItemChanged,SelectedValueChanged或SelectedIndexChanged。
非常重要:每个组合都需要一个空值。
//will hold a list of your combobox names
private List<string> SelectedCombos;
//form load: Link the events to the correct handler
private void Form_Load()
{
foreach(var combo in Controls)
{
if(combo is ComboBox)
combo.SelectedValueChanged += HandleComboBoxSelectedIndexChanged;
}
}
//will handle the selectedIndexOrItemchanged event
private void HandleComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
if(SelectedCombos = null)
SelectedCombos = new List<string>();
var combo = (ComboBox)sender;
if(combo.SelectedValue == null)
if(!SelectedCombos.Exist(combo.Name))
SelectedCombos.Remove(combo.Name);
else
if(!SelectedCombos.Exist(combo.Name) && SelectedCombos.Count < 2)
SelectedCombos.Add(combo.Name)
SetComboAvailability();
}
另一个可能有用的功能,无论何时选择2个组合,都会使启用状态为false。这意味着您的用户只能从2个选定的组合中进行选择,直到其中一个组合被重置为空位置。这样,您的用户只能选择可能的内容。
//set the combos availability, less then 2 selections ok, own selection also ok else not ok
private void SetComboAvailability()
{
foreach(var combo in Controls)
{
if(combo is ComboBox)
combo.Enabled = SelectedCombos.Count < 2 || SelectedCombos.Contains(combo.Name);
}
}
下面是一个小例子,说明如何根据名称从组合中检索所有值。
//Something for handling all the values
private void RetrieveValues()
{
foreach(var v in SelectedCombos)
{
var combo = this.Controls.Find(v);
if(combo is ComboBox)
{
//do something with the selectedValue
var val = combo.SelectedValue; // or selecteditem
MessageBox.Show(string.Format("Control {0} has value {1}", combo.Name, val);
}
}
}
如果你不明白,可以随意提问。
此外:
EventArgs属性可能会根据所选事件而改变:
执行:_comboBox1.SelectedValueChanged + =(VS中的TAB TWICE)以查看自动代码生成的样子。 (总是很方便)
示例:
_comboBox1.SelectedValueChanged += _comboBox1_SelectedValueChanged;
private void _comboBox1_SelectedValueChanged(object sender, EventArgs e){}