How to set focus to the Username on the Asp.net Login Control

C#, asp.net 4 Comments »

[csharp]
protected void Page_Load(object sender, EventArgs e)
{
TextBox usernameControl = Login1.FindControl(”UserName”) as TextBox; // Change Login1 to the name of your Login Control
if (usernameControl != null)
Page.SetFocus(usernameControl);
}

[/csharp]

A collection of Common Regular Expressions

C# 2 Comments »

The following are a list of Regular expressions that I have either written or come across on other sites. This list will be expanded over time so check bak on this article.

Usage expression
Check email address [code]([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})[/code]
Check URL [code](ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?[/code]
UK Date dd/mm/yyyy [code](((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))

[/code]

An unbelievable source of expressions can be found at http://regexlib.com/DisplayPatterns.aspx
They even have a validator there.

ASP.net DropDown list of UK Counties

C#, asp.net 1 Comment »

The following code provides a static list of Counties in the UK

 <asp:DropDownList ID=”EditCounty” runat=”server”>

<asp:ListItem>Aberdeenshire</asp:ListItem>

<asp:ListItem>Anglesey</asp:ListItem>

<asp:ListItem>Angus</asp:ListItem>

<asp:ListItem>Antrim</asp:ListItem>

<asp:ListItem>Argyllshire</asp:ListItem>

<asp:ListItem>Armagh</asp:ListItem>

<asp:ListItem>Ayrshire</asp:ListItem>

<asp:ListItem>Banffshire</asp:ListItem>

<asp:ListItem>Bedfordshire</asp:ListItem>

<asp:ListItem>Berkshire</asp:ListItem>

<asp:ListItem>Berwickshire</asp:ListItem>

<asp:ListItem>Brecknockshire</asp:ListItem>

<asp:ListItem>Buckinghamshire</asp:ListItem>

<asp:ListItem>Buteshire</asp:ListItem>

<asp:ListItem>Caernarfonshire</asp:ListItem>

<asp:ListItem>Caithness</asp:ListItem>

<asp:ListItem>Cambridgeshire</asp:ListItem>

<asp:ListItem>Cardiganshire</asp:ListItem>

<asp:ListItem>Carmarthenshire</asp:ListItem>

<asp:ListItem>Cheshire</asp:ListItem>

<asp:ListItem>Clackmannanshire</asp:ListItem>

<asp:ListItem>Cornwall</asp:ListItem>

<asp:ListItem>Cromartyshire</asp:ListItem>

<asp:ListItem>Cumberland</asp:ListItem>

<asp:ListItem>Denbighshire</asp:ListItem>

<asp:ListItem>Derbyshire</asp:ListItem>

<asp:ListItem>Devon</asp:ListItem>

<asp:ListItem>Dorset</asp:ListItem>

<asp:ListItem>Down</asp:ListItem>

<asp:ListItem>Dumfriesshire</asp:ListItem>

<asp:ListItem>Dunbartonshire</asp:ListItem>

<asp:ListItem>Durham</asp:ListItem>

<asp:ListItem>East Lothian</asp:ListItem>

<asp:ListItem>Essex</asp:ListItem>

<asp:ListItem>Fermanagh</asp:ListItem>

<asp:ListItem>Fife</asp:ListItem>

<asp:ListItem>Flintshire</asp:ListItem>

<asp:ListItem>Glamorgan</asp:ListItem>

<asp:ListItem>Gloucestershire</asp:ListItem>

<asp:ListItem>Hampshire</asp:ListItem>

<asp:ListItem>Herefordshire</asp:ListItem>

<asp:ListItem>Hertfordshire</asp:ListItem>

<asp:ListItem>Huntingdonshire</asp:ListItem>

<asp:ListItem>Inverness-shire</asp:ListItem>

<asp:ListItem>Kent</asp:ListItem>

<asp:ListItem>Kincardineshire</asp:ListItem>

<asp:ListItem>Kinross</asp:ListItem>

<asp:ListItem>Kirkcudbrightshire</asp:ListItem>

<asp:ListItem>Lanarkshire</asp:ListItem>

<asp:ListItem>Lancashire</asp:ListItem>

<asp:ListItem>Leicestershire</asp:ListItem>

<asp:ListItem>Lincolnshire</asp:ListItem>

<asp:ListItem>Londonderry</asp:ListItem>

<asp:ListItem>Merioneth</asp:ListItem>

<asp:ListItem>Middlesex</asp:ListItem>

<asp:ListItem>Midlothian</asp:ListItem>

<asp:ListItem>Monmouthshire</asp:ListItem>

<asp:ListItem>Montgomeryshire</asp:ListItem>

<asp:ListItem>Morayshire</asp:ListItem>

<asp:ListItem>Nairnshire</asp:ListItem>

<asp:ListItem>Norfolk</asp:ListItem>

<asp:ListItem>Northamptonshire</asp:ListItem>

<asp:ListItem>Northumberland</asp:ListItem>

<asp:ListItem>Nottinghamshire</asp:ListItem>

<asp:ListItem>Orkney</asp:ListItem>

<asp:ListItem>Oxfordshire</asp:ListItem>

<asp:ListItem>Pembrokeshire</asp:ListItem>

<asp:ListItem>Peeblesshire</asp:ListItem>

<asp:ListItem>Perthshire</asp:ListItem>

<asp:ListItem>Radnorshire</asp:ListItem>

<asp:ListItem>Renfrewshire</asp:ListItem>

<asp:ListItem>Ross-shire</asp:ListItem>

<asp:ListItem>Roxburghshire</asp:ListItem>

<asp:ListItem>Rutland</asp:ListItem>

<asp:ListItem>Selkirkshire</asp:ListItem>

<asp:ListItem>Shetland</asp:ListItem>

<asp:ListItem>Shropshire</asp:ListItem>

<asp:ListItem>Somerset</asp:ListItem>

<asp:ListItem>Staffordshire</asp:ListItem>

<asp:ListItem>Stirlingshire</asp:ListItem>

<asp:ListItem>Suffolk</asp:ListItem>

<asp:ListItem>Surrey</asp:ListItem>

<asp:ListItem>Sussex</asp:ListItem>

<asp:ListItem>Sutherland</asp:ListItem>

<asp:ListItem>Tyrone</asp:ListItem>

<asp:ListItem>Warwickshire</asp:ListItem>

<asp:ListItem>West Lothian</asp:ListItem>

<asp:ListItem>Westmorland</asp:ListItem>

<asp:ListItem>Wigtownshire</asp:ListItem>

<asp:ListItem>Wiltshire</asp:ListItem>

<asp:ListItem>Worcestershire</asp:ListItem>

<asp:ListItem>Yorkshire</asp:ListItem>

</asp:DropDownList>

Rendering a Focus Rectangle

C#, Component Development, Graphics and Images No Comments »

Although you can use the DrawRectangle method to render a focus rectangle, you’ll have to set up the pen to render in the stadard dotted format. However, in the controlpaint static class there is already a DrawFocusRectangle method which does the job for you and makes the appropriate system call.

[csharp]

ControlPaint.DrawFocusRectangle(g, FocusRect);

[/csharp]

ControlPaint is found in System.Windows.Forms.

Hex String to byte array converter

C#, C# Language 6 Comments »

The following simple static class will take a string of Hexdecimal characters and convert it to a byte array.

 

    static class HexStringConverter
    {
        public static byte[] ToByteArray(String HexString)
        {
            int NumberChars = HexString.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
            }
            return bytes;
        }
    }

Writing and Reading AVI files in C#

C#, External C# Articles, Graphics and Images 1 Comment »

Here is a very extensive article, with demos and code written by Corinna John on the CodeProject.

http://www.codeproject.com/cs/media/avifilewrapper.asp

Nullable Types

C#, C# Language No Comments »

In C# and .net you can define a variable of the standard values types to be nullable as well. This is extremely useful in circumstances when you want to check if a varaible has been intialized correctly.

For example, if you have a class that represents a user which has an integer user id and it is not initialized in the constructor. Rather than having the user id intitialize to 0 (zero) (which would be the default for an integer) as the User id 0 may well be a valid user’s id. the type can be defined as a nullable integer using int? UserID and will default to null instead.

Examples of nullable declarations

[csharp]

int? i = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = ‘a’;
int?[] arr = new int?[10];

[/csharp]

Custom Exceptions and Serialization.

C#, C# Language, Win Forms No Comments »

Microsoft state that if you want to raise custom exceptions in your application then you should derive them from the ApplicationException class and not the Exception class.

ApplicationException is thrown by a user program, not by the common language runtime. If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class. ApplicationException extends Exception, but does not add new functionality. This exception is provided as means to differentiate between exceptions defined by applications versus exceptions defined by the system.

However, the blog entry by Microsoft’s Brad Adams states that ApplicationException should not be used !

I found that descending from Exception, at least gives a nice error message detailing the exception that was raised as opposed to ApplicationExceptiosn, “Application Error ocurred” message.

So how do we serialize a custom exception class ?

Step 1) Add the Serializable attribute to the exception

Step 2) Provide a contructor that takes the serialization information and context e.g. MyException(SerializationInfo info, StreamingContext context)
Step 3) If you have a constructor that passes state information to the exception to be included in the data collection ensure this is copied from the exception object to the info in the serialization constructor (step2)

e.g.

[csharp]

[Serializable]
public class MyException : Exception
{
public MyException(String sessionId)
: base()
{
this.Data.Add(”SessionID”, sessionId);
}

public MyException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
info.AddValue(”SessionID”,Data["SessionID"]);
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
[/csharp]

How to grab input keys (Tab, Cursors Keys) in a control

C#, Component Development No Comments »

As default, systems keys such as tab, return, up, down, left and right are handled by the form to move between controls. To enable theses keys to be raised as key down, up and pressed events, you need to override the IsInpuKey method and return true for the keys you wish your control to process.

e.g. to get a control to process the Tab key.

[csharp]

protected override bool IsInputKey(System.Windows.Forms.Keys keyData)
{
switch (keyData)
{
case Keys.Tab: return (true);
default:
return (base.IsInputKey(keyData));
}
}
[/csharp]

How to create a bit-based enumeration

C#, C# Language No Comments »

If you want to create an enumeration that acts like a set, where the value of the enumeration can be a combination of any of the members of the enumeration then you C# allows you to do this using the FlagsAttribute.

Simply add the attribute to the start of the enumeration and the enumeration values will be treated as bit flags instead.

[csharp]

[FlagsAttribute]
enum Colors : short
{
None = 0,
Red = 1,
Green = 2,
Blue = 4,
White = 7
};[/csharp]

You can use bitwise operators Not, And, Or and XOR on the enumeration.

e.g. to see if an enumeration value is set

[csharp]

if ( Colors & Red == Red)

{

// do something
}

[/csharp]

Note that the not (!) does not work on bitwise operations so the complement(~) operator is required.

So to remove a the red and green flag from the set…

[csharp]

Colors = Colors & ~(Red | Green);
[/csharp]

WP Theme &Design by minus19.com & Icons
Entries RSS Comments RSS Log in