Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Archive for the ‘WinForms’ Category

DataGridView set boolean cell to true in Runtime. How?

Posted by Paul on March 10, 2012

(dataGridView1.Rows[0].Cells[0] as DataGridViewCheckBoxCell).Value = true;
var dzone_url = ‘’;var dzone_title = ‘DataGridView set boolean cell to true in Runtime. How?’;var dzone_blurb = ‘DataGridView set boolean cell to true in Runtime. How?’;var dzone_style = ‘2’;

Posted in C#, WinForms | Tagged: | Leave a Comment »

How to make ComboBox highlight color to none?

Posted by Paul on February 24, 2011

In some forums I have seen developers wanted to change the selected color (usually Blue) of ComboBox to normal background color. I would like to add the code for following.

Custom Drawing

The method used for accomplishing this is Custom Drawing.


1. Create a new windows application and place a combo box on the form

2. Set the property DrawMode to OwnerDrawVariable

3. On the combo box DrawItem event, add the following code:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)


ComboBox combo = sender as ComboBox;

e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds);

e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new

SolidBrush(combo.ForeColor), new Point(e.Bounds.X, e.Bounds.Y));


Testing the Application

You can add some test data items to the combox and run the application.


Now you can see that eventhough the mouse is on the dropdown list area the highlight color is not there.

Posted in WinForms | Tagged: , , | Leave a Comment »

Zipping and Emailing Project – Using DotNetZip

Posted by Paul on January 30, 2011

What about having a utility that zips and emails your project folder content? I agree that the source control tools provides backup. But this utility can be used to easily take the backup of your private projects at home.

The Challenge

The project folder usually contains many other folders and with different file extensions. The .Net inbuilt zipping library can be used to compress a file. But not the entire file/folder structure like winzip. There are multiple choces for creating zip files like using J#/ZipPackage/Third Party/DotNetZip framework.

The Solution

My chosen solution to zip entire folder content can be the use of DotNetZip which is an open source library for zipping. There are numerous examples available in the DotNetZip download, but we trying to create a utility that will do zipping and emailing of the given folder. This project should be extensible too.

You can download the library from url:

The Design

The project has been divided into the following parts:

  • The Application UI


  • The Core Library (containing folder searching, zipping, emailing classes)



The development of the project was gone through Test Driven Development. You can examine the unit tests generated.

Classes Explained

The core class involved here is the Zipper class:


The Zipper class consists of the properties:




The folder to zip for


Exclude the folders from zipping


Exclude the following file extensions from zipping


Contains the action items to be performed after ipping

The Zipper class contains the Zip() method which is the core one:

The Zip() method performs the following actions:

  • Searches the Folder recursively and creates all files/folders list
  • Zip the file/folder list to a temporary file using DotNetZp
  • Performs the actions on zip file like Email, Copy etc.
  • Deletes the temporary zip file
  • Returns the size of the zip file

public bool Zip()


// Ensure valid folder is specified

if (!Directory.Exists(Folder))

throw new ArgumentException("Invalid Folder!");

// Get the files

var files = _searcher.GetFiles(Folder, ExcludeFolders, ExcludeFileExtensions);

// Perform zip

using (ZipFile zipFile = new ZipFile())


foreach (string file in files)


// Create output zip file path


if (File.Exists(ZipFilePath))




// Perform actions assigned (email, copy to folder etc.)

foreach (IAction action in this.AfterZipActions)


// Set file size for later reference

FileSize = new FileInfo(ZipFilePath).Length;

// Delete the original zip file


// Return true

return true;


The DotNetZip’s ZipFile class is used to do the zipping operation. The associated dll can be found in the project’s Library folder.

The Extensibility Factor

The Zipper class is designed in such a way that in future the developer wanted to add a new action – it can be easily incorporated. For this purpose the IAction interface is used.


The interface contains the Perform() method. The IAction interface is implemented by the Emailer and Copier classes. So in future a new action is required, we can create the new class implementing the IAction interface and add to the AfterZipActions list property of the Zipper class.

Gmail for Sending Mails

As gmail is supporting smtp service, if the user is having a gmail account it would be really easier. Anyway other mail services are also supported as the mail parameters can be changed over the screen. The changes will be saved to config file back when the application exits.

Saving user preferences

The config file contains default values which can be overridden by the end user.

<?xml version="1.0" encoding="utf-8" ?>



<add key="LatestFolder" value=""/>

<add key="ExcludeFolders" value="obj,bin;TestResults"/>

<add key="ExcludeFileExtensions" value="*.suo;*.zip"/>

<!–Email Options–>

<add key="SenderEmail" value=""/>

<add key="SenderPassword" value=""/>

<add key="SMTPServer" value=""/>

<add key="Recipients" value=""/>

<!–Copy Options–>

<add key="DestinationFolder" value=""/>



The recommended usage of the utility is to add to the Visual Studio Tools>External Tools menu. This would be lot easier in zipping the project while after development.

Posted in WinForms | Tagged: , , | 2 Comments »

How to edit app.config file?

Posted by Paul on January 13, 2011

The application configuration file provides flexibility in reading application settings.  But once in a while it is needed to change the settings programmatically. 

We can see how it is made possible.

Step 1: Create new Windows Forms application

Create a new windows forms application and add a config file into it.

Step 2: Add reference to System.Configuration

Add reference to System.Configuration namespace

Step 3: Add code to edit config

System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            configuration.AppSettings.Settings.Add(“font”, “Calibri”);



For editing an existing appSettings key – we have deleted and added the key.

Posted in WinForms | Tagged: , , , , | Leave a Comment »

WinForms: How to create a control from another thread?

Posted by Paul on October 28, 2010

Control.CheckForIllegalCrossThreadCalls = false; is needed for refreshing controsl from other threads.

But for creating controls and setting parent to other control which was created in another thread the following code is needed.


Create a new windows forms application and place a button with click event handler on it.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;


namespace WindowsFormsApplication1


    public partial class Form1 : Form


        public Form1()





        public delegate void DelegateDefinition();


        private void button1_Click(object sender, EventArgs e)


            DelegateDefinition delgate = new DelegateDefinition(CreateButton);

            ThreadStart threadStart = new ThreadStart(delegate { this.BeginInvoke(delgate); });

            Thread thread = new Thread(threadStart);




        public void CreateButton()


            Button button = new Button();

            button.Parent = this;

            button.Location = new Point(10, 10);

            button.Size = new Size(200, 25);

            button.Text = "Hi! I am the new button";





Posted in WinForms | Tagged: , , | Leave a Comment »

Global Exception Handler in WinForms

Posted by Paul on October 12, 2010

This is a small code snippet to set the global exception handler in Windows Forms application.

Setting a global exception handler can help us in logging the exceptions to a common file, show them to the user in a friendly way or email to the moderator.

// Place the code inside Program.cs, before Application.Run

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);


public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    // Process the e.Exception
    MessageBox.Show("I got the exception!");

Posted in WinForms | Tagged: , , | Leave a Comment »