简体   繁体   中英

C#: Real-time combobox updating

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

I just had a similar problem: I had Winforms Combo box, VB.Net, Style= DropDown, and I wanted changes in the edit box to change the actual list item.

I also had multiple combo boxes that I wanted to have the same behavior.

Here's how I adapted the approach above:

Public Class frmDocEntry
    ...
    Private lastIdx As Integer = -1
    ...
    Private Sub cbAnyMV_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbRGBStringMV.Enter, cbRGBIntegerMV.Enter, cbRGBFloatMV.Enter, cbRGBDateMV.Enter
        ' comboBox.SelectedIndex will get *reset* to -1 by text edit
        lastIdx = sender.SelectedIndex
    End Sub

    Private Sub cbAnyMV_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbRGBStringMV.Leave, cbRGBIntegerMV.Leave, cbRGBFloatMV.Leave, cbRGBDateMV.Leave
        If lastIdx >= 0 Then
            sender.Items(lastIdx) = sender.Text
        End If
        lastIdx = -1
    End Sub

    Private Sub cbAnyMV_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbRGBStringMV.SelectedIndexChanged, cbRGBIntegerMV.SelectedIndexChanged, cbRGBFloatMV.SelectedIndexChanged, cbRGBDateMV.SelectedIndexChanged
        lastIdx = sender.SelectedIndex
    End Sub

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM