Saturday, February 28, 2009

Setting border at RunTime Using C#, In WPF

After two busy weeks in my office work, and learning the WPF in my new office project, Now i am back with the new article of how to apply the border at runtime, by setting the shadow, linear Gradient on the border using C#. Then on weekend i have explore and try learning more about WPF which may help me in future work.So by doing some R&D i came to a solution which i will share with you. So lets start the with the article.
For this articel i have Grid added in the window, with 3 Row so that i can add one border at one row and between 2 rows i have set one row for space. Here is the complete xaml for the window. you can see that i have use the Grid for my layout and then i have added three rows in the grid with same height.

And i have added XAML code for the border and added that border in the First row. Here is the C# code you can use to set the border at runtime.

DropShadowBitmapEffect myTestBorderEffect = new DropShadowBitmapEffect();
myTestBorderEffect.Direction = 315;
myTestBorderEffect.Color = Color.FromRgb(84, 84, 84);

LinearGradientBrush myTestBorderGradient = new LinearGradientBrush();
myTestBorderGradient.EndPoint = new Point(0.5, 1);
myTestBorderGradient.StartPoint = new Point(0.5, 0);
myTestBorderGradient.GradientStops.Add(new GradientStop(Color.FromRgb( 75, 218, 92), 1));
myTestBorderGradient.GradientStops.Add(new GradientStop(Color.FromRgb( 12, 126,4), 0));

Border TestBorder = new Border();
TestBorder.Margin = new Thickness(2, 2, 2, 2);
TestBorder.CornerRadius = new CornerRadius(5, 5, 5, 5);
Grid.SetRow(TestBorder, 2);
TestBorder.BitmapEffect = myTestBorderEffect;
TestBorder.Background = myTestBorderGradient;
In the above code what i did is to declare the DropShadowBitmapEffect object which i will use to set the shadow of my border, then i have set the Direction of the shadow in which angle the shadow is cast. I have set the 315 angle so that the shadow is shown on the bottom and right side of the border. You can set the angle of your choice. And then i have set the color property of my shadow object in which color the shadow is shown. i have choose gray color for my shadow.
Next is to declare the LinearGradientBrush object for my border object, i have declare and set the properties of my LinearGradientBrush object, First i have set the EndPoint of my linear gradient object which are 0.5 and 1 , and then i have set the starting point of my gradient object which are 0.5 and 0(zero). Next i have set the GradientStop of the Linear Gradient object. And set the color and the offset of the Gradient stop , which is in first case 1 and then set the stop color of the second Gradient stop and set the offset to 0(zero). Now my shadow and Gradient objects are ready.
Now i have to declare the border object and set the shadow and gradient object to that border object and then add that border object to the Grid row. First i declare the border object and then set the properties of the border object like i have set the Margin of the border from left, top , right and bottom. Then i set the CornerRadius of the border so that the corner of the border looks curvy. I have set 5 from each side you can increase or decrease the corner radius of the border as you like.
Now i will set the Grid row , in which row the border will be placed i have use Grid.SetRow function to set the grid row of the border, As i have only rows defined in my grid so i only set the Grid row. If you have the column defined in you grid and you want to set teh column of the your border then you can use the Grid.SetColumn function to set the border column as well. Similery if you want to set the columnSpan then you can set it by using Grid.SetColumnSpan function. And you can use Grid.SetRowSpan function to set the Row span of your control( in this case for border).
In the last three statement what i have done is to set the BitmapEffect property of the border object to the shadow object which i have declare and then i have set the background property of the border to the LinearGradientBrush object and at the end i have add the border to the my grid where i want to display the grid, as i have set the row of the border. And you can see the out put of the above work as below.

The first border is added using XAML and then second border is added using C# code, you can see that output of both the code is same.Hope you like this article and it will help you.

All and any comments / bugs / suggestions are welcomed!

Sunday, February 15, 2009

How to Get current Working Directory

In my day to day programming i came across problem how to get the current working directory in web, desktop or even if you call the dll(dynamic link library) . Let us start with the current working directory of the desktop applications.

Current Location in Desktop Application
When developing desktop application you often need the current working directory of your application, when you want to write your log file for your application in case if there is some error in you application and you want to track that errors. Here is the code how you can get the current working directory of your applications. Here is the code you can use to get the current working directory of your application.
string strEnviromentDirectory = Environment.CurrentDirectory;
string strIODirectory = Directory.GetCurrentDirectory();
string strApplicationStartPath = Application.StartupPath;
Here is the output of the above statements, you can see that the out put of the above statements are same, mean they return same value.

Current Location in Web Application
To get the current working directory in the web application is very simple one like the desktop application. Here is the code you can use to get the current working directory of your web applications.
string strServerMapPath = Server.MapPath(".");
string srtRequestMapPath= Request.MapPath(".");
string strHttpRuntimePath = HttpRuntime.AppDomainAppPath;
The above statement return same result. The Server.MapPath() actually call the Request.MapPath internally to get the current working directory. And the third statement will return same result with the forward slashs("\\") appended at the end of the path.

You can see from the image that above three statement return same values execpt that the HttpRuntime.AppDomainAppPath will append the forward slash at the end of the return string.

Current Location in DLL(dynamic Link Library)
When using Dll in your application, here is the code you can use to get the dll working directory.

string strAssemblePath = Assembly.GetExecutingAssembly().CodeBase;
string strAssemblyLocation = Assembly.GetExecutingAssembly().Location;
The Assembly.GetExecutingAssembly() will return the assembly, that contains the code that is currently executing. When you are using desktop application then both the above statement return same result, except the CodeBase will append "file:///" before the full path of the dll.
But when you are using the web application then they both return different result. Because of the following reasons.
  1. ASP.NET application will always shadow copy all the private assemblies(in bin dir) to the temporary ASPNET folder......
  2. And the Assembly.Location point to the assembly's location after it has been shadow copied.....
Using the Assembly.CodeBase instead of the Assembly.Location , CodeBase point to the original path before shadow copied. here is the MSDN document on this:

The location of the loaded file that contains the manifest. If the loaded file was shadow-copied, the location is that of the file after being shadow-copied. If the assembly is loaded from a byte array, such as when using the Load(Byte[]) method overload, the value returned is an empty
string ("").

To get the location before the file has been shadow-copied, use the CodeBase property (Source)

All and any comments / bugs / suggestions are welcomed!

Sunday, February 8, 2009

ComboBox Binding in WPF

In this short post i will try to explain you how you can bind the comboBox with data source in wpf. Let us start with our example. i have put one comboBox in XAML and the XAML of the compboBox looks like

You can see that i have set the Grid.Column and Grid.Row property of the ComboBox so that in which column of the grid and in which row the comboBox is places and i also set the name of the combox which is cboItems. Now on my code behind what i did is to set the DataContext of the combox with the name of the table to which i want to bind the comboBox , here i have created local table at runtime and assign it to the comboBox. Next set the DisplayMemberPath which is used to dispaly the Text to the user and The SelectedValuePath which is used to save the key value which is used further if you need to do some processing on the selected combox Item.

Hope you will get some idea how you can bind the comboBox with the data from the database. if you have any question please feel free to ask me.
All and any comments / bugs / suggestions are welcomed!

Loop through CheckBoxList Control in Javascript

In this post i will show you how you can access each checkBoxList Item in javascript. Here is small piece of javascript code you can use to loop through the CheckBoxList control.
function CheckAllCheckBoxListOption()
var checkBoxList = document.getElementById("<%=CheckBoxList.ClientID %>");
if(checkBoxList !=null)
var chkCheckBoxListItems= checkBoxList.getElementsByTagName("input");
for(var intCounter=0;intCounter<=chkCheckBoxListItems.lenght-1 ; )
// Item is selected
//Item is not selected by user
intCounter= intCounter+1;


In the code above what i have done is to get the checkBoxList control which i want to loop through, by using the document.getElementById and passing the checkboxList control Client Id so if you used the master page in your project then the actual id of the code is passed which also contain the master page prefix. Main statement of this small code snip is to get the ListItem from the checkBoxList which i did't by checkBoxList.getElementsByTagName("input"), using this statement i have store the number of listitem in the chkCheckBoxListItems which hold the array of the CheckboxList Items and then we can use this array variable and check the each ListItem and check whether the list Item is checked or not.

All and any comments / bugs / suggestions are welcomed!

CheckBoxList Control, 2.0

The CheckBoxList control provides a multi selection check box group that can be dynamically generated with data binding. It contains an items collection with members corresponding to individual items in the list. To determine which items are checked, iterate through the collection and test the Selected property of each item in the list. Let us start how you can use the checkboxlist control in your code and which are the important properties which you need to remember when using the checkBoxList control. Here are the important properties of the CheckBoxList control
CellPaddingThe amount of pixels between the border and the contents of the table cell
CellSpacingGets or sets the distance (in pixels) between cells.
RepeatColumnsThe number of columns to use when displaying the check box group
RepeatDirectionSpecifies whether the check box group should be repeated horizontally or vertically
RepeatLayoutThe layout of the check box group
TextAlignOn which side of the check box the text should appear
The above table show some of the important properties regarding the layout of the checkBoxList control. When checkBoxList control is render it generate the html table and place the List item. Let us start with our example code here is the image which show the html of the checkBoxList control.
From the image you can see that i have only set the cell Padding , Cell Spacing and the repeat columns. i have set the repeat column property of the checkBoxList property to 2 as i want to display the checkboxs in two column table. Here is the output of the setting, which are shown in above image

Here is the code sample which is used to bind the checkBoxList , which is show in above image.

chkMainCategoriesList.DataSource = DataSource;
chkMainCategoriesList.DataTextField = "Text column to be displayed";
chkMainCategoriesList.DataValueField = "Value Column";
In the code above the datasource is either datatable, table of a dataset or even the datareader object as we are binding to the the value from the database. Set the DataTextField property of the checkBoxList to display the text of the the checkbox and finally set the DataValueField property of the checkBoxList to save the id of the option which is used for further processing. And at the end call the DataBind() function of the checkboxList to bind the datasource values.
Now here is the code to get all the selected checkboxes in the checkBodList control when user select his/her options from the checkboxList. Here is the cod you can use to get the selected cehckBox fromt he checkboxlist

foreach (ListItem listItem in chkRequirements.Items)
if (listItem.Selected)
//place your logic here

Here is the image which show which property of the ListItem are available when you declare and use the listItem in foreach loop.

You can see that listItem have Selected property which is of boolean type, Text property which is displayed to user and the value property.

All and any comments / bugs / suggestions are welcomed!