[英]ComboBox SelectedIndexChanged event: how to get the previously selected index?
我有一個用戶控件,它有一個 ComboBox 和一個 SelectedIndexChanged 事件處理程序。 在事件處理程序中,我需要能夠說出之前選擇的索引是什么……有人能指出我正確的方向嗎?
private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e)
{
// need to get the previously selected index and do some handling here...
// ... some handler code here ...
switch (cboTargetMode.SelectedIndex)
{
case 1: // ..... some code here...
break;
case 2: // ..... some code here...
break;
case 3: // ..... some code here...
break;
default: // ..... some code here...
break;
}
}
沒有內置任何東西,您需要監聽此事件並在實例變量中進行跟蹤。
使用 -1 作為未初始化的“最后一個索引”,因此在第一次通過時設置它但不要使用它。 后續通過您使用它並設置它。
您始終可以使用您自己的派生 ComboBox 類來執行此操作,並覆蓋OnSelectedIndexChanged
並公開一個PreviousSelectedIndex
屬性。 這樣,它就不會與表單緊密耦合。 或者,由於您可以使用事件來執行此操作,因此它也可以作為擴展程序提供程序來實現。
我想您必須將當前(稍后將成為前一個)存儲到一個變量中,以便將其用作緩存或類似的東西。
private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e) {
// need to get the previously selected index and do some handling here...
// ... some handler code here ...
// Assuming that the variable PreviousSelectedIndex is declared in the class with value -1.
if (PreviousSelectedIndex < 0)
PreviousSelectedIndex = cbo.TargetMode.SelectedIndex;
else
// Do some handling here...
switch (cboTargetMode.SelectedIndex) {
case 1: // ..... some code here...
break;
case 2: // ..... some code here...
break;
case 3: // ..... some code here...
break;
default: // ..... some code here...
break;
}
}
這是你已經想到的嗎?
否則,也許使用Control.Validating
事件? 我只是不能說這個事件是發生在SelectedIndexChanged
事件之前還是之后。 =(
comboBox_SelectionChangeCommitted 事件怎么樣? 在進行選擇后調用此方法,菜單折疊但項目未完全更改。 所以只需閱讀 comboBox.SelectedText 或 comboBox.SelectedValue 甚至 comboBox.SelectedIndex
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
int prevIndex = comboBox1.SelectedIndex;
}
這個獲取當前選定的索引,我只需要它,我找不到任何地方,所以我希望它有幫助
private void lsbx_layers_SelectedIndexChanged(object sender, EventArgs e)
{
int i = lsbx_layers.SelectedIndices[0];//selected index
MessageBox.Show("Selected item at index : " + i);
}
我有一個與此類似的問題,其中我使用以下命令將所有組合框設置為“自動完成”
ComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
我循環並設置了他們所有的 lostFocus 事件:
foreach(Control control in this.Controls)
{
if(control is ComboBox)
{
((ComboBox)control).LostFocus += ComboBox_LostFocus;
}
}
並有一個字典對象來保存舊值
public Dictionary<string, int> comboBoxOldValues = new Dictionary<string, int>();
然后最后確保該值存在或設置為舊索引,然后保存到字典
private void ComboBox_LostFocus(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.DataSource is List<YourDataType>)
{
if (((List<YourDataType>)comboBox.DataSource).Count(x => x.YourValueMember == (YourValueMemberType)comboBox.SelectedValue) == 0)
{
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBox.SelectedIndex = comboBoxOldValues[comboBox.Name];
}
else
comboBox.SelectedIndex = -1;
}
}
if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
{
comboBoxOldValues[comboBox.Name] = comboBox.SelectedIndex;
}
else
comboBoxOldValues.Add(comboBox.Name, comboBox.SelectedIndex);
if (comboBox.SelectedIndex == -1)
comboBox.Text = string.Empty;
}
您可以在DropDown
事件中獲取並存儲舊值。 然后恢復到SelectionChangeCommitted
事件中的舊值(如果有保證)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.