Is Excel Present in client machine?

by munnaonc 16. August 2010 21:36
In one of our application we had to make sure that whether client have excel installed or not. We tried few codes and have some experience while resolving the solution. Bello the solution code is given to detect if client have excel 2007 installed or not.
 
The bellow code works fine. But it fails one single time. If the user installed excel but never run it. The registry keys are not built until the user run excel for the first time. so we had to change our message to inform user as “You don’t have excel or you didn’t run it after install, if you have install please run it for the first time and try again” some thing like this.
 
private static bool IsExcelPresent()
{
var key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office");
if(key!=null)
{
string[] subKeyNames = key.GetSubKeyNames();
if(subKeyNames.Contains("12.0"))
{
var excelKey = key.OpenSubKey("12.0\\Excel\\");
if(excelKey!=null) return true;
}
else
{
return false;
}
}
return false;
}

 
Eventually we got rid of the obstacle and found a very interesting component called SpreadsheetGear and used that component. we bypassed all the excel requirement using this component.
 
There are some more solutions on the web to you guys can also try those. Bellow a yet another code sample is given.
 
private static bool IsExcelPresent()
{
try
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
return officeType != null;
}
catch (Exception ex)
{
return false;
}
}

Another code sample to detect excel which using registry key search
 
private static bool IsExcelPresent()
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey excelKey = key.OpenSubKey("Excel.Application");
bool excelInstalled = excelKey == null ? false : true;
return excelInstalled;
}



Using Background Process in WPF

by munnaonc 16. June 2008 19:30

WPF applications often needs to call time consuming methods or processes, the time consuming methods or processes can be, huge time consuming calculation or perhaps a web service call. In case of WPF specially XBAP (wpf browser application) which run on remote client machines browser, this sort of calls can make your user interface unresponsive.  I am working with WPF for more than four month. Lot of things come in to way and dealt with nicely. Well in this post I am going to share a simple trick for avoiding unresponsiveness of user interface. We have already used this technique in many cases in our application, but this is for WPF.

 

The "Thread & Invoke"

 

BackgroundWorker is a very smooth and useful tool for our purpose of making more responsive UI(user interface). Before discussing more about BackgroundWorker lets take a flash back of legacy technique (which is pretty smart)  implementation of making  more responsive UI. Previously I used threading for implementing such kind of UI smoothness. What I did is to create a background thread and call expansive operation on that thread. When the job is finished use the "MethodInvoker" method to let know the UI thread that the job is finished. And this model is called asynchronous model. And this is quite smart model and rest of the models are based on this approach. Here is a quick code snippet for demonstration the technique.

//first start the method with tread
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(ExpansiveMethod);
System.Threading.Thread t = new System.Threading.Thread(ts);
t.Start();
protected void ExpansiveMethod()
{
//Very expansive call will go here...
//after the job is finished call method to update ui
MethodInvoker updaterMI = new MethodInvoker(UpdateChange);
this.BeginInvoke(UpdateChange);
}
protected void UpdateChange()
{
//again back to main ui thread
}

 

The "Background Worker"

 

Okay, its time to use the BackgroundWorker. An amazing thing about background worker is, its simple to use.  First, lets see what a background worker is. "BackgroundWorker" is a class under "System.ComponentModel" which executes an operation on a separate thread. Which is introduced from dot net framework 2.0. Things are again pretty simple just like tread, All you have to do is instantiate a BackgroundWorker and subscribe its events, and call the "RunWorkerAsync()" method. Lets put a code snippet. Since we are programmers we understand code better.

void MyMethodToCallExpansiveOperation()
{
//Call method to show wait screen
BackgroundWorker workertranaction = new BackgroundWorker();
workertranaction.DoWork += new DoWorkEventHandler(workertranaction_DoWork);
workertranaction.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workertranaction_RunWorkerCompleted);
workertranaction.RunWorkerAsync();
}
void workertranaction_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Call method to hide wait screen
}
void workertranaction_DoWork(object sender, DoWorkEventArgs e)
{
//My Expansive call will go here...
}

As you can see in above code I have subscribed two event "DoWork" (which is the main function) and "RunWorkerCompleted", In dowork event handler we will put our expansive time consuming operations, as the name imply's RunWorkerCompleted event is fired when the work is finished . BackgroundWorker also has "ProgressChanged" event which is used to let the main UI thread know how much work is completed.

 

The "Dispatcher"

 

In few cases the BackgroundWorker needs to access the main UI thread. In WPF we can use "Dispatcher" which is a class of "System.Windows.Threading" and a delegate to access the main thread. First of all we have to declare a delegate for our candidate methods, and then use the delegate to call the method using Dispatcher. Dispatcher has few thread priority and you can use various priority from DispatcherPriority enum. "Send" has the highest priority in DispatcherPriority.

//delegate for our method of type void
public delegate void Process();
//and then use the dispatcher to call the method. 
Process del = new Process(UpdateMyUI);
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, del);
void UpdateMyUI()
{
//get back to main UI thread
}

 

For more reading about this Asynchronous Programming please visit the references.

 

Reference

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

http://weblogs.asp.net/justin_rogers/articles/126345.aspx

http://www.eggheadcafe.com/articles/20050926.asp

VS templates not working? Easy to get things back.

by munnaonc 6. March 2007 20:59

Recently I had to work with window SharePoint Service 3.0 and its development. I download the visual studio 2005 project templates for SharePoint development. I downloaded the project msi from Microsoft download site.

http://download.microsoft.com/download/e/8/a/e8aa8476-5af6-4f38-aed2-0247a99d2bc6/VSeWSS.msi

Installed in my local computer… but when I opened the IDE2005 project templates are not there…..

I got confused… and try reinstall the msi… but noting happed… I went to google and found few people is also suffering from this problem… the problem is my vs2005 is installed in different drive other that “c:\” and the installed temples are in default “c:\”… and then I moved my template in the drive where the IDE2005 is installed….

Location of the project templates is [your dive letter]:\Program Files\Microsoft Visual Studio 8\Common7\IDE…

There were two directories one if project template and other is item template… last thing that I did is to make ide identity the project templates….

For this I had to just a command in vs2005 comma prompt..

devenv /installvstemplates

I found help from the following forum… please go there for more detailed discussion…

http://geekswithblogs.net/ehammersley/archive/2005/11/08/59451.aspx

Hope this will help someone someday….

Install assembly into Global Assembly Cache

by munnaonc 3. March 2007 20:52

Option One

Step One add the sn.exe to External Section of VS2005

1. Click on Menu->Tools->External Tools
2. A window with all external tool listed will popup.. click on add button
3. In title type “String Name tool”
4. In command brows for sn.exe tool [Generally in the bin directory of your sdk folder]
5. Check the use output window and prompt for argument check box…
6. Click on apply to dismiss the widow…

Now you can see that in tools menu you will have a menu item named “Strong Name Tool”. Click on the menu will launch a window asking the argument …. In the argument text box type

–k “c:\\mykeypair.snk”Click on okey to dismiss the window… in output window you will see that successful keypart creation message will be shown…

Your key is reated but not yet associated with any

Adding the key to the project to associate with assembly1. Open your project…
2. Open the property of the project…
3. You will see a tab called signning is bottom of the window… click on the signning button
4. In siginning tab check the sign the assembly check box.
5. In choose a strong name key file … click on brows …. And brows for your mykeypair.snk file…
6. save the project and build it…

That’s it your assembly is strong name signed on….

Option Two [Too Easy]

1. Open your project…
2. Open the property of the project…
3. You will see a tab called signning is bottom of the window… click on the signning button
4. In siginning tab check the sign the assembly check box.
5. In choose a strong name key file … click on new…
6. A window asking for snk file will popup… type the desiered name and click on okey to dismiss the window… save the project and build it…

That’s it your assembly is strong name signed on….

Deploy the assembly in GAC…

1. Add the gacutil.exe tool just like the sn.exe tool
2. Now open the gacutil tool from tools….
3. In the argument window type….
4. -i "$(ProjectDir)\bin\debug\DataBaseHelper.dll" or [-I “yourprojectdir\buildmode[debug or release]\bin\filename.dll”]
5. Click on okay to dismis the window..
6. In output window you will see a successful message….
7. “Assembly successfully added to the cache”

Happy programming!

Tags: , , , , , ,

.net | .net 1.1 | .net 2.2 | .net 3.0 | .net 3.5 | dll | GAC

CSS Problem of Ajax Tool Kit Calendar

by munnaonc 19. February 2007 20:35

AjaxControlTookKit is a very nice addition of Asp.net 2.0 Ajax Extension. Recently I had very nice experience using asp.net 2.0 Ajax in few of my projects. "update panel" is very use full control as far as easy Ajax is concern. I haven't explored the whole area of client side scripting, but so far the experience with the server side controls are pretty much good and satisfactory.

Problem with Calender

In one of my project, I had to use the Calendar Extender of Ajax. Well in the page level the Calender Extender worked fine. but when i put the extender in one of my usercontrol. after the page render in browser the calender popup's position is pretty strange. No deterministic behavior was found in popup. when I click on the button of the calender the popup window appeared way up where it should actually show.


I went on trying few things to fix the thing and succeed on the setting position to relative. I just put the position behavior of the parent div of the Extender to relative.


style="position:relative"

worked like cramp

Another problem

If you set the css property of the calender extender suddenly the calender loose all its default behaviour and color skim. And no detail guide line is found in the ajax.asp.net site for customizing the style of the calender control.


Since ajaxControlToolKit is under development we hope this issues will be fixed in the actual release product.

Asp.net Tree view control, very useful But.

by munnaonc 26. November 2006 20:30

Asp.net 2.0 ships with a whole new set of controls. Navigation controls are one of them. In navigation control we have three options

  1. Sitemap path
  2. Menu
  3. Tree View

TreeView

 

Recently I have worked in a project which is a demo project where I had the pleasure of using the asp.net 2.0 tree view control. Below I will be demonstrating a simple way to use the treeview and some things that we should know while we use tree view control of asp.net 2.0 in our projects.

 

Obviously tree view control has node property and nodes can be create dynamically and modify the node collection on runtime. But it’s for only level one node. For level two nodes node is created just the way it is, but need to add in the child node property. So in a sense tree view has node property and nodes have no recursive node property and to deal with child node, node has child node collection.

 

Constructor of Node class has several overloads. Actually 5 of them are there.

 

TreeNode tn = new TreeNode("Text");

TreeNode tn = new TreeNode("Text", _value);

 

Here we should always be careful about the value property. This value property must be distinct for each collection.

 

For example if we have a Tree Node consists of three Nodes and each and every done has value property as a same value. SelectedNodeChanged even returns always the first node of the collection.

 

TreeNode tn1 = new TreeNode("Node One",-1);

TreeNode tn2 = new TreeNode("Node Two",-1);

TreeNode tn3 = new TreeNode("Node Three",-1);

TreeView1.Nodes.Add(tn1);

TreeView1.Nodes.Add(tn2);

TreeView1.Nodes.Add(tn3);

 

In the web application if we select node three that is tn3 … in SelectedNodeChanged event we will have th1 as SelectedNode. I don’t know how this happened but surely it happened. But if we provide different value in the value field of the node constructor this was okay. Even if we don’t provide any value things work just fine.

 

Tree view has a wide range of configuration and style settings. Among these StaticItemStyle and StaticItemHoverStyle is most useful.

 

Well sometime we need to disable a node in our view. But TreeNode class do not have any enable or disable property, rather it has selection Action Property. We need to set the selection property value to none to make a node disabled.

Implementing Xceed GroupRow and Enabling Row Grouping

by munnaonc 19. October 2006 21:26
Xceed Grid of 3.x Version has a very interesting feature Called Grouping. It allows you to show rows in groups and in a horizontal tree structure. Now let’s discuss about how to implement Xceed grid group feature. Well Xceed grouping feature works on columns that is you must group the rows on a particular column, but you can also use recursive grouping.

To add grouping feature first you need to add a Group Object. And we need to set the property “GroupBy”. “GroupBy” property of Group object takes a string as value which represents a column name of the grid. Bellow in code we have a simple example how to add a group by row.

private Xceed.Grid.Group group;

//in declaration section

//in initialization section

grid.Columns.Add( new Column( "DEPT", typeof( string ) ) );

grid.Columns["DEPT "].Title = "Department";

grid.Columns.Add( new Column( "NAME", typeof( string ) ) );

grid.Columns["NAME"].Title = "Employee Name";

group = new Group();

group.GroupBy = "DEPT";

grid.GroupTemplates.Add( group );

grid.UpdateGrouping();

In the above example we have two column name and department. And we want to group employees by department. So groupby property of group object is set to the column name. And last of all you need to add the group object in the grid’s group template and call the update grouping method of the grid to activate the grouping.

Xceed by default adds a group header row. But we can always customize the group header and add our own customized group row. Bellow a simple example is given how to add group header row.

//in declaration section

private Xceed.Grid.GroupManagerRow gmrow;

//in initialization section

gmrow = new GroupManagerRow();

gmrow.Height = 24;

group.HeaderRows.Add( gmrow );

Xceed have GroupMangerRow for this purpose. You must add the GroupMangerRow to the header collection of the group object. And you can customize its Font, Fore Color, Title Format, Back Color, and other properties. Title format is very interesting. By default GroupMangerRow show the assigned groupby row column Title. But we can customize the Group by Header Row Title.

gmrow.TitleFormat = "%GROUPKEY% have %DATAROWCOUNT% Employees";

Here I have used only two keys to customize the Title format of the group header row. Please go to your Xceed Grid help and look for GroupManagerRow.TitleFormat property and look for more interesting feature to customized the group header row title.  

Tags: , , ,

.net | Xceed | Xceed Grid | Xceed Grid For Win Froms

Working with Xceed Cell Editor

by munnaonc 11. October 2006 21:15
In version 3.1 Xceed provided lot of interesting stuff and one of them is Xceed editors. Some most interesting Xceed editors are

Win Combo box
Option Picker

Let get started with how to define a custom cell editor. Let say we have one column that provide description of a data and the column is a multi line supported column. And we want to have custom editor for the description.

First we need to define a cell editor control, for this case this will be a text area, which is a textbox with multiline property set to true. some other property of the textbox also need to set to true for instance AcceptsReturn and WordWrap

//declaration sectionprivate TextBox cellEditorTextBox;

//initialize component

sectioncellEditorTextBox = new TextBox();

cellEditorTextBox.Multiline = true;

cellEditorTextBox.WordWrap = true;

cellEditorTextBox.AcceptsReturn = true;

cellEditorTextBox.Width = 200;cellEditorTextBox.Height = 100;

Now we need to assign the control to the column editor so that when a cell for a column is enter in
edit mode our control can be used to edit the cell. Xcced provide two manager class to deal with cell view and cell edit.
those are

CellEditorManagerCellViewerManager

//assuming that we have have a column named Description
Description.CellEditorManager = new CellEditorManager(cellEditorTextBox,"Text",false,true);

Parameters of CellEditorManager

templateControl
A reference to a Control representing the control that will be used as a template to create the controls that will edit the content of cells.

propertyName
A string representing the property used to get the control's value.
inPlace
true if the Control is painted within the bounds of the cell; false otherwise.

handleActivationClick
Indicates if the control should handle the mouse click once it is activated. Only in the case where inPlace is set to true does it make sense for handleActivationClick to also be set to true.

Defining a WinComboBox

So now we know how to deal with the custom cell editor. may be in some case we need to use a combo box in a xcced data grid to select a value for the cell. we can use custom cell editor for solving the purpose. Some issue need to consider before you actually do the implementation. Case one can be whither all your row need same set of value in the combo box or in case two you need to populate different set of items in the combo box when user enters in edit mode.

If you need only one set then initialize the combo box before assigning to the cell editor manager or if you need different set then you need to subscribe the event of a cell EnteringEdit in the entering edit you can repopulate any item collection you want.

//assuming that we have a column named Name and a win combo box named

//wincombo1private

WinComboBox  NameGridCombo = new WinComboBox();

grid.Columns["Name"].CellEditorManager = new ComboBoxEditor (wincombo1);

Configure row to show Multi line Data

well xceed do support multi line data in there cell for this we need to set some properties of the cell, but one thing must be considered if you have assigned a fixed height to a xceed row this will not work.

//three property must be set for this

Grid.DataRowTemplate.AutoHeightMode = AutoHeightMode.AllContent;

Grid.DataRowTemplate.WordWrap = true;

Grid.DataRowTemplate.FitHeightToEditors = true;

Tags: , , ,

Xceed | Xceed Grid | Xceed Grid For Win Froms | .net

Try.Catch. is not a good Practice

by munnaonc 12. September 2006 20:09
I don’t know using try catch in your code is good or bad. But I am sure that it makes your code run more slowly. For the early edge of my programming carrier I believed that c# language has managed the try catch or exception handling quite well. So we can use try catch as much as we can in our code to deal with the exception. But while working on the International Tax Expert project I found try catch is making my program working slow... this happens specially in paring a string to integer.

Let say

We have a code portion like

bool IsValidInteger(string s)
{
try
{
int x = int.parse(s);
return true;
}
catch
{
return false;
}
}

If we manage to provide an invalid string which is not an integer the program returns the accurate value that is false. And for only one case this works fine and no performance degradation occurs.

But if we have to fill a data grid which has 1000 rows and one columns value is filed using the above mentioned function to show an icon or assign formatting for the data to show on the cell. The performance hit shows, and the program run slow.

So it is better to avoid “try catch” as much as you can in your code, there is always a good way to code such things. We should always remember coding is also an art and we can make it stylish.

Our senior software Engineer Hasan Shahriar Masud provided a good example on this matter the example is given bellow.

“We should not use try-catch if we can logically conclude the exception. It will help the performance, which munna(that is me) proved.

For example

//We should not write
Try
{
C=A/B;
//Do Something 1
}
Catch
{
//Do Something 2
}
//We should write
If (B==0)
{
Do Something 2
}
Else
{
C=A/B;
//Do Something 1
}
I have investigated this on Xceed grid 3.1 in case of data convertion. May be its a problem of xceed grid.

Tags: , , , , ,

Xceed | .net 3.5 | .net | .net 1.1 | .net 2.2 | .net 3.0