Tuesday, September 7, 2010

Code: No source control? Use DropBox

Recently I was working on a small development project that was only semi-work related.  I didn't want to use my work's source control since it was non-work and I didn't need versioning, just backup.  I created the project in my Dropbox folder and it worked really well to backup and keep the project synced across machines.

Wednesday, August 25, 2010

Code: FullText Search in SQL Server 2008 Express

In order to do full text search in SQL Server 2008 Express (Original or R2) you have to install the "Advanced Services". You can find the download for original here or R2 here.




After upgrading your current instance (or installing the new instance) you also need to make sure the Full Text service is enabled

You can then follow this guide to add a full text catalog and index.

If you are using SSMS and want the full text index scripts to be generated when you generate the create script for a table, you'll need to modify a setting.  Go to  Tools | Options - Scripting page and then scroll down to table options and select "Script full-text indexes". This will script the full-text indexes with the table definition.

If you are using the scripts then in a Visual Studio Database project, don't forget to set the Build Action property of the file to "Build", otherwise it will drop the indexes when it deploys

Tuesday, August 17, 2010

Code: Silverlight Navigation from a UserControl

For some reason Silverlight does not provide the ability to access a page's NavigationService from a user control, so in order to navigate correctly you have to manually walk the control tree. See the method below to use as a helper class.

public static class NavigationHelper
    {
        public static void Navigate(Uri url, UserControl control)
        {
            Page pg = GetDependencyObjectFromVisualTree(control, typeof(Page)) as Page;
            pg.NavigationService.Navigate(url);
        }

        private static DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
        {
            //Walk the visual tree to get the parent(ItemsControl)
            //of this control
            DependencyObject parent = startObject;
            while (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                    break;
                else
                    parent = VisualTreeHelper.GetParent(parent);
            }
            return parent;
        }
    }

Monday, August 2, 2010

Code: Create Thumbnail from Video using VLC command line

If you want to create a thumbnail from a video, you can use VLC and its command line arguments as seen below. This was tested using VLC 1.1.2

C:\Users\amiller.NG>"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" --video-filter scene -V dummy --scene-width=80 --scene-format=jpeg --scene-replace --scene-ratio 24 --start-time=6 --stop-time=7 --scene-path=D:\ --scene-prefix=thumb D:\1.mp4 vlc://quit

--scene-path is the output directory
--scene-prefix is the output filename
--scene-format can be jpeg, jpg, or png
--start-time is the location in the video (in seconds) to grab the image from
--stop-time tells the vlc to stop playing the video at that point
--scene-width specifies the output width of the thumbnail in pixels, you can optionally include --scene-height, but the image will be stretched to fit that dimension.

Thursday, June 24, 2010

Code: WPF Binding a TreeView to an Array of Complex Objects

I’m taking my first stab at WPF as a learning exercise and figured out how to bind an array of objects (with sub-objects) to a TreeView control. 

First, here is the resulting output:

image

 

This TreeView binds to an Array of type ClientWithAssets, which contains an array of Asset and a Client as properties. 

image

Step 1: make sure your sub-objects are public properties and not members! (I was stuck for an hour because of this).

Step 2: Drop a TreeView control on your window and define the hierarchy:

        <TreeView HorizontalAlignment="Stretch" Margin="12,41,12,12" Name="treeView1" VerticalAlignment="Stretch" ItemsSource="{Binding}" >
<
TreeView.ItemTemplate>
<
HierarchicalDataTemplate ItemsSource="{Binding Assets}">
<
TextBlock Text="{Binding ClientItem.ClientName}"/>
<
HierarchicalDataTemplate.ItemTemplate>
<
DataTemplate>
<
TextBlock Text="{Binding Name}"/>
</
DataTemplate>
</
HierarchicalDataTemplate.ItemTemplate>
</
HierarchicalDataTemplate>
</
TreeView.ItemTemplate>
</
TreeView>


Step 3: Bind on window load:



private void Window_Loaded(object sender, RoutedEventArgs e)
{
treeView1.ItemsSource = ClientWithAssets.FindAll();
}

There are many, many ways to perform this same functionality, but this is what made most sense to me.

Thursday, June 17, 2010

Code: Create Properties for ASP.Net Web Form Pages

When developing web form pages I like to create properties to interact with user input controls that are more complex than simple strings (Enums, DateTime, etc).  This allows me to keep more of my page strongly typed.

using System;

namespace WebFormsSamples
{
public partial class PageProperties : System.Web.UI.Page
{
public enum PrimaryColors
{
Red,
Blue,
Yellow
}

public PrimaryColors SelectedColor
{
get
{
PrimaryColors result;
Enum.TryParse<PrimaryColors>(ColorInput.SelectedValue, out result);
return result;
}
set
{
ColorInput.SelectedValue = value.ToString();
}
}

public DateTime SelectedDate
{
get
{
DateTime result = DateTime.MinValue;
DateTime.TryParse(DateInput.Text, out result);
return result;
}
set
{
DateInput.Text = value.ToShortDateString();
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindControls();
SelectedColor = PrimaryColors.Yellow;
SelectedDate = DateTime.Today;
}
}

/// <summary>
///
Notice the strong types here!
/// </summary>
protected void SaveInput_Click(object sender, EventArgs e)
{
SaveItem(SelectedColor, SelectedDate);
}

private void BindControls()
{
ColorInput.DataSource = Enum.GetNames(typeof(PrimaryColors));
ColorInput.DataBind();
}

private void SaveItem(PrimaryColors color, DateTime date)
{
lblResult.Text = SelectedColor.ToString() + " " + date.ToLongDateString();
}
}
}

Tuesday, June 8, 2010

Code: Encoding Videos using Expression Encoder

I wrote an article for MSDN magazine about Expression Encoder, check it out at http://msdn.microsoft.com/en-us/magazine/ff714558.aspx. This is my first article pretty exciting!