Wednesday, December 14, 2011

Reboot from the Command Line

I often use a remote desktop connection to work on my desktop from my laptop. This is very handy, but there are some limitations. One such limitation is the lack of ability to reboot the machine you are remoting into. The option is not available within the start menu when you are remoting. There are some instances, though, when you really need to reboot. If you find yourself in this situation, you can reboot via the command line. Here is the command to do it.

shutdown /r /t 01

Happy Remoting!

Monday, November 28, 2011

How to Create a Simple Splash Screen in WPF

I have posted on this topic before, but I thought I would re-post with a step by step tutorial for those who are very new to wpf. Sorry to those of you who are more veterans. I will post more advanced topics soon, but this one’s for the newbies.

Here goes…

WPF makes creating a splash screen fairly simple. All you need to do is embed an image file into your project and change the Build Action on it.

Here is a step-by-step tutorial on how to do this.

Step 1) Create your application.
Here is the xaml for my very complicated application:


Step 2) Add an image file to your application. PNG files with transparent backgrounds are supported and are often a good choice for splash screens. Here is the image I created in Photoshop… nice huh?


Step 3) Right click on the image file in the solution explorer and click on Properties


Step 4) Set the Build Action to SplashScreen in the Properties panel


Step 5) Build and run the application.


You should now see your image splash before the application is loaded. I managed to get a screenshot as the splash screen was fading out and my application was starting to fade in.


That’s all there is to it. It is very simple. There are other ways to show and hide your splashcreen in code, but I won’t get into that in this post. I just wanted to keep it simple for those of you new to SplashScreens in wpf. Happy Splash Screening!

Friday, October 7, 2011

Wpf and the invisible checkbox (and radio button)

Just ran into another strange wpf bug. This one involves checkboxes and radiobuttons. I was working on a login screen that has a couple of checkboxes for remembering the username and password. The code looked like this…

<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
CheckBox Content="Username" Foreground="White"/>
     <CheckBox Content="Password" Foreground="White"/>

These checboxes were on a dark colored panel, so the white text showed up nicely. Running the app on my Windows 7 box worked fine, but when I ran the app on a Windows XP box, you could not see the check mark inside the box when the user clicked on it. The control still worked, but the check was just invisible. Actually, it wasn’t invisible, it was just a white checkmark on a white box (aka… invisible). This is a wpf/windows xp/checkbox bug. In Windows XP, when the foreground color is changed, the checkmark picks up that color change… why? you ask?… I don’t know. But, here is a workaround for this situation. Simply put labels with the white foreground as the content for your checkboxes. Now the foreground color of the checkbox is still the default (black) color and the checkmark will be visible for those running the app in Windows XP. Here is the modified code.

<StackPanel HorizontalAlignment="Left"  Orientation="Horizontal">
Label Content="Username" Foreground="White"/>
<Label Content="Password" Foreground="White"/>

I have also seen this issue with radiobuttons. Use the same workaround for that and you should be good to go.

I hope this helps. If it does, feel free to leave a comment. Thanks!

Friday, August 5, 2011

Launching the Default Email Client

I was asked to write some code to send an email through the default email client and I was pleasantly surprised as to how easy it was to do. I had never done this before, so it was a learning experience for me, so I thought I’d share it with all of you.

string toEmail = "";
string subject = "Test Subject";
string body = "test email message text";
string message =
string.Format("mailto:{0}?subject={1}&body={2}",toEmail, subject, body);

So that should do it. It’s pretty simple. When you run this code, you should see the default email client launch with the appropriate fields filled in.


Monday, June 20, 2011

First WP7 App

Hey everyone. I decided to try some Windows Phone 7 development to see how easy or hard it is. So far, I have found it to be pretty easy since I’m a WPF and Silverlight developer. If you are a .Net developer and are interested in mobile development, I would highly encourage you to give WP7 development a try.

Here is a very simple application I wrote just to get my feet wet with WP7 development. It’s available as a free download in the marketplace, so if you have a Windows Phone 7 device, give it a shot and let me know what you think. Again, it is an extremely simple application, but if you are a baseball fan, it should be useful.



Let me know what you think.


WPF Selected Treeview Item Background

This one took me a while to figure out, so I thought I’d share it with everyone. If you have a treeview item and don’t want the selected item to be that ugly blue color, you can easily change it.


All you need to do is add this line of code into your Treeview. In this particular example, I didn’t want the selected item to be colored at all, so I set the color to Transparent, but you can enter other colors here to suit your app’s needs.

Hope this helps. Happy coding!

SolidColorBrush Color="Transparent" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>

Wednesday, February 2, 2011

How to Properly Sort on a WPF DataGrid Column in Code

There are a lot of blog posts and articles out there on how to apply a sort to a datagrid in code, but there are very few out there that show you how to do it properly. Most of the posts I ran into show you how to apply the sort, but the column header does not update properly. The items are sorted, but the sort indicator (tiny triangle on the header)


Does not appear. This can be really confusing especially if you have already sorted by another column by clicking on the column header. This makes the sort indicator appear on that column, but if you sort by a different one in the code behind, the sort indicator still shows that the grid is sorted by the first column (Thanks WPF DataGrid! Nice one!)

Well, there is a way to get it working properly. Here it is.

        private void applySortDescriptions(DataGridColumn col, ListSortDirection listSortDirection)
            //Clear current sort descriptions

            //Get property name to apply sort based on desired column
            string propertyName = getSortPropertyName(col);           

            //Add the new sort description
            MyDataGrid.Items.SortDescriptions.Add(new SortDescription(propertyName, listSortDirection));
            //apply sort
            applySortDirection(col, listSortDirection);           
            //refresh items to display sort

        private string getSortPropertyName(DataGridColumn col)
            //place logic in here that will return the name of the property to sort by (ex: return “name”; if you are sorting by the name property)

            return string.Empty;

        private void applySortDirection(DataGridColumn col, ListSortDirection listSortDirection)
            foreach (DataGridColumn c in PatientsViewDatGrid.Columns)
                c.SortDirection = null;
            col.SortDirection = listSortDirection;

That should do it. Now you can sort and the column headers will show the sort indicator appropriately.

Monday, January 3, 2011

How to Create a Splash Screen for a WPF App

Creating a Splash Screen for a WPF application is extremely simple. Simply add your image into your project and set the build action to SplashScreen.


That’s pretty much all it takes. When you start up your application, you will see your splash screen image appear while the application is initializing.

You can also add a a new item to your project and select SplashScreen from the available templates


This will add a default image which you can then swap for your own, custom splash screen.

Yet another approach would just be to use the Splash Screen APIs. You can create a new SplashScreen object and call it’s Show method to display it and it’s Close method to, you guessed it, close it.

Happy Coding!