views:

139

answers:

2

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

} 
A: 

What if you create a property in code-behind and bind to that property?

First win is better debuggability, second win is that you can decide what to do when getting / setting the data.

Martin
I'm sorry, but I'm using Visual Studio 2008 Express Edition, and I have no access to anything else at the moment.So, I don't think I can use WPF? -sorry, I'm a bit new to all this, I haven't coded much in C# yet.
Nick
I'm using Visual C# 2008 Express Edition and I can select File -> New Project -> WPF Application. About 6 months ago I started a project in WPF with lots of "someElement_Click"-code and "Panel.Children.Add(new Button())"-code but then taught myself some [MVVM](http://msdn.microsoft.com/en-us/magazine/dd419663.aspx). It was quite a relief - had to write more code in some places, but everything got quite structured.
Martin
+1  A: 

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;
    }
MAKKAM
You'r the best! That code works, finally! The only problem I'm having now, is that 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.
Nick
Rechecked, it's my fault. I'll recheck the working solution
MAKKAM
Answer updated. Hope it helps.
MAKKAM