Wednesday, June 17, 2009

How to disable the Close button of win forms?

Many times, there was a requirement to hide or disable the close button at the top right of the form.
There is a way to hide the control box by setting the ControlBox property to false. But unfortunately it removes the Windows icon which could be key to the company's/personal branding/marketing efforts.
The below code disables the close button without hiding the window's icon.

IndexOf method in strings

The string's IndexOf methods are all case-sensitive.

Fortunately, we have the CompareInfo class in the Globalization namespace, that includes a case-insensitive IndexOf method

using System.Globalization;
string strValue = "C# is a GREAT programming language.";
string strCheck = "great";

CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int i = Compare.IndexOf( strValue, strCheck, CompareOptions.IgnoreCase );


Compare.IndexOf here returns the index of the string "great".

Monday, June 1, 2009

Get Row Count of the table

Normally count(*) will be used to query the no of records of a table. But this might consume significant resources if the tables are very big because scanning a large table or index can consume a lot of I/O.

One way to find the number of rows is through sysindexes. The value of sysindexes.indid will always be 0 for a table and 1 for a clustered index. If a table doesn't have a clustered index, its entry in sysindexes will always have an indid value of 0. If a table does have a clustered index, its entry in sysindexes will always have an indid value of 1.

SELECT object_name(id) ,rowcnt FROM sysindexes WHERE indid IN (1,0) AND OBJECTPROPERTY(id, 'IsUserTable') = 1 and object_name(id) = ''