Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:24:40+00:00 2026-05-27T16:24:40+00:00

I am making C# / WinForms application. The problem I couldn’t solve (yet) is

  • 0

I am making C# / WinForms application. The problem I couldn’t solve (yet) is that when I change the SelectedItem of ComboBox programatically, it is changed until the ComboBox loses the focus, after that it “reminds” its value before assigning the SelectedItem. I think that it takes the old value from binding source. When choosing an item using UI the underlying bound object is updated normally but it doesn’t so when I’m assigning new value to SelectedItem programatically.

Just for additional info: I am trying to implement “undo”, which means I am saving every change somewhere and when Edit>>>Undo I’m reversing all these changes done by user. Interesting thing is that the other controls (TextBoxes, NumericUpDowns) work fine.

Here are the Details:

I have a ComboBox which I bind to the ComboItem object like this:

ComboBox comboBox = new ComboBox();
List<ComboItem> items = new List<ComboItem>();
ComboList comboList = Configuration.ComboList.LoadComboList();

Combo myCombo = comboList.GetCombo(control.MemberName);
if (myCombo != null)
{
    items.Add(new ComboItem(0, "", 0.0, 0.0));
    for (int index = 0; index < myCombo.ComboItems.Count; index++)
    {
        items.Add(myCombo.ComboItems[index]);
    }
}

where Combo and ComboList are custom classes for loading the data from configuration file. Then I set the Display and Value members and also DataSource as well:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Key";
comboBox.DataSource = items;

“Text” and “Key” are members of ComboItem class:

public class ComboItem
{
    public int Key { get; set; }
    public string Text { get; set; }
    public double Coef1 { get; set; }
    public double Coef2 { get; set; } 

    public void CopyValues() {...}
    public override bool Equals() {...}
}

Now the problem: when executing undo I check everything needed to have all the cast operations safe and clear and trying to “undo” with this code:

Logger.Info(controls[0], op, "ExecuteUndo");
((ComboBox)controls[0]).Focus();
((ComboBox)controls[0]).SelectedItem = (ComboItem)op.GetOldValue();
Logger.Info(controls[0], "AFTER CHANGE");

Logger is just logging. op object is taken from undo sequence and it gives appropriate value using “GetOldValue()”. That code actually IS AFFECTING the UI, but until the control loses its focus. It happens on next undo which should affect other control and thus let this combobox to lose the focus.

I am sure that this happens on comboBox_LostFocus event because first thing I do on this event is Logging and it already shows me the value that SHOULDN’T BE.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:24:40+00:00Added an answer on May 27, 2026 at 4:24 pm

    I think the problem you are seeing is that the ComboBox is displaying one value, but hasn’t written the value to the binding source yet (which doesn’t happen until you lose focus).

    You can try doing something like this to write the data when an item is selected (assuming there is just the one databinding associated with the ComboBox):

    private void comboBox_SelectedIndexChanged(object sender, EventArgs e) {
      comboBox.DataBindings[0].WriteValue();
    }
    

    And just to make sure, you either subscribe to this event from the designer, or wire it up manually:

    public Form1() {
      InitializeComponent();
      comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a WinForms app with a ListView set to detail so that several
I've got a C# winforms application that runs in the background, listening for hotkeys
I am making Winforms application. I have put gridview in usercontrol. Which is another
I have a .NET application that was written in C# and VB.NET using WinForms.
I'm trying to modify a C# WinForms application that uses multiple forms. At startup,
I'm making my own commentbox control that inherits from a winforms textbox. One of
Using VB.net 4.0 I have a Winforms application that is loosely based on MVVM.
I have a winforms app that is making asyncronous WebClient calls with a callback
I am making an application in C# which uses a winform as the GUI
Making an adobe flex ui in which data that is calculated must use proprietary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.