Tuesday, September 15, 2009

How to know if your code is run by the visual studio designer?

While developing a windows application in .net (using c# or VB) we generally use the design view to add and position controls, thanks to the great interface provided by Microsoft.

But many a times while working with custom controls or user controls, you may want the designer to behave in a different way in the design view than while running the application.

For example, say that you have created a custom control extending System.Windows.Forms.Button control to MyButton and specifying that all the buttons created using this class must have same background and same image.

Now say that you a created a from with 100 buttons created using the MyButton class. The design view would take a lot of time to load the 100 buttons and their corresponding images.

As you have created buttons from the MyButton class, you already know the image that would be set and the background color. So you always need not have both the image and the background color set while designing the form, this can be done in the runtime , so that your designer would load quickly.

The .NET framework provides a way to do this. The System.Windows.Forms.Control has a property "DesignMode", this property tells us if the control is in design mode or is in runtime.

But this property may not always be accurate. While using custom controls, this property may not behave as expected.

So how do we actually know if the code is running in Visual Studio or is in runtime?

I stumped over an idea that the base process for the design to be displayed should be visual studio, so a conditional statement can be used to check if the current process is visual studio. This can be done by using the System.Diagnostics.Process.GetCurrentProcess.ProcessName, this property gives the name of the current running process. This can be checked with the visual studio process name ( which is devenv).

So we can check if the code is being run by visual studio designer or by the application by using the following code:

VB:

if System.Diagnostics.Process.GetCurrentProcess.ProcessName <> "devenv" then
' code if not run in visual studio
else
' code when run in visual studio.
end if

c#
if(System.Diagnostics.Process.GetCurrentProcess.ProcessName != "devenv")
{
// code if not run in visual studio
}
else
{
// code if run in visual studio.
}



Hope this helps.

Saturday, August 29, 2009

ClickOnce Deployment of windows applications

Hi Geeks,
 
Today I was able to implement ClickOnce deployment of windows applications, and thought it would be helpful if I blog this. Wondering what is ClickOnce, well it cannot be told in a single word.
 
Ever came across a situation in which you had to manually install application in each and every node of a network? It is acceptable when the nodes are limited say around 5 - 10 nodes. But imagine a network having 50 or 100 nodes, located on different floors or even different buildings, and you need to install the application manually in each and every node, and you need to install in each and every node for every change ( either minor or major ) you have made to the application. 
To move around installing the application itself is tiring. Wouldn’t it be nice if we change something on the server and it reflects on all nodes? 
This can be achieved using ClickOnce deployment. ClickOnce deployment is, as the name suggests click once to publish the application and whoever is using the application get the newly published version automatically.
Anyway coming back to clickonce, I did not explore completely, so spare me if I am wrong anywhere. I am just putting the steps I have taken to implement ClickOnce.
 
Open the project properties and select Debug tab. In this make sure that the Configuration is set to Release or Active (Release), and Platform to Any CPU.
 
Now go to the Signing tab and check the Sign the ClickOnce manifests checkbox. You can sign the assembly if you desire.
 
Then go to security tab and check “This is a Full Trust Application.”
 
Then go to publish tab. In this you need to specify
•    publish path / location
•    installation URL ( optional )
•    Install Mode and Settings
•    Application Files
•    Prerequisites
•    Updates
•    Options
•    Publish version
 
Let’s go one at a time now.

Publish path / Location:
This is the location where the application is published, i.e. a setup file is created.
This is also the location where a client checks for updates.
 
Installation URL (optional)
This can be understood by seeing the publish screen
Install Mode and Settings:
Select the desired option, but I recommend to make the application available both offline and online.

Application Files:
Here we need to remember that the Application.ExecutablePath is neither \bin\debug or \bin\release, its always the root directory, so place any dependent files from debug \ release in the root directory.
Click on Application Files button and you can see the list of all the files that would be published.
Verify that all the files required are present in the list. If you know that some are not needed then you can exclude them by selection from the combobox.

Prerequisites:
Make sure all the necessary Prerequisites are checked, i.e. if your application uses crystal reports make sure Crystal reports is checked.

Updates:
The updates screen is self explanatory.
I would recommend to you to select before the application starts radio button.

Publish Version:
Make sure that Automatically increment revision with each publish is checked.
 
Now click on publish button, and you should be done.
 
Once you install the application from the publish location on every node, it will automatically check for updates from the publish location every time the application starts.
 
Hope I have covered everything. Please tell me if I missed out anything.
Happy publishing

Friday, August 28, 2009

My First Post

Hi all,

This is my first blog, and I have started blogging today. Watch out this space for my interests in Web  and other technologies. Will definitely keep in touch and will post as regularly as possible.