C# – Auto-populate a dropdown with enum values

When you need to show enum values in a dropdown, it’s a good idea to automatically populate the list, instead of manually setting all of the values. To autopopulate the dropdown, set the DataSource to Enum.Values(), like this: Then to get the option the user picked, do the following: When I launch my form, I … Read more C# – Auto-populate a dropdown with enum values

ArgumentOutOfRangeException: InvalidArgument=Value of 0 is not valid for SelectedIndex

Problem Let’s say you’re initializing a ComboBox like this: And you get the following exception: System.ArgumentOutOfRangeException: ‘InvalidArgument=Value of ‘0’ is not valid for ‘SelectedIndex’. (Parameter ‘value’)Actual value was 0.’ You’re getting this exception because the DataSource is empty. Solution Are you expecting there to always be data? If you’re expecting there to always be data, … Read more ArgumentOutOfRangeException: InvalidArgument=Value of 0 is not valid for SelectedIndex

WinForms: How to handle DataGridViewButtonColumn click event

There’s not a direct way to handle the DataGridViewButtonColumn button click event. Instead, you have to: Handle the DataGridView.CellContentClick event. Check if they clicked the button column. Execute the button click handling logic, passing in the bound data item. In this article I’ll show a step-by-step example of how to handle the button click. Example … Read more WinForms: How to handle DataGridViewButtonColumn click event

WinForms: How to check if another form is open

If you’re working on a Windows Forms project and need to know which forms are open, use: This gives you an IEnumerable collection of form objects that are currently open. You can lookup a form by name, by type, or loop through the list. Example scenarios There are many scenarios where you’d want to know … Read more WinForms: How to check if another form is open

Event-driven .NET: How to add an event to a class

In the previous article we saw how events work. In this article I’ll show how to add an event to a class. In order to illustrate this I’ve created a simple chat app that uses an in-memory message queue. Each chat window is subscribed to the message queue’s OnMessageReceived event, and updates their UI upon … Read more Event-driven .NET: How to add an event to a class

Event-driven .NET: how events work

The very basis of event-driven programming is the concept of events. Events are an implementation of the Observer Pattern. With the Observer Pattern you have an observable entity and entities that want to observe that entity. Objects with events are observable, and other objects subscribe to those events and are notified when the events happen. … Read more Event-driven .NET: how events work

How to update UI from another thread

I often need to be able to run multiple threads and update the UI based on the results. For example, I may need to execute GET requests to 10 different endpoints concurrently, and then report their results in a datagrid as they come back. The problem is you can’t just update the UI from any … Read more How to update UI from another thread