WinForms – Loop through a form’s controls

Forms also have a collection of controls (Controls property) that you can loop through. This is useful for when you want to do something to multiple controls and don’t want to have to manually type out code to deal with individual controls. Here’s an example of looping through a form’s top-level controls: Note: In the … Read more

WinForms – Date and time input

When you need to let the user select a date and/or time of day, you can use the DateTimePicker control: You can set the control properties in the UI or programmatically: If you don’t set an initial value, it’ll default to DateTime.Now (at the time the code is executed). The value the user picked is … Read more

Complex DataBinding accepts as a data source either an IList or an IListSource

If you try to set a list control’s DataSource to a type it can’t handle, then you’ll get the following exception: System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource. (Parameter ‘value’)at System.Windows.Forms.ListControl.set_DataSource(Object value) Note: This applies to all controls that subclass ListControl, such as ComboBox and ListBox. This is … Read more

WinForms – How to prompt the user for a file

In a WinForms project, you can prompt the user to select a file by using the OpenFileDialog control: When you call ShowDialog(), it’ll prompt the user to select a file: When the user clicks Open, you’ll be able to get the file path they selected from the OpenFileDialog.FileName property. To use the OpenFileDialog control, drag … Read more

C# – DataGridView index out of range exception

Problem When you’re using WinForms and click a DataGridView column header, you get an exception like this: Index was out of range. Must be non-negative and less than the size of the collection. This problem is caused by the column header “row” triggering a click event (such as CellContentClick) with event args containing RowIndex of … Read more

Invoke or BeginInvoke cannot be called on a control until the window handle has been created

Problem In a WinForms project, if you try to call Invoke/BeginInvoke before the window handle is created, you’ll get the following exception: System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created Because this exception happens while the form is initializing, it typically results in the form not … Read more

C# – Fill a dropdown with enum values

When you need to show enum values in a dropdown (ComboBox control with DropDownStyle=DropDown), it’s a good idea to automatically populate the list, instead of manually setting all of the values. To fill the dropdown with the enum’s values, set the DataSource to Enum.Values(), like this: Read more about how to show the enum’s Description … Read more

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 you’re binding to is empty. Solution Are you expecting there to always be data? If you’re expecting there to … Read more

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