C#:实时组合框更新 [英] C#: Real-time combobox updating

查看:134
本文介绍了C#:实时组合框更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图到一个文本文件加载到组合框,然后让一键拯救我做在组合框中回文本文件进行任何更改。



现在的问题是,当我在组合框中键入内容,选择'项目'没有更新。我可以改变句子,但是当我点击保存按钮,还更新组合框,它可以追溯到之前,我编辑它。



此外,当我编辑的组合框,然后点击下拉箭头,它显示的文本文件,内容再次没有我的编辑判决。



我一直在寻找一个而现在,但似乎没有人知道该怎么做为止。 :P

 私人无效cbBanken_SelectedValueChanged(对象发件人,EventArgs五)
{
this.cbBanken.Update ();
}



我觉得这样的事情可能会奏效,但它不会做任何事情。我还是设法改变它后添加到列表中的新项目,但是这不是我想要的。我希望能够修改的项目,而不是增加一个新的。



我希望这是不够详细。 !谢谢你的时间



编辑:好的,只是一件事:这将只更新第一个字符更改所以,如果我用退格键的任何地方,它更新。 ,然后我不得不重新启动前,将再次更新。此外,它会去最左边的组合框线,这可能相当烦人,如果还有人知道如何解决这个问题也一样,我会非常gratefull。



我目前使用此代码:

 私人无效comboBox1_TextChanged (对象发件人,EventArgs五)
{
如果(comboBox1.SelectedIndex> = 0)
{
INT指数= comboBox1.SelectedIndex;
comboBox1.Items [指数] = comboBox1.Text;
}

}


解决方案

ComboBox.Update方法只重绘组合框区域。
我的理解,要更改运行系统中的组合框中选择的项目。在这种情况下,你可能想使用TextUpdate事件。 ComboBox中选定的索引是自动停止编辑。因此,有另一种方式。跟踪值更改。下面是一个代码片段:

 私人INT editedIndex = -1; 
私人字符串editString =;
私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs五)
{
如果(editedIndex == comboBox1.SelectedIndex)回报;
如果(editedIndex大于0)comboBox1.Items [editedIndex] = editString; //改变以往项目
如果(comboBox1.SelectedIndex> = 0)//获取新项目的参数
{
editedIndex = comboBox1.SelectedIndex;
editString = comboBox1.Items [editedIndex]的ToString();
}
}


私人无效comboBox1_Leave(对象发件人,EventArgs五)
{
如果(editedIndex> = 0)
comboBox1.Items [editedIndex] = editString;
}

私人无效comboBox1_TextUpdate(对象发件人,EventArgs五)
{
如果(editedIndex> = 0)
{
editString = comboBox1.Text;
}
}

私人无效comboBox1_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.KeyData == Keys.Enter&放大器;&安培; editedIndex> = 0)
comboBox1.Items [editedIndex] = editString;
}


I have been trying to load a text file into a combobox, and then making a button to save any changes I make in the combobox back to the text file.

The problem is, when I type something in my combobox, the selected 'item' doesn't get updated. I can change the sentence, but as soon as I click the 'save' button, which also updates the combobox, it goes back to before I edited it.

Also, when I edit the combobox and click the dropdown-arrow, it shows the content of the text file, once again without my edited sentence.

I've been searching for a while now, but nobody seems to know how to do it so far. :P

private void cbBanken_SelectedValueChanged(object sender, EventArgs e)
{
    this.cbBanken.Update();
}

I thought something like that might work, but it doesn't do anything. I did manage to ádd a new item to the list after changing it, but that's not what I want. I want to be able to edit the items, not add a new one.

I hope this is detailed enough. Thank you for your time!

Edit: Alright, just one more thing: "It will only update the first character I change. So if I use backspace anywhere, it updates, and then I have to restart before it will update again. Also, it will go to the far left of the combobox line, which can be pretty annoying.. If anyone knows how to fix that too, I'd be really gratefull."

I am currently using this code:

private void comboBox1_TextChanged(object sender, EventArgs e) 
{ 
    if(comboBox1.SelectedIndex>=0) 
    { 
        int index = comboBox1.SelectedIndex; 
        comboBox1.Items[index] = comboBox1.Text; 
    } 

} 

解决方案

ComboBox.Update method just redraws the combobox area. As I understood, you want to change the combobox selected item in runtime. In that case you might want to use TextUpdate event. Combobox selected index is automatically stops the editing. So there is another way. Tracking of the value changing. Here is a code snippet:

    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;
    }

这篇关于C#:实时组合框更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆