Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, April 27, 2014

Displaying the column names for MS Project in C#


Whilst, MSDN provides a very updated and informative developer reference for MS Office  suite in general; it seems to have missed out the same for MS Project .

I was recently developing a MS Project addin in C# using VSTO, and came across several challenges ; but I am happy to say I completed the addin in due time.

The following code displays the name of the columns in the currently open project .

   MSProject.Project activeProject  = Globals.ThisAddIn.Application.ActiveProject;
    MSProject.Table prjTable = prjTable.TaskTables[activeProject  .CurrentTable];

    for (int i = 1; i <= prjTable .TableFields.Count; i++)
    {
         MSProject.PjField field = objTable.TableFields[i].Field;
         string colName  = Globals.ThisAddIn.Application.FieldConstantToFieldName(field).ToString();
        MessageBox.Show(colName);

    }
         
    if (activeProject  != null)
    {
        Marshal.ReleaseComObject(activeProject  );
        objProject = null;
    }

    if (prjTable != null)
    {
       Marshal.ReleaseComObject(prjTable );
       prjTable = null;
    }


There are two other points that should be noticed in this post :-

a) Usage of for loop instead of foreach loop. According to OOM.NET: Part 2 - Outlook Item Leaks , foreach  loops causes a memory leak while for does not. I am following this principle for all the Office addins.

b) Usage of Marshal.ReleaseComObject - Its usage is mandatory as all the objects being dealt with are COM objects

.

Saturday, May 04, 2013

Analayze your code with Ndepend

Ndepend is a static code analyzer  for .NET projects. It performs analysis of the project codebase;  and provides metrics, graphs and reports ; all of which helps a developer to enhance the code quality. I would like to  thank Patrick , for developing it, and also for providing a licence to me.

Post code analysis, Ndepend provides several tools to better your code . 

1) Dependency Graph - As the name implies, the graph  displays the caller and the callee in the project. This is important as it provides a birds eye view , when the code base is large.

2) Queries and Rules explorer - This is an amazing tool , which displays areas where the code needs improvement. The explorer addresses several sections - architecture, design considerations, dead code etc . I am providing a snapshot below :-



Once a rule is selected ; the query and rules editor automatically creates a CQLinq syntax , executes the query and displays the result. Alliteratively , the developer can also write a query ( for those not covered by Ndepend ) and get respective results.

3) Metrics - The Metrics window displays the usage and relevance of the code being analyzed.  The developer can use the metrics, to view where are the methods being called directly or indirectly. Besides methods, developers can also view metrics of namespaces , fields  ; which come in very handy to understand calling sequences and workflow in large projects.

4) Dependency Matrix - The dependency matrix is a visual representation of the methods being used, and the methods using them . I am posting a screenshot below .



The following is the representation of the green and blue colour .

The Matrix Cell is Green because the method (in column) is used by the method (in row). 

The Matrix Cell is Blue because the method (in column) is using the method (in row). 

More features can be found here.  Overall I find it a great tool ; I shall update this post once I come across new features.

Wednesday, April 04, 2012

Enabling and disabling Office Addins programatically using C#

The following code is an example for enabling / disabling Outlook addins.

I have used the Interop.Outlook as I was developing for Outlook. If you want to do this for other Office applications, eg, Word , Excel, Powerpoint, please reference the respective interop.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Core;
using Microsoft.Office;
using Microsoft.Office.Interop.Outlook;


private static void Connect(bool bStatus)
{
  // More code
   
objComAddin.Connect = bStatus;

}
}

Monday, August 29, 2011

Hiding the scroll bar in Checklistbox

Recently I was working on a checklist box control, which was disabled  but did not prevent the vertical scroll bar from appearing. As I had to to keep the checklist box disabled,  I added a panel  on to the checklist box, but still the horizontal scroll bar was visible. Please see the comparison in the picture.



The only resolution I could come was to hide the scroll bar of the checklist box from appearing. I wrote the following code and instantiated an object of it, and it did the trick.

Thursday, November 25, 2010

Implementing Timeout feature in C#

The timeout feature is a handy tool in computer programming , when the user wants to execute some code  after a specific time period . Normally the timeout feature can be acheived by the Sleep command. The following code has a sleep for 2 seconds; also note that Sleep blocks the calling thread .

if (timeout > 0 )
  Threading.Sleep(2000);


Now, what if the user wants to do a polling - there are several options like Timers, WaitforSingleObject , and many others according to your needs . The code I am providing below , also implements the timeout feature using the StopWatch class .

Sunday, February 14, 2010

Adding folders to a custom Outlook store

In continuation to my earlier post on creating an Outlook store, today I shall show you how to create custom folders for the store .
I started off by trying the MSDN way, and am providing the code below. The myStore refers to the Outlook.MAPIFolder which represents the custom store.

myStore.Folders.Add("First Folder", olFolderInbox);
myStore.Folders.Add("Second Folder", olFolderInbox);


While the first line created the folder for me , the second line greeted me with an execption "Invalid argument type" . I tried replacing the olFolderInbox parameter with the other permissible parameters of  OlDefaultFolders, but without success .It took me some time to solve this problem , but what I learnt was interesting.

If you look at the Folders.Add(Name,Type ) method you shall see that the Type is an optional parameter, and if you have not worked with Office Automation earlier in C#, you might be wondering how to populate the default parameter; and there was the problem .

Sunday, January 24, 2010

Creating a Outlook store programatically

Office programming has become much easier with Visual Studio for Office. Though the object model still needs to be know, but there is less of COM plumbing, and helper classes to make life easier .
Among the entire Office suite family, Outlook is probably the most used , and hence users prefer addins which can connect to other external applications, be it CRM or fetching data from web service and displaying in the Outlook dashboard
If you are really serious about Office Development,  you should get a copy of Add In Express, which makes office development very easy .  The Addin Express team has been kind enough to provide a 10% discount to the readers of this blogs and so you can grab a copy of Add In Express ( Standard , Professional, Premium ) at http://www.add-in-express.com/purchase/index.php using the discount coupon ADX-GSUJAY-ADX10.

The following code creates a MAPIStore programmmatically. I have used C# as the language. Though I have worked with MAPI , ( Simple and Extended MAPI ) with C++/COM, if you dont need Extended MAPI , C# is just fine . But again remember , that the entire Office Suie is unmanaged code, and C# is managed code , so you may have to do marshalling sometimes .


Outlook.NameSpace olNamespace = OutlookApp.GetNamespace("MAPI");
// Get the Default Inbox folder
Outlook.MAPIFolder olMapiFolder = olNamespace.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Outlook.MAPIFolder olCustomFolder;               
string path = "C:\\mydata.pst";
// Create a store

Wednesday, December 23, 2009

Develop Office Addins in a breeze – Part 1

Five years back I was working on a product, which included Word automation. The task was simple ; let the user create a new Word document from within the product . The product was being developed in Visual C++ 6.0 and there was no VSTO . The key idea behind this feature was Office integration .  I had done this using Word automation and COM plumbing with the Word Object Model.
If you are new to Offiice Development ;  and interested in getting a quick overview, I would recommend VSTO 3.0 for Office 2007 Programming. On the other hand if you want to get more deep ,  I would recommend Visual Studio Tools for Office 2007: VSTO for Excel, Word, and Outlook .
Why Office Add - Ins
Things have changed in the last 5 years. Visual C++ 6 has been replaced by the Visual Studio suite, Windows Programming has become much easier, .NET has evolved,  global economy has gone north and new innovative and useful products have made development easier.
Maybe if I was developing the same product today, the concept might have been different . My boss would have probably told me , “Hey , why not develop a product specific menu  in the Word application itself, so the user can directly talk to our app while using Word”. This approach makes much sense, as it not only addresses our need, but also makes the user feel more comfortable ( he doesn't have to start our application to access  the  specific features )
So I would have landed developing a Word Add-in , maybe using Visual Studio Professional Edition ( Extensibility –> Shared Add In Project )  or Visual Studio for Office ( haven’t used it ever ) .

Thursday, March 12, 2009

Adding/Removing dynamically created controls in .NET/C#

There are times when one needs to create and remove controls ; e.g switching from one tab to other, and showing up relevant settings for the tab.

I have found various examples of adding dynamic controls, but removing dynamically created controls are not there.

Adding dynamic controls is easy ;one can find the needful in the InitializeComponent( ). I am giving a example below :-

public void AddControl( )
{
Button myButton = new Button() ;
myButton.Location = new Location ( 10,20);
myButton.Size = new Size(100,25);
myButton.Name = "testButton";
myButton.Text = "Dynamic Button";
myButton.Click += new EventHandler(myButton_Click);
// If you are adding the button to a
// earlier defined GroupBox control, mygroupBox, you can add it as shown below
mygroupBox.Controls.Add(myButton);
}