Posts Tagged ‘Software’

WPF: Bind Control Enabled to Checkbox Checked

July 23rd, 2009

This is a simple example of a nifty use of binding in WPF.

Say you have an element in your application such as a TextBox, ComboBox, or some RadioButtons that you want to keep disabled to the user unless the user checks a box. You may even want to disable a whole section of controls contained inside of a StackPanel or DockPanel.

Everything enabled

To accomplish this you bind the IsEnabled property of the target control (in this case a ComboBox) to the IsChecked property of the CheckBox.


    <CheckBox x:Name="chkEnableBackup">Run Backup Sets</CheckBox>
    <StackPanel Orientation="Horizontal">
        <Label Margin="12,0,0,0">Run backup every</Label>
        <ComboBox Width="70" SelectedIndex="0"
          IsEnabled="{Binding ElementName=chkEnableBackup, Path=IsChecked}">

That’s it! Our result is that when the box is not checked the control is disabled.

Control disabled!

C#, Functions Creating Functions, with Lambda!

November 6th, 2008

So as my knowledge pertaining to the programming language C# progresses by contributions from the people around me as well as the infinite amount of resources online, I find it more and more fascinating and powerful. It’s sort of the “do-all” language, yet it still does it all in style.

Take this little concept for example. With the use of lambda expressions much like you’d find in any other functional programming language such as F#, F#’s predecessor ML, or Haskell, you can produce some pretty slick functional code right inline with everything else. This also produces an effect that is similiar (if not exactly) known as currying.

The following code uses a set of lambda expressions to create a nested function who takes an int and returns a function that matches the event signature for a Button.Click event. This allows us to dynamicly create a new function for each Button added to the form.

void AddButtons()
    // Create a brand new function that takes
    // an int and returns another function that
    // matches the definition of an EventHandler.
    Func<int, EventHandler> func =
        (x) => (sender, args) =>
    // Add a list of buttons to our control/form
    // giving each a unique Click event.
    for (int i = 0; i < 10; i++)
        Button btn = new Button();
        btn.Click += func(i);    // Add handler
        this.Controls.Add(btn);  // Add the button

I know this isn’t the most useful example or most impressive, but it’s still pretty cool and serves as a nice reference. For some more slick examples of code optimization in C# you should check out some of the posts on my buddy Erin’s blog over at Random Bits of Foo.

C#: Send Email with SMTP

September 30th, 2008

So I’ve actually had to do this before for a project I was working on, and thought that I’d go ahead and post a brief example of how to accomplish it here.

This example sends a simple email to a single recipient by using GMail. Please note that the principle classes used (MailMessage, SmtpClient, and NetworkCredential) are only available in the .NET Library 2.0 or later. For .NET < 2.0 you’ll want to see the System.Web namespace.

// Create the mail object.
System.Net.Mail.MailMessage msgMail =
    new System.Net.Mail.MailMessage(
        "",   // FROM
        ""         // TO
// Set the subject of the message
msgMail.Subject = "Hello from C#";
// Set that the body of this message will be HTML
msgMail.IsBodyHtml = true;
// Set the body of the email. Since I specified that
// I'm using HTML this will be fully qualified HTML/XHTML
msgMail.Body =
    "" +
      "" +
<h4>Hello There!</h4>
" +
 You've been selected to receive this email." +
" +
      "" +
// Now I'll create an SMTP object to send the message
// by means of a SMTP server.
System.Net.Mail.SmtpClient smtp =
    new System.Net.Mail.SmtpClient(
            "", // Server address
            587               // Server port
// GMail uses SSL
smtp.EnableSsl = true;
// GMail uses authentication
smtp.UseDefaultCredentials = false;
    = new System.Net.NetworkCredential (
        "", // Account Name
        "MyPassword"           // Account Password
// Finally, send the message!

Here’s a few extra things you might want to know about…
» Read more: C#: Send Email with SMTP

Green Sudoku, a C# App

September 27th, 2008

So I’ve written a small Sudoku game in C#. I originally started writing it so that I could have an interface to test my Sudoku Solving Algorithm. But as I worked on it, it became more and more a nice little application worthy of its own existence.

It only has 3 puzzles hard-coded into it that I’ve been using for testing purposes, but they’re fun to play, even though they’re a little on the easy side. My plan is to update the game to automatically download puzzles from some of the major Sudoku game websites that are out there. This would allow you to play literally millions of puzzles from several different levels of difficulty.

So I post it here for your puzzle-solving pleasure! Please be patient with it as it has a few bugs I’m still working out. If you happen to find any please let me know by commenting in this post!

This link will provide you with a direct download to a zip file that contains the game and a few files it needs to run. After downloading it just extract it anywhere you’d like and enjoy!

* (360KB)

Green Sudoku Screenshot

Green Sudoku Screenshot

*Note: You need to have .NET framework version 2.0 or later installed on your system. If you’re not sure whether you have it you can download and run this small program from Microsoft which will install it if it’s not present on your system.

C#: Regular Expressions

September 27th, 2008

I’ve decided to document what little knowledge I have on using Regular Expressions in C#. Nothing grand, just a list of formats, special characters and usage.

Control Characters:

Character Matches
. Any character but the newline (\n)
$ Characters at the end of a string
^ Characters at the beginning of a string. Also used in conjunction with ‘[]’ to specify “not.”
+ One or more of the specified characters
* Zero or more of the specified characters
? Zero or One of the specified characters
\ Used to escape special characters as well as signify special character sets
( ) Used to specify a collection of characters to match
[ ] Used to specify a set of single characters or ranges to match
{ } Used to specify how many times to match a given character(s)
| Used as a logical OR. Allows one or more expressions to be selected for a match

Special Character Sets:

Character Matches
\w Any word character. Same as [A-Za-z0-9_]
\W Any non-word character. Same as [^A-Za-z0-9_]
\s Any whitespace character. Same as [ \t\v]
\S Any non-whitespace character. Same as [^ \t\v]
\d Any digit. Same as [0-9]
\D Any non digit. Same as [^0-9]

» Read more: C#: Regular Expressions