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: Generate a list of objects containing the enum value/description pairs. Set ComboBox.DataSource … Read more

How to bind controls to an object data source in a WinForms App (.NET Core) project

Mapping classes to UI 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

A fatal error occurred. The required library hostfxr.dll could not be found

Problem You are trying to run a .NET executable and you get the following error: A fatal error occurred. The required library hostfxr.dll could not be found.If this is a self-contained application, that library should exist in [C:\MyApp].If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use 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

C# – Global exception event handlers

There are two global exception event available in all .NET applications: FirstChanceException: When any exception is thrown, this event is fired before anything else. UnhandledException: When there’s an unhandled exception, this event is fired right before the process is terminated. You wire up these event handlers in Main() (before anything else has executed), like this: … Read more

System.ArgumentException: 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 some 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 will … Read more