Thursday, January 29, 2009

.Net Application Single Instance

If you want to ensure that your application only runs one instance at a time, the best method is to use a Mutex.

Mutex: Short for mutual exclusion object. In computer programming, a mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. When a program is started, a mutex is created with a unique name. After this stage, any thread that needs the resource must lock the mutex from other threads while it is using the resource. The mutex is set to unlock when the data is no longer needed or the routine is finished.

On your program.cs unit, do the following:

  
static class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
bool createdMutex = false;
Process currentProcess = Process.GetCurrentProcess();

Mutex mutex = new System.Threading.Mutex(true,
currentProcess.ProcessName + currentProcess.MachineName + " Mutex", out createdMutex);

if (createdMutex)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());

GC.KeepAlive(mutex);
}
else
{
// Application to topmost window
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id != currentProcess.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}

Tuesday, January 20, 2009

Exception of type 'System.ComponentModel.Design.ExceptionCollection' was thrown

Sometimes I get the message "Exception of type 'System.ComponentModel.Design.ExceptionCollection' was thrown" When trying to open a Form in designer view.
The real problem of the "ExceptionCollection" being thrown is that when there is a WSOD (White Screen of Darn) indicating a designer load issue, the designer gets unloaded. These get caught by the unload method and get displayed in the dialog box you see.
So, to fix this you should:
* Attach a visual studio debugger to VS. Turn on exception catching when first thrown (in the Debug|Exceptions menu).
* Open the designer with the debugger attached
* Determine what component is throwing the exception.

Sometimes you cannot open the form design view because of a missing resource file or an invalid reference in the design code unit.

Thursday, January 8, 2009

Enable remote connection to SQL Server 2005 Express

How many times did you had the following error on SQL Server 2005 Express, "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connection". Check this post on Linglom’s blog and a step by step solution for the problem.

Another solution is to go to SQL Server Configuration Manager on Microsoft SQL Server 2005 folder at Start menu, under Configuration Tools. Select SQL Server 2005 Services, and change the State and Start Mode of SQL Server Browser item to Running and Automatic.

Hopefully it will solve your problem ;-)

Monday, November 10, 2008

Unable to open Server Explorer in Visual Studio

When server explorer doesn't open when clicking on the menu option View|Server Explorer, the first solution is to run the command /ResetSkipPkgs on the visual studio command prompt. Open a development command line window by clicking on the Start|Programs|Microsoft Visual Studio 2008|Visual Studio Tools|Visual Studio 2008 Command Prompt shortcut.
Type devenv /resetskippkgs in there. What /ResetSkipPkgs does is that it clears all options to skip loading added to VSPackages by users wishing to avoid loading problem VSPackages, and then starts Visual Studio. The presence of a SkipLoading tag disables the loading of a VSPackage; clearing the tag re-enables the loading of the VSPackage.

If this doesn't work, then reinstall the visual studio again.

Wednesday, October 15, 2008

Add any custom controls or Windows Forms controls to a ToolStrip

When we are in need to put some custom controls or Windows Forms controls to a ToolStrip and we think that we only have the options gave to us from the ToolStrip control, we need the help of the ToolStripControlHost Class. This class Hosts custom controls or Windows Forms controls.

Declare a ToolStripControlHost field. In the constructor of the Windows form and after InitializeComponent();, create a new ToolStripControlHost with your required controller in the argument. Then add the ToolStripControlHost to your ToolStrip Items.

It's that easy!!

  
private ToolStripControlHost toolStripControlHost;

public WindowsFormConstructor()
{
InitializeComponent();
toolStripControlHost = new ToolStripControlHost(dateTimePicker1);
toolStrip1.Items.Add(toolStripControlHost);
}

Saturday, October 11, 2008

Break Strings in Excel

So many times we needed to import an excel file to a database, receives an excel file filled with people full names, and we need to break it in two other columns regarding the first name and the last name, or even the two together.
This is the way to do it:

First String: =LEFT(A1,FIND(" ",A1,1))

Last String: =RIGHT(A1;LEN(A1)-FIND("*";SUBSTITUTE(A1;" ";"*";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))))

All Except the First String: =RIGHT(A1;LEN(A1)-FIND(" ";A1;1))

When ever I have more useful excel string functions, I'll update this post.