--- layout: post status: publish published: true title: Binding to RadioButtons in .NET Windows Forms wordpress_id: 254 wordpress_url: http://pro.grammatic.org/post-binding-to-radiobuttons-in-net-windows-forms-47.aspx date: !binary |- MjAwOC0wNC0wNiAwOTozODo1NiArMDIwMA== date_gmt: !binary |- MjAwOC0wNC0wNiAwOTozODo1NiArMDIwMA== categories: - Technology - .NET tags: - .NET - C# - Windows Forms comments: [] ---

Well, it's not security related, but I thought it was worth sharing my solution for all those people who are having trouble binding either ApplicationSettings or any other datasource to a RadioButton in the .NET Framework (C# and VB.NET) WinForms environment.

The problem is that when binding a RadioButton's Checked property clicking on a differentbutton in the set will not select the new option, but merely deselect all options. The reason for this is that the deselect event changes the datasource, the select event never fires and then the whole set are deselected.

The solution is to set the DataSource Update Mode to "Never" (in Visual Studio 2005 go to the Properties window of the RadioButton -> DataBindings -> Advanced and then toggle the value for the selected field.)

Now, this doesn't end the trouble though, because now you have to manually update your datasource in an OK button or similar. This is a problem, especially if it's an ApplicationSettings object because doing the following:

{% highlight csharp %} Properties.Settings.Default.ASetting = RadioButton.Checked; {% endhighlight %}

Will alter the settings collection and all the RadioButtons will attempt to rebind, thoroughly messing up your efforts.

Therefore, the way to do it is as follows:

{% highlight csharp %} bool aBoolean = RadioButton.Checked; Properties.Settings.Default.ASetting = aBoolean; {% endhighlight %}

Which is, obviously, incredibly tedious but does at least work. Good luck and here's hoping it gets fixed in .NET 3.5.