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.