Rajesh's Blog

My Photo
Name:
Location: Bangalore, India
Google

Tips to Reduce Abdominal Fat!

Monday, July 17, 2006

Previous instance of an application in C#

How to check whether a previous instance of an application is already running in C#?

It is possible with C# to look for previous instances of an application. Here is a method.

private static bool IsPrevInstance()
{
//Find name of current process
string currentProcessName = Process.GetCurrentProcess ().ProcessName;
//Find the name of all processes having current process name
Process[] processesNamesCollection = Process.GetProcessesByName(currentProcessName);
//Check whether more than one process is running
if (processesNamesCollection.Length > 1)
return true;
else
return false;
}

Note: Process class is available in System.Diagnostics name space

Monday, July 03, 2006

How to change the color of Tab Control (in c#)

Hi,
The Tab Control in .net does not expose properties to change its back ground and foreground color. This can be achieved by overriding the DrawItem event of the control. Following are the steps involved to do the same.
1. Set the TabControl's DrawMode to OwnerDraw
2. Handle the DrawItem event to draw things yourself
You may call the following method at the DrawItem event of tab control to test this.



private void ChangeTabColor(DrawItemEventArgs e)
{
Font TabFont;
Brush BackBrush = new SolidBrush(Color.Green); //Set background color
Brush ForeBrush = new SolidBrush(Color.Yellow);//Set foreground color
if (e.Index == this.tabControl1.SelectedIndex)
{
TabFont = new Font(e.Font, FontStyle.Italic FontStyle.Bold);
}
else
{
TabFont = e.Font;
}
string TabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(BackBrush, e.Bounds);
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
//Dispose objects
sf.Dispose();
if (e.Index == this.tabControl1.SelectedIndex)
{
TabFont.Dispose();
BackBrush.Dispose();
}
else
{
BackBrush.Dispose();
ForeBrush.Dispose();
}
}