Wednesday, October 27, 2010

Error 11 An error occurred while establishing a connection to SQL Server instance ‘.SQLEXPRESS’. LightSwitch

It was a wonderful start for me today. I wanted to be the first to download the beta1 release of Visual Studio LightSwitch, but somehow i didn’t. Still the start was good because i had the opportunity to download and test the latest wonderful work from microsoft. Describing about it would take me hours or writing and would also not be so interesting to read. But ofcourse you definitely want to check out the two introductory videos channel9 at the following :

1. http://channel9.msdn.com/posts/Dan/Jay-Schmelzer-Introducing-Visual-Studio-LightSwitch/

2. http://channel9.msdn.com/posts/funkyonex/Visual-Studio-LightSwitch-Beyond-the-Basics/

Some of you might have found this error :

Error    11    An error occurred while establishing a connection to SQL Server instance ‘.SQLEXPRESS’.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)    C:Program Files (x86)MSBuildMicrosoftVisualStudioLightSwitchv1.0Microsoft.LightSwitch.targets    95    10    CMS

I did too. The reason is simple. You need SQLEXPRESS (Instance name also should be SQLEXPRESS) setup at the development machines because LightSwitch use it for all of it’s intrinsic data operations and authorization configurations. This ofcourse does not effect your data source configurations. Infact i have used MySQL 5.1 as my external data source and it worked as smooth like butter. I will come up with more on LightSwitch. Happy codeless programming with LightSwitch. (Though i don’t like not coding Smile)

Tuesday, October 5, 2010

WPF Color Picker Like Office 2007

main

Source code : http://customcolorpicker.codeplex.com/
Introduction

Recently i was working on a WPF application where which required a color picker for setting colors of various graph elements within the application. At first this seemed like a five minutes task for me as i was quite confident that i would get one for free in the internet. I was quite right until i was told that it should have a look and feel like that of office 2007 color picker. Thus i built my own and here it.

Features

The Color picker has the following features :

  • Choose color from a predefined list of colors
  • Choose custom color from a Color palette
  • Adjust the opacity of the selected color using a slider in custom color selection mode
  • Ease of use anywhere within a WPF application with a single bindable property for the selected color.
Implementation

The source code supplied is very much self explaining and easy to understand. So i will highlight on the points of interest. Lets begin with the how to use it in our application.

Basically the control has three files, so i have packaged it in a dll.

 

<CustomWPFColorPicker:ColorPickerControlView x:Name="ForeColorPicker" />

 

The Control has a dependency property “CurrentColor” which gets updated when a color is picked from the picker either from the predefined list of colors or the advanced color palette.


public static DependencyProperty CurrentColorProperty = DependencyProperty.Register("CurrentColor", typeof(SolidColorBrush), typeof(ColorPickerControlView), new PropertyMetadata(Brushes.Black));



The picker is a ToggleButton with little style edited to show the CurrentColor inside a . A Popup holds the predefined colors as in office 2007. These are set of button’s each representing a color via the command parameter.


<Button Click="Button_Click" Style="{StaticResource ColorButtonStyleFirstRow}"Command="{x:Static CustomWPFColorPicker:ColorPickerControlView.SelectColorCommand}" CommandParameter="#FFFFFF" Margin="3,4,3,4" Background="#FFFFFF" />


1

The “More Colors” button is used to open a model dialog containing the Advanced Picker where a user can select a color from a color palette with the mouse.


2


The color palette is a jpg image from which 4 bits are read during mousedown event, contaning the colors for ARGB.


private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try { cb.CopyPixels(_pixels, 4, 0);
UpdateCurrentColor();
UpdateMarkerPosition();
} catch {}
UpdateSlider();
}

catch (Exception) { }
}

Using these bits the color is updated :


private void UpdateCurrentColor()
{
CurrentColor = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
currentColorBorder.Background = new SolidColorBrush(Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]));
brightnessSlider.Value = 0.5;
SelectedCurrentColor = new SolidColorBrush(CurrentColor);
}


3

A slider is used to adjust the color value in order to let the user get more shades of the color selected from white to black . I have edited the style of the default WPF slider control to get the look that you see in the demo. It’s background reflects the color shades that the users will get when they change the value. For achieving this i have used a LinearGradientBrush which has 3 GradientStop at point 0, 0.5 and 1. at zero the color is white, at 0.5 the color is the selected color and at 1 it is black.


<LinearGradientBrush x:Key="BrightnessGradient" StartPoint="0,1" EndPoint="0,0" ColorInterpolationMode="ScRgbLinearInterpolation">
<GradientStop Offset="0" Color="sc# 1, 0,0,0" />
<GradientStop Offset="0.5" Color="sc# 1, 0.5,0.5,0.5" />
<GradientStop Offset="1" Color="sc# 1, 1,1,1" />
</LinearGradientBrush>



As the slider value is changed i calculate the color value based on the value of the slider. Two special cases to consider here. When the slider value is nearing 0, the color should near more to White. When the slider value is nearing 1, it should near to Black. at 0 the color becomes white and at 1 its Black. For this i ahve devised an efficient algorithm which is very simple to understand. Note that here i am not simply changing the alpha value of the color, but calculating shades of the color from white to black with the selected color and the color whose shade is being adjusted.


void BrightnessSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (_pixels == null)
{
_pixels = new byte[3];
_pixels[2] = CurrentColor.R;
_pixels[1] = CurrentColor.G;
_pixels[0] = CurrentColor.B;
}

var nc = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
var f = (float)e.NewValue;
float r, g, b;
const float a = 1;
if (f >= 0.5f)
{
r = nc.ScR + (1 – nc.ScR) * (f – 0.5f) * 2;
g = nc.ScG + (1 – nc.ScG) * (f – 0.5f) * 2;
b = nc.ScB + (1 – nc.ScB) * (f – 0.5f) * 2;
}

else
{
r = nc.ScR * f * 2;
g = nc.ScG * f * 2;
b = nc.ScB * f * 2;
}

CurrentColor = Color.FromScRgb(a, r, g, b);
currentColorBorder.Background = new SolidColorBrush(CurrentColor);
}



Source code : http://customcolorpicker.codeplex.com/

Conclusion

The most impressive part of the control is the slider which calculates shades of the selected color. I hope the control is useful to many out there who require a office 2007 style color picker.