Wednesday, March 24, 2010

Creating a Checked ListBox in Silverlight with C#

I'm not sure if there is already a CheckedListBox control in Silverlight 4, but creating one manually is trivial enough that such a control may be unnecessary. Adding checkboxes to a ListBox is fairly easy in Silverlight 4 with a little bit of LINQ and some event handling.

In this example, we are going to be adding a list of names to a ListBox and figure out which names have a check next to them and which don't. We will also see how to set the IsChecked property of a CheckBox using code

string[] names = new[] {"Susan", "John", "Andrew", "Bob", "Raphael", "Sam"};

foreach (var name in names)
{
CheckBox checkBox = new CheckBox();
checkBox.Content = name;
checkBox.IsChecked = name.Contains('a'); // Place a check next to names containing an 'a'
//checkBox.Click += checkBox_Click;
ListBox1.Items.Add(checkBox);
}

It's seriously that simple. However, this probably isn't useful if you can't pull any data out of it. Finding which boxes are checked and which aren't is just as simple using LINQ.


string[] GetSelectedNames()
{
List selectedNames= new List();
var checkedCheckBoxes = from CheckBox checkBox in ListBox1.Items
where checkBox.IsChecked == true
select checkBox;
foreach (var name in checkedCheckBoxes)
{
selectedNames.Add(name.Content + Environment.NewLine);
}
return selectedNames.ToArray();
}


Here is a sample:
Get Microsoft Silverlight

Saturday, March 20, 2010

Dynamically Compiling F# in .NET

If you've ever needed to dynamically compile C# or VB.NET code in your program, you've likely seen and/or used the CodeDomProvider class. However, if you try to use this class to compile F# code, you will get a ConfigurationErrorsException with the following message: "There is no CodeDom provider defined for the language."

To compile F# code dynamically, you will need to use the FSharpCodeProvider class. Using it is hardly different from using the CodeDomProvider class

First, you need to add the following reference:
FSharp.Compiler.CodeDom.dll


Then add the following using statements:
using System.CodeDom.Compiler
using Microsoft.FSharp.Compiler.CodeDom;

The code is below.
public static void CompileFSharp(string sourceCode)
{
CompilerResults compilerResults;
CompilerParameters compilerParameters = new CompilerParameters();

compilerParameters.GenerateExecutable = true;
compilerParameters.OutputAssembly = "FSharpExecutable.exe";

var fsharpCodeProvider = new FSharpCodeProvider();
compilerResults = fsharpCodeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);

if(compilerResults.Errors.HasErrors)
{
// Handle compiler errors
}
}

If I have made any mistakes in the post, please do not hesitate to inform me about them.

Friday, March 19, 2010

An introduction of sorts

A lack of idea in addition to lacking the ability to write well outside of academia have both been factors in my avoidance of creating a blog. The main problem however was a mixture of a lack of direction and being too strict on myself. At times I was unsure of what exactly I wanted to write about and other times I felt as though I was obligated to write about a certain topic even when I didn't want to. This time around, I intend on being a bit more relaxed in my writing.

Now, an extremely brief personal introduction. I am an 18 year old software developer living in Columbia, South Carolina. I enjoy writing software and learning about new technologies related to the field in general (more on this later). I am currently a sophomore in college aiming for a Bachelor's in Computer Science. This is likely a hint as to what type of content this blog will be mostly about.

My purposes for creating this blog are not unlike the common reasons people tend to give: I want to document my experiences for future reference, teach what little I know and learn what I don't, improve my writing skills - or lack thereof - and discuss the things that I find interesting on the web and in the world.

I don't believe this blog will be much different from most of the other blogs in existence, but for those that actually take the time out to read it I hope you benefit from my thoughts in one way or another.