Smallest download for the .NET Framework

Scott Hanselman built a web site that helps you to get the smallest download possible to get your .NET Framework up to date. On the site he also provides the script that developers can use to add the functionality to their own websites. It’s very useful if you provide a download of your application on your web site.

Get it at http://www.hanselman.com/smallestdotnet/

Building a SketchFlow Demo with Expression Blend 3

Building a SketchFlow demo with Expression Blend is easy. You can even add data to the demo and add navigation without writing any code.

This post shows you how to build a simple demo for searching and editing an employee database.

  1. Start Blend and select “New Project -> WPF SketchFlow Application”
  2. In the “SketchFlow Map” section, right-click on the item labeled “Screen 1″ and select “Rename”. Rename the screen to “SearchEmployees”.
  3. You will find the SketchFlow controls in the “Assets” section under “SketchFlow -> Styles -> SketchStyles”
  4. Now add the controls for the search form. You can paste the following XAML:
    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_1_Name"
        mc:Ignorable="d"
        x:Class="SketchFlowScreens.Screen_1"
        Width="260" Height="260">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <TextBlock HorizontalAlignment="Left" Margin="8,8,0,0"
             Style="{DynamicResource TextBlock-Sketch}" VerticalAlignment="Top"
             Width="79" Height="25" Text="Name:"/>
            <TextBlock HorizontalAlignment="Left" Margin="8,37,0,0"
             Style="{DynamicResource TextBlock-Sketch}" VerticalAlignment="Top"
             Width="79" Height="25" Text="Department:"/>
            <TextBox HorizontalAlignment="Left" Margin="91,8,0,0"
             Style="{DynamicResource TextBox-Sketch}" VerticalAlignment="Top"
             Width="162" Height="25" TextWrapping="Wrap"/>
            <TextBlock HorizontalAlignment="Left" Margin="8,78,0,0"
             Style="{DynamicResource TextBlock-Sketch}" VerticalAlignment="Top"
             Width="79" Height="25" Text="Results:"/>
            <ListBox HorizontalAlignment="Left" Margin="8,103,0,0"
             Style="{DynamicResource ListBox-Sketch}" VerticalAlignment="Top"
             Width="245" Height="119"/>
            <Button HorizontalAlignment="Right" Margin="0,226,8,0"
             Style="{DynamicResource Button-Sketch}" Width="64" Content="Open"
             VerticalAlignment="Top" Height="25"/>
            <Button Margin="120,226,76,0" Style="{DynamicResource Button-Sketch}"
             Content="Cancel" VerticalAlignment="Top" Height="25"/>
            <ComboBox Margin="91,37,8,0" Style="{DynamicResource ComboBox-Sketch}"
             VerticalAlignment="Top" Height="25"/>
    
        </Grid>
    </UserControl>
  5. The next step is to add data. Go to the data tab and click on the “Add sample data source” icon.

    Select “Define New Sample Data”. Call it “EmployeeDataSource”. Under “Define in”, make sure “Project” is selected, and make sure that “Enable sample data when application is running” is selected.
  6. You will see that the data source will be listed under “Project” in the Data tab.

    Right-click on “Property2″ and select “Remove Property2″.
  7. Rename “Property1″ to “Name” by clicking the text and editing it.
  8. Click on the “Edit sample values” icon next to “Collection”.
  9. Change “Number of Records” to 3.
  10. Edit the values on the grid to reflect people’s names.
  11. Add the following to the properties of the ListBox to bind it to the data source:
    ItemsSource="{Binding Collection, Mode=Default}" DisplayMemberPath="Name"
  12. You will now see items in the search results box, and this can be used to show your client what the screen will look like.
  13. Now we want to show how the user will move from the search screen to the employee property screen. Add a new screen called “EmployeeDetails” to the SketchFlow map.
    You can use the following XAML for the new screen:

    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_1_1_Name"
    	mc:Ignorable="d"
    	x:Class="SketchFlowScreens.Screen_1_1"
    	Width="260" Height="110">
    
    	<Grid x:Name="LayoutRoot" Background="White">
    		<TextBlock HorizontalAlignment="Left" Margin="8,8,0,0"
                     Style="{DynamicResource TextBlock-Sketch}"
                     VerticalAlignment="Top"
                     Width="79" Height="25" Text="Name:"/>
    		<TextBlock HorizontalAlignment="Left" Margin="8,37,0,0"
                     Style="{DynamicResource TextBlock-Sketch}"
                     VerticalAlignment="Top"
                     Width="79" Height="25" Text="Department:"/>
    		<TextBox HorizontalAlignment="Left" Margin="91,8,0,0"
                     Style="{DynamicResource TextBox-Sketch}"
                     VerticalAlignment="Top"
                     Width="162" Height="25" TextWrapping="Wrap"
                     d:LayoutOverrides="HorizontalAlignment"/>
    		<ComboBox Margin="91,37,0,0"
                     Style="{DynamicResource ComboBox-Sketch}"
                     VerticalAlignment="Top" Height="25"
                     HorizontalAlignment="Left"
                     Width="161">
    			<ComboBoxItem>Finance</ComboBoxItem>
    			<ComboBoxItem>Human Resources</ComboBoxItem>
    			<ComboBoxItem>Development</ComboBoxItem>
    		</ComboBox>
    		<Button HorizontalAlignment="Left" Margin="188,75,0,0"
                     Style="{DynamicResource Button-Sketch}" Width="64"
                     Content="Update"
                     VerticalAlignment="Top" Height="25"
                     d:LayoutOverrides="VerticalAlignment"/>
    		<Button Margin="120,75,0,0"
                     Style="{DynamicResource Button-Sketch}"
                     Content="Cancel" VerticalAlignment="Top" Height="25"
                     HorizontalAlignment="Left"
                     Width="64" d:LayoutOverrides="VerticalAlignment"/>
    	</Grid>
    </UserControl>
  14. Now we want to set up the flow in the demo. Go to the SearchEmployees screen, right-click the “Open” button, go to “Navigate to” and select “EmployeeDetails”
  15. If you run the demo by pressing F5, and you click on the “Open” button, it will now open the EmployeeDetails screen.
  16. In the same way you can let the “Update” and “Cancel” buttons in the EmployeeDetails screen navigate to the SearchEmployees screen.
  17. You can package the demo for redistribution by clicking on “File -> Package SketchFlow Project”.
  18. You can also export your screen designs to Word by clicking on “File -> Export to Microsoft Word”.
<UserControl
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ x:Name=”Screen_1_Name”
mc:Ignorable=”d”
x:Class=”SketchFlowScreens.Screen_1″
Width=”260″ Height=”260″><Grid x:Name=”LayoutRoot” Background=”White”>
<TextBlock HorizontalAlignment=”Left” Margin=”8,8,0,0″ Style=”{DynamicResource TextBlock-Sketch}” VerticalAlignment=”Top” Width=”79″ Height=”25″ Text=”Name:”/>
<TextBlock HorizontalAlignment=”Left” Margin=”8,37,0,0″ Style=”{DynamicResource TextBlock-Sketch}” VerticalAlignment=”Top” Width=”79″ Height=”25″ Text=”Department:”/>
<TextBox HorizontalAlignment=”Left” Margin=”91,8,0,0″ Style=”{DynamicResource TextBox-Sketch}” VerticalAlignment=”Top” Width=”162″ Height=”25″ TextWrapping=”Wrap”/>
<TextBox HorizontalAlignment=”Left” Margin=”91,37,0,0″ Style=”{DynamicResource TextBox-Sketch}” VerticalAlignment=”Top” Width=”162″ Height=”25″ TextWrapping=”Wrap”/>
<TextBlock HorizontalAlignment=”Left” Margin=”8,78,0,0″ Style=”{DynamicResource TextBlock-Sketch}” VerticalAlignment=”Top” Width=”79″ Height=”25″ Text=”Results:”/>
<ListBox HorizontalAlignment=”Left” Margin=”8,103,0,0″ Style=”{DynamicResource ListBox-Sketch}” VerticalAlignment=”Top” Width=”245″ Height=”119″/>
<Button HorizontalAlignment=”Right” Margin=”0,226,8,0″ Style=”{DynamicResource Button-Sketch}” Width=”64″ Content=”Open” VerticalAlignment=”Top” Height=”25″/>
<Button Margin=”120,226,76,0″ Style=”{DynamicResource Button-Sketch}” Content=”Cancel” VerticalAlignment=”Top” Height=”25″/>
</Grid>
</UserControl>

Team Foundation Server for smaller teams

On his blog, Brian Harry describes how TFS 2010 makes life easier for small teams, enabling more teams to use it.
Definitely worth a read if you are still using SourceSafe.

Remotely controlling Windows Mobile devices

Typing long text messages is a pain on most phones. Today, while typing a message that was quite long (and getting really frustrated), I decided to look for a product that I could use to control my phone from my PC.
I found a nice application called My Mobiler.
Apart from allowing you to remotely control your Windows Mobile device, it also supports copy/paste and dragging files between your PC and mobile device.

WPF: Changing the background of selected items in a ListBox

When you select an item in a ListBox, the background will be set to the default system background colour.

You can override the background by defining a style for the ListBox:

<Window.Resources>
    <Style x:Key="ListBoxStyle">
        <Style.Resources>
            <SolidColorBrush
              x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
        </Style.Resources>
    </Style>
</Window.Resources>

Now, define a ListBox, and let it use the style that we just defined:

<ListBox Style="{StaticResource ListBoxStyle}" HorizontalAlignment="Left"
        Margin="120,96,0,0" Width="151"
        IsSynchronizedWithCurrentItem="True" Height="92" VerticalAlignment="Top">
    <ListBoxItem>Item 1</ListBoxItem>
    <ListBoxItem>Item 2</ListBoxItem>
    <ListBoxItem>Item 3</ListBoxItem>
    <ListBoxItem>Item 4</ListBoxItem>
    <ListBoxItem>Item 5</ListBoxItem>
</ListBox>

The selected items will now have a red background:

You can also define a foreground colour. Add the following line to the style definition to make the foreground of selected items blue:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Blue" />

WPF : The type reference cannot find a public type named ……

I encountered this error while writing a sample app. The namespace reference was:
xmlns:local=”clr-namespace:ListviewTick;assembly=ListviewTick”
Everything looked 100% and I could not figure out why the compiler was complaining.
After some googling, I found out that you can remove “;assembly=ListviewTick”, and it was happy.

Sorting a WPF ListView

If you need to give users the ability to sort a ListView by clicking on the header, you can use the following method:

First, we will define a style for the header (this is optional, but allows you to specify the alignment of the header). Add the following to the Window.Resources section:

<Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>

Now, define the listview (for this example, we will display a list of employees):

<ListView Name="lvwEmployees">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}"
         HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="135" >
<GridViewColumnHeader Content="Name" Tag="Name"
         Click="GridViewColumnHeader_Click" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Surname}"
         HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="135" >
<GridViewColumnHeader Content="Surname" Tag="Surname"
         Click="GridViewColumnHeader_Click" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

We will use the Tag property of each GridViewColumnHeader to figure out which one was clicked.

The ListView is bound to a generic list of clsEmployee, defined as follows:

class clsEmployee
{
public clsEmployee(string sName, string sSurname)
{
_Name = sName;
_Surname = sSurname;
}
private string _Name;
private string _Surname;
public string Surname
{
get { return _Surname; }
set { _Surname = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}

Now populate the ListView with dummy data:

Employees = new List<clsEmployee>();
Employees.Add(new clsEmployee("John", "Smith"));
Employees.Add(new clsEmployee("Jenny", "Jones"));
Employees.Add(new clsEmployee("William", "Wilson"));
Employees.Add(new clsEmployee("Susan", "Brown"));
Employees.Add(new clsEmployee("Robert", "Jones"));
Employees.Add(new clsEmployee("David", "Moore"));
Employees.Add(new clsEmployee("Linda", "Taylor"));
lvwEmployees.ItemsSource = Employees;

Now for the click event on the header:

private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader ClickedHeader = (GridViewColumnHeader)sender;
Employees.Sort(delegate(clsEmployee e1, clsEmployee e2)
{
if (ClickedHeader.Tag.ToString() == "Name")
{
return e1.Name.CompareTo(e2.Name);
}
else
{
return e1.Surname.CompareTo(e2.Surname);
}
}
);
lvwEmployees.ItemsSource = null;
lvwEmployees.ItemsSource = Employees;
}

This sample only sorts the items ascending.  If you swap e1 and e2 in the comparison, the list will be sorted descending.
In this sample, the user will not be able to see the sort direction (usually indicated by an arrow on the header). I will explain how to add this arrow, using an adorner, in another post.

Upgrading to Windows 7 Using a USB Drive

One of the many cool features of Windows 7 is the ability to install it from a USB drive.
On her blog, Lynn Langit describes how she upgraded a low-end netbook from Windows XP to Windows 7.
Check it out

LINQ to Objects

LINQ to objects allows you to use LINQ queries on any collection that implements the IEnumerable interface.
This has a huge impact on developers who need to perform operations like search and sort on collections.

In order to demonstrate LINQ to Objects, we will write an application that allows the user to select a folder and an extension, and then list the files with the selected extension in the folder. Please note that providing a search pattern to Directory.GetFiles is much more efficient than the method I’ve used in this sample, but I’ve chosen to retrieve all the files first in order to filter the collection through LINQ.

I’ve chosen to create a .NET 3.5 WPF application in Visual Studio 2008.

When you’ve created the application, add a reference to System.Windows.Forms (this will allow you to use the FolderBrowserDialog class).

The XML for the window looks as follows:

 XAML

 

 

 

 

 

The following 3 functions allow the user to select the folder and populate the list of available file extensions:

private IEnumerable<FileInfo> getAllFiles(string sFolder)
        {
            //Get a list of files in the selected folder
            List<FileInfo> allFiles = new List<FileInfo>();
            string[] fileNames = Directory.GetFiles(sFolder);
            foreach (string name in fileNames)
            {
                allFiles.Add(new FileInfo(name));
            }
            return allFiles;
        }       
        private IEnumerable<string> getExtensions(IEnumerable<FileInfo> allfiles)
        {
            //Get a distinct list of file extensions
            IEnumerable<string> extensions =
                (from file in allfiles
                 orderby file.Extension
                 select file.Extension).Distinct();
            return extensions;
        }
        private void cmdSelectFolder_Click(object sender, RoutedEventArgs e)
        {
            //Select the folder to search and populate the combo box with a list of file extensions
            System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFolder.Text = folderBrowser.SelectedPath;
                cboExtensions.ItemsSource = getExtensions(getAllFiles(txtFolder.Text));
            }
        }

Now, when the user clicks the “Search” button, we will only list the files with the selected extension:

private void cmdSearch_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<FileInfo> allFiles;
            allFiles = getAllFiles(txtFolder.Text);
            IEnumerable<FileInfo> filteredFiles =
                (from file in allFiles
                 where file.Extension == (string)cboExtensions.SelectedItem
                 select file);
            lvwResults.ItemsSource = filteredFiles;
        }

Windows XP Mode

One of the coolest features in Windows 7 is Windows XP Mode. It allows you to run applications in an XP virtual machine to ensure backwards compatibility.

While this sounds like something you could do previously with virtual machines, XP mode allows for better integration with the Windows 7 environment.

XP mode does not ship with the release candidate, so you will need to download it from http://www.microsoft.com/windows/virtual-pc/download.aspx
Read the “Before You Download” section about enabling virtualisation in your BIOS, since this can save you some frustration.
Now follow steps 1 to 3 on the site to download and install the required components.

Once you have downloaded and installed all the components, you can try it out by installing an application. For this illustration I’ve installed Visual Studio 6.
When the XP virtual machine is running, it gives you direct access to your machine’s storage, so you can install it directly from your hard drive.

Install_VS

When the setup has completed, you can close the virtual machine, and get a shortcut to the installed software from your Windows 7 start menu under “Windows XP Mode Applications”.

Shortcut

When you click on the shortcut, you will see the virtual machine starting:

Launching_XP

As soon as the virtual machine is running, your application will run, while appearing to run directly in Windows 7:

Running


Twitter Updates

del.icio.us


Follow

Get every new post delivered to your Inbox.