Sunday, March 23, 2008

Assign Shortcut Key to a Button in C#



1) Buttons normally have accelerator keys (EG Alt+G) rather than
shortcut keys (EG Ctrl+G). Accelerator keys are set using the Text
property of the button. Put an ampersand (&) before the letter in
the caption you want the button to react to.


When the user presses Alt, all the accelerators on the form get underlined.


Note that the form's Accept button automatically reacts to the Enter
key and it's cancel button reacts to the Escape key. Both of these are
properties of the form.


You should not put Accelerators on the Accept or Cancel button.


IE Button captions should not be OK or Cancel because you don't need to be able to press Alt+O as well as Return to do the same thing.

2) You could set the KeyPreview property of your Form to true, and then subscribe to the KeyDown event. There, you simply put an if statement .. if (e.keycode == keys.return) ...

Tuesday, January 15, 2008

Regular Expression Pattern for 1 or 2 digit number

following pattern would match 1 or two digit number:

[0-9]?

"[0-9]" will match any number from 1 to 9.
"?" will match if the previous number repeated 0 or 1 time.

Saturday, January 12, 2008

How to Insert Newline in a Multiline Label or Textbox

1) First thought might be that inserting a line feed "\n" inside your multi-line string will work. But unfortunately not. You have to insert a carriage return and line feed together i.e. "\r\n".

Example: MyLabel.Text = "Line 1\r\nLine2"

will produce:

Line 1
Line 2

2) Alternately for TextBox one can make use of Lines[] array property, like

MyTextBox.Lines[0] = "Line 1";
MyTextBox.Lines[1] = "Line 2";

will also produce the same result as above.

Friday, January 4, 2008

Use System.Reflection to get Information about Application Name, Version, Product, Copyright Information, etc.

Following C# class can be reused to get Information about Application Name, Version, Product, Copyright Information, etc. The code uses the Assemblyinfo.cs file settings to retrieve the required information.


using System;
using System.Reflection;

namespace Reuse
{
/// <summary>
/// Summary description for clsAssembly.
/// </summary>
public class KAssembly
{
public KAssembly()
{
//
// TODO: Add constructor logic here
//
}
public static string GetName()
{
return Assembly.GetEntryAssembly().GetName().Name;
}

public static string GetVersion()
{
return Assembly.GetEntryAssembly().GetName().Version.ToString();
}

public static string GetProduct()
{
object[] attrs = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute),false);
return ((AssemblyProductAttribute) attrs[0]).Product;
}

public static string GetCopyright()
{
object[] attrs = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute),false);
return ((AssemblyCopyrightAttribute) attrs[0]).Copyright;
}
}
}

Thursday, January 3, 2008

.* Pattern in Regular Expression

A dot (.) in regular expression means any character whatsoever except the newline character.
A start (*) means 0 or more occurrence of the previous character.
Therefore the dot-star (.*) combination in a regular expression means "some sequence of characters on the same line" or "simply nothing".

Example content:

<body>
anything goes here...
</body>

Regular expression:
<body>.*</body>

Search result:
Will not match anything

But if the example content is:

<body>anything goes here...</body>

and the Regular is expression as before:
<body>.*</body>

Search result:
Will match the whole string including the <body>...</body> tags.