5

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

} 
Nick
  • 1,082
  • 1
  • 15
  • 27
  • 1
    When you "edit" the question with a small follow up question, don't rewrite the whole thing, otherwise previous answers make no sense and new comers have no idea what you are talking about. Just append your edit to the end the end of the original question. – Grant Peters Sep 21 '10 at 04:10

3 Answers3

3

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;
    }
default locale
  • 13,035
  • 13
  • 56
  • 62
  • 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 Sep 20 '10 at 12:13
0

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
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

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.

Community
  • 1
  • 1
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
  • 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 Sep 20 '10 at 11:36
  • 1
    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 Hennings Sep 20 '10 at 14:36