我一直在尝试将文本文件加载到组合框中,然后创建一个按钮将我在组合框中所做的任何更改保存回文本文件。
问题是,当我在组合框中输入某些内容时,所选的“项目”不会更新。我可以更改句子,但是一旦单击“保存”按钮(这也会更新组合框),它就会回到我编辑之前的状态。
另外,当我编辑组合框并单击下拉箭头时,它会显示文本文件的内容,再次没有我编辑的句子。
我已经搜索了一段时间,但到目前为止似乎没有人知道如何做到这一点。 :P
private void cbBanken_SelectedValueChanged(object sender, EventArgs e)
{
this.cbBanken.Update();
}
我以为类似的东西可能会起作用,但它没有任何作用。我确实在更改后设法将新项目添加到列表中,但这不是我想要的。我希望能够编辑项目,而不是添加新项目。
我希望这足够详细。感谢您的时间!
编辑:好吧,还有一件事:“它只会更新我更改的第一个字符。所以如果我在任何地方使用退格键,它就会更新,然后我必须重新启动才能再次更新。而且,它会转到最远的地方组合框线的左侧,这可能非常烦人。如果有人也知道如何解决这个问题,我将非常感激。”
我目前正在使用这段代码:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex>=0)
{
int index = comboBox1.SelectedIndex;
comboBox1.Items[index] = comboBox1.Text;
}
}
ComboBox.Update 方法只是重绘组合框区域。
据我了解,您想在运行时更改组合框所选项目。在这种情况下,您可能需要使用 TextUpdate 事件。组合框选定的索引将自动停止编辑。所以还有另一种方法。跟踪值的变化。这是一个代码片段:
private int editedIndex = -1;
private String editString = "";
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (editedIndex == comboBox1.SelectedIndex) return;
if(editedIndex>0) comboBox1.Items[editedIndex] = editString; //Change the previous item
if(comboBox1.SelectedIndex>=0) //get new item parameters
{
editedIndex = comboBox1.SelectedIndex;
editString = comboBox1.Items[editedIndex].ToString();
}
}
private void comboBox1_Leave(object sender, EventArgs e)
{
if(editedIndex>=0)
comboBox1.Items[editedIndex] = editString;
}
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
if (editedIndex >= 0)
{
editString = comboBox1.Text;
}
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData==Keys.Enter&&editedIndex>=0)
comboBox1.Items[editedIndex] = editString;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)