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 – ComboBox with enum description

By default, when you load enum values into a ComboBox, it’ll show the enum names. If you want to show the enum descriptions (from the [Description] attribute) instead, and still be able to get the selected enum value, you can do the following: I’ll show the code for this below. First, let’s say you have … Read more

WinForms – Bind controls to an object data source

Mapping classes to WinForm controls manually is probably the most tedious thing you can do in coding. In order to minimize this coding effort, you can bind your controls to an object data source. In this article, I’ll show how to do this in a WinForms App (.NET Core+) project. First, I’ll show step-by-step how … Read more

WinForms – Can’t edit a DataGridView column

Problem You’re using a DataGridView and you can’t edit one or more of the columns. When you click a cell, it just highlights and doesn’t enter editing mode. Check if your grid, column, or bound property are readonly. If any of these are readonly, you won’t be able to edit the values in the column. … 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

WinForms – How to remove checked items from CheckedListBox

Normally the items in a CheckedListBox are hardcoded or added programmatically (from an enum or from binding a data source). But sometimes you may want to allow the user to add and remove items from a CheckedListItem. In this article I’ll show how to add items and remove them. I’ll be working with text, and … 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

WinForms – How to remove icon from form/taskbar

Here’s the quick way to remove an icon from a WinForm: It’ll revert to the default icon. Removing an icon without the UI You may be interested in how to remove an icon without going through the Visual Studio UI. You may want to remove the icon manually, or you may be trying to figure … 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

WinForms: How to handle DataGridViewButtonColumn click event

Here’s how to handle the DataGridViewButtonColumn button click event: In this article I’ll show a step-by-step example of how to handle the button click. Example DataGridView with a button column When I click the button I want it to say Hi to the person. 1 – Set the DataSource to BindingList<Person> 2 – Add ClickHandler(Person … Read more

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 get CheckedListBox selected values

A CheckedListBox is a list control with multiple checkboxes. This allows the user to check multiple boxes at once. You can also programmatically check items in the CheckedListBox and remove them. How can I can get all the values they selected? By looping through the CheckedListBox.CheckedItems collection. See the UI and Code examples below. UI … 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

Objects added to a BindingSource’s list must all be of the same type

Problem When you’re binding a control to a list of objects by adding a BindingList to a BindingSource, you get the following exception: System.InvalidOperationException: Objects added to a BindingSource’s list must all be of the same type. Here’s the code causing this: Solution Instead of adding the BindingList to the BindingSource with Add(), set BindingSource.DataSource … Read more