Tuesday, September 28, 2010

Control catalog name when using AttachDBFilename

I was using the AttachDBFilename and discovered that the database name ended up being the full path of the file being attached.

Setting InitialCatalog as well enable me to control the catalog name of the attached file.

var cb = new SqlConnectionStringBuilder {
IntegratedSecurity = true,
DataSource = ".\MyInstance",
AttachDBFilename = @"c:\mypath\mydb.mdf",
InitialCatalog = "mydb catalog name"
};

Thursday, September 23, 2010

Visual studio designer hangs and fails to load ToolStripControlHost derived class

I found a problem using a ToolStripControlHost derived class when the class is being built in the same solution as the form that uses the component.

When performing a rebuild the designer would freeze and wouldn't redraw the toolbar. The fix I discovered solves the problem where the designer disposes the object and then attempts to access it after the rebuild.

public class CustomButtonToolStripHost : ToolStripControlHost
{
public CustomButtonToolStripHost ()
: base(CustomToolStripButton())
{
}
/// Used to overcome a designer error when rebuilding the project.
private class CustomToolStripButton : Button // Replace with your control
{
/// Override CreateHandle to avoid exception if called when object is disposed.
/// This occurs when the designer updates after a rebuild.

protected override void CreateHandle()
{
if (!IsDisposed)
{
base.CreateHandle();
}
}
}
}

.Net button that doesn't take/steal focus

This is a simple solution that I've came up with after some thinking:

public class MyButton : Button
{
public MyButton()
{
if (!DesignMode)
{
SetStyle(ControlStyles.Selectable, false);
}
}
}

Much easier than trying to handle mouse activation messages etc...