我有一个显示枚举项目的列表框。我想在列表框显示/表单打开时选择/突出显示当前值(从数据库中读取)。但是这段代码:
lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(lblSelectedPrinter.Text);
......不起作用。我在这里看到了一个使用“GetItemAt”的例子(Programmatically selecting Items/Indexes in a ListBox),但是我的精简版C#(.NET 1.1,C#2)没有这样的生物。
我认为这样可行:
string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblSelectedPrinter.Text = currentPrinter;
int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
listBoxBeltPrinters.SelectedItem = currentPrinterIndex;
...但它也没有(当前打印机显示在标签中,但未选中列表框中的相应条目/值)。
答案 0 :(得分:7)
我知道你已经解决了这个问题,但为什么不尝试这种经过验证的方式呢?
lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
listBoxBeltPrinters.SelectedIndex = -1;
if (!String.IsNullOrEmpty(lblSelectedPrinter.Text)) {
for (int index = 0; index < listBoxBeltPrinters.Items.Count; index++) {
string item = listBoxBeltPrinters.Items[index].ToString();
if (lblSelectedPrinter.Text == item) {
listBoxBeltPrinters.SelectedItem = index;
break;
}
}
}
这样,您知道文本更改后SelectedIndex
值设置为-1,如果在ListBox
中找到该值,则该项目已被选中。
更好的做法是在Label控件lblSelectedPrinter
触发TextChanged
事件时编写处理程序。
lblSelectedPrinter.TextChanged += new EventHandler(SelectedPrinter_TextChanged);
然后,创建如上所示的事件处理程序:
private void SelectedPrinter_TextChanged(object sender, EventArgs e) {
listBoxBeltPrinters.SelectedIndex = -1;
if (!String.IsNullOrEmpty(lblSelectedPrinter.Text)) {
for (int index = 0; index < listBoxBeltPrinters.Items.Count; index++) {
string item = listBoxBeltPrinters.Items[index].ToString();
if (lblSelectedPrinter.Text == item) {
listBoxBeltPrinters.SelectedItem = index;
break;
}
}
}
}
你已经解决了问题,所以这只是值得思考的问题。
答案 1 :(得分:2)
int i = AppSettings.ReadSettingsVal("beltprinter"); //Save it as an int.
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(i);
lblSelectedPrinter.Text = listBoxBeltPrinters.SelectedItem.toString();
答案 2 :(得分:2)
你需要它是一个整数。您可以使用int.Parse转换为将其从字符串转换为int。
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("beltprinter")));
lblSelectedPrinter.Text = listBoxBeltPrinters.SelectedItem.toString();
答案 3 :(得分:2)
lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.FindByText(lblSelectedPrinter.Text);
按值:
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.FindByValue(1);
答案 4 :(得分:2)
这有效:
string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblSelectedPrinter.Text = currentPrinter;
int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
listBoxBeltPrinters.SelectedIndex = currentPrinterIndex;
这是显示,读取和写入设置val所需的唯一代码:
private void PrinterPickerForm_Load(object sender, System.EventArgs e)
{
Type type = typeof(PrintUtils.BeltPrinterType);
foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
{
string display = field.GetValue(null).ToString();
listBoxBeltPrinters.Items.Add(display);
}
string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblCurrentPrinter.Text = currentPrinter;
int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
listBoxBeltPrinters.SelectedIndex = currentPrinterIndex;
}
private void btnSaveSelectedVal_Click(object sender, System.EventArgs e)
{
string sel = listBoxBeltPrinters.SelectedItem.ToString();
if (sel != lblCurrentPrinter.Text)
{
AppSettings.WriteSettingsVal("beltPrinter", sel);
}
}
答案 5 :(得分:2)
string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblSelectedPrinter.Text = currentPrinter;
int index = listBoxBeltPrinters.FindString(lblSelectedPrinter.Text);
listBoxBeltPrinters.SelectedIndex = index;
答案 6 :(得分:2)
这有效:
listBoxBeltPrinters.SetSelected(listBoxBeltPrinters.FindString("beltprinter"), true);
答案 7 :(得分:2)
listBoxObject.SetSelected()
和listBoxObject.FindString()
的组合是一种优雅的解决方案。它也适合我。