Me, I think this is a horrible and terribly primitive solution and design.
Microsoft had the same idea; that's why the threaded version of the timer isn't in the WinForms designer-toolbox. The Timer from the toolbox simply raises events on the GUI-thread. No syncing required.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
Hmmmmm... I am using a System.Windows.Forms.Timer. Isn't that the one you are referring to that doesn't use a separate thread? Okay, now I'm confused. My code works if i execute it from a button. It works if I invoke it thru a delegate. Other than that nothing happens.
Perhaps I misunderstood your problem, I thought that it was that the ListBox was not automatically reflecting changes to the underlying DataSource List. The BindingList raises events that would cause the ListBox to update automatically when the List changes.
Here is a simple example. New WinForm project with two buttons and two ListBoxes. Clicking Button1 adds items to the underlying lists, but only the ListBox with the BindingList as the DataSource is update. Clicking Button2 tells ListBox1 to Refresh it's data.
publicpartialclass Form1 : Form
List<int> L1 = new List<int>();
BindingList<int> L2 = new BindingList<int>();
privatevoid Form1_Load(object sender, EventArgs e)
listBox1.DataSource = L1;
listBox2.DataSource = L2;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button2_Click);
privatevoid button1_Click(object sender, EventArgs e)
L1.Add(3); // adds a value to L1, but listBox1 does not display it until the binding is refresshed
L2.Add(33); // adds a value to L2 and listBox2 is automatically refreshed to display it
privatevoid button2_Click(object sender, EventArgs e)
// tell listBox1 to refresh the data
Kuel. I like that you can use a List<> of Key/Values for a ListBox. Did not know that. Then can use the DataMember, ValueMember flexibility. Thx. However...
You are executing the update from a button_click Method - thus the code is executed on the UI thread - thus it will work. My problem is that I am executing from a timer handler - because I need the update at regular intervals (taking samples) - thus the handler is on a different thread and thus, when executed, silently does absolutely nothing.
Thx for the code. Educational. (and 'yes' I got it working)
I have two objects, PriceRule and WeekDay, and PriceRule has a DayId column I have bound to a ComboBox column in a DataGridView. When I load the grid, if DayId is null, the combo is blank, but as soon as I drop the combo down, I have to select a day. No problem, I can inject an extra empty string day into the combo's data source, but the Id property of WeekDay is not nullable, so I need to do something as clumsy and stupid as insert a false day with Id of -1.
How do I catch this -1 and make it null before saving, and vice versa, catch a null DayId and make it -1 when loading?
Is there no other way to do what is a bloody common task that MS clearly isn't capable of handling themselves?
I am working on a heavily used winform C# application.
I have a user control, that has 3 textboxes and it can contain another user control. user can add n number of main user control on the form. due to this, if the user adds more than 20 controls, after the 20th control, the UI doesnt show anything. It is not giving any exception, its simply not drawing the controls? My application is not exceeding GDI objects