
LISTING 1. Parse a Text File the Smart Way .

ISTRUCTIONS: compile this code as a console application; ensure that the C:\DATA.TXT file is a comma-delimited file in this format:
John	Smith	New York
Ann	Doe	Los Angeles


' VB.NET
Sub Main()
  Dim sr As New StreamReader("c:\data.txt")
  Dim pattern As String = _
	"^(?<first>.{6})(?<last>.{8})(?<city>.+)$"
  Dim re As New Regex(pattern)
  Do While sr.Peek <> -1
	Dim ma As Match = re.Match(sr.ReadLine())
	Console.Write("First name = " & _
		ma.Groups("first").Value.TrimEnd())
	Console.Write(", Last name = " & _
		ma.Groups("last").Value.TrimEnd())
	Console.WriteLine(", City= " & _
		ma.Groups("city").Value.TrimEnd())
  Loop
  sr.Close()
End Sub

// C#
static void Main()
{
  StreamReader sr = new 
	StreamReader("c:\\data.txt");
  string pattern = 
	@"^(?<first>.{6})(?<last>.{8})(?<city>.+)$";
  Regex re = new Regex(pattern);
  while ( sr.Peek != -1 )
  {
	Match ma = re.Match(sr.ReadLine());
	Console.Write("First name = " + 
		ma.Groups["first"].Value.TrimEnd());
	Console.Write(", Last name = " + 
		ma.Groups["last"].Value.TrimEnd());
	Console.WriteLine(", City= " + 
		ma.Groups["city"].Value.TrimEnd());
  }
  sr.Close();
}

--------------------------------------------------------

LISTING 2. Trap hotkeys.

INSTRUCTIONS: put this code inside a Windows Forms class. Run the application, then give the input focus to another application. If you now press the Ctrl-A key combination you'll see that your code can trap it, and displays a message in the debug window.

' VB.NET
' Windows API functions and constants
Private Declare Function RegisterHotKey _
	Lib "user32" (ByVal hwnd As IntPtr, _
	ByVal id As Integer, ByVal fsModifiers _
	As Integer, ByVal vk As Keys) As Integer
Private Declare Function UnregisterHotKey _
	Lib "user32" (ByVal hwnd As IntPtr, _
	ByVal id As Integer) As Integer
Private Declare Function GlobalAddAtom _
	Lib "kernel32" Alias "GlobalAddAtomA" _
	(ByVal lpString As String) As Short
Private Declare Function GlobalDeleteAtom _
	Lib "kernel32" (ByVal nAtom As Short) As Short
Private Const MOD_ALT As Integer = &H1
Private Const MOD_CONTROL As Integer = &H2
Private Const MOD_SHIFT As Integer = &H4
Private Const MOD_WIN As Integer = &H8
Private Const WM_HOTKEY As Integer = &H312

Dim hotkeyID As Short

Sub Form1_Load(ByVal sender As Object, _
	ByVal e As System.EventArgs) _
	Handles MyBase.Load
	' determine a unique ID for hotkey
	hotkeyID = GlobalAddAtom("GlobalHotKey " & _
		Me.GetHashCode().ToString())
	' Register Ctrl+A
	RegisterHotKey(Me.Handle, hotkeyID, _
		MOD_CONTROL, Keys.A)
End Sub

Sub Form1_Closing(ByVal sender As Object, ByVal _
	e As System.ComponentModel.CancelEventArgs) _
	Handles MyBase.Closing
	' unregister the hotkey and delete the atom
	UnregisterHotKey(Me.Handle, hotkeyID)
	GlobalDeleteAtom(hotkeyID)
End Sub

Protected Overrides Sub WndProc( _
	ByRef m As Message)
	MyBase.WndProc(m)
	If m.Msg = WM_HOTKEY Then
		' process the hotkey here
		Debug.WriteLine("WndProc: CTRL+A")
	End If
End Sub

// C#
// Windows API functions and constants
[DllImport("user32")]
static extern int RegisterHotKey(IntPtr hwnd, 
	int id, int fsModifiers, Keys vk) ;
[DllImport("user32")]
static extern int UnregisterHotKey(IntPtr hwnd, 
	int id);
[DllImport("kernel32", 
	EntryPoint="GlobalAddAtomA")]
static extern short GlobalAddAtom(
	string lpString);
[DllImport("kernel32")]
static extern short GlobalDeleteAtom 
	(short nAtom);

private const int MOD_ALT = 0x01;
private const int MOD_CONTROL = 0x02;
private const int MOD_SHIFT = 0x04;
private const int MOD_WIN = 0x08;
private const int WM_HOTKEY = 0x312;

short hotkeyID;

void Form1_Load(object sender, EventArgs e)
{
	// determine a unique ID for hotkey
hotkeyID = GlobalAddAtom("GlobalHotKey " + 
	this.GetHashCode().ToString());
	// Register Ctrl+A
	RegisterHotKey(this.Handle, hotkeyID, 
		MOD_CONTROL, Keys.A);
}

void Form1_Closing(object sender, 
	System.ComponentModel.CancelEventArgs e)
{
	// unregister the hotkey and delete the atom
	UnregisterHotKey(this.Handle, hotkeyID);
	GlobalDeleteAtom(hotkeyID);
}

protected override void WndProc(ref Message m)
{
	base.WndProc(ref m);
	if ( m.Msg == WM_HOTKEY )
	{
		// process the hotkey here
		Debug.WriteLine("Ctrl+A");
	}
}

-----------------------------------------------------------------------------------

LISTING 3. Set the focus with style.

INSTRUCTIONS: Put this code in a Web Form class. Drop a few controls on the form

' VB.NET
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  SetAllIputControlsColors(Me, _
	SystemColors.Window, _
	SystemColors.WindowText, _
	Color.Yellow, Color.Blue)
End Sub

Sub SetInputControlColors(ByVal ctl As _
	WebControl, ByVal backColor As Color, _
	ByVal foreColor As Color, _
	ByVal focusBackColor As Color, _
	ByVal focusForeColor As Color)

	Dim jsOnFocus As String = String.Format( _
		"this.style.backgroundColor = '{0}';" _
		& "this.style.color = '{1}';", _
		focusBackColor.Name, focusForeColor.Name)
	Dim jsOnBlur As String = String.Format( _
		"this.style.backgroundColor = '{0}';" _ 
		& "this.style.color = '{1}';", _
		backColor.Name, foreColor.Name)
	ctl.Attributes.Add("onfocus", jsOnFocus)
	ctl.Attributes.Add("onblur", jsOnBlur)
End Sub

Sub SetAllInputControlsColors(ByVal parent As _
	Control, ByVal backColor As Color, _
	ByVal foreColor As Color, _
	ByVal focusBackColor As Color, _
	ByVal focusForeColor As Color)

	For Each ctl As Control In parent.Controls
		If TypeOf ctl Is TextBox OrElse _
			TypeOf ctl Is ListBox OrElse _
			TypeOf ctl Is DropDownList Then
			SetInputControlColors(_
				DirectCast( ctl, WebControl), _
				backColor, foreColor, _
				focusBackColor, _
				focusForeColor)
		Else
			SetAllInputControlsColors(ctl, _
				backColor, foreColor, _
				focusBackColor, _
				focusForeColor)
		End If
	Next
End Sub

// C#
void Page_Load(object sender, EventArgs e) 
{
  SetAllIputControlsColors(this, 
	SystemColors.Window, 
	SystemColors.WindowText, 
	Color.Yellow, Color.Blue);
}

void SetInputControlColors(WebControl ctl, 
	Color backColor, Color foreColor, 
	Color focusBackColor,  Color focusForeColor)
{
	string jsOnFocus = string.Format(
		"this.style.backgroundColor = '{0}';" +
		"this.style.color = '{1}';", 
		focusBackColor.Name, focusForeColor.Name);
	string jsOnBlur = string.Format(
		"this.style.backgroundColor = '{0}';" +
		"this.style.color = '{1}';", 
		backColor.Name, foreColor.Name);
	ctl.Attributes.Add("onfocus", jsOnFocus);
	ctl.Attributes.Add("onblur", jsOnBlur);
}

void SetAllInputControlsColors(Control parent, 
	Color backColor, Color foreColor, 
	Color focusBackColor,  Color focusForeColor)
{
	foreach (Control ctl in parent.Controls)
	{
		if (ctl is TextBox || ctl is ListBox || 
			ctl is DropDownList)
		{
			SetInputControlColors(
				ctl as WebControl, backColor, 
				foreColor, focusBackColor, 
				focusForeColor);
		}
		else
		{
			SetAllInputControlsColors(ctl, 
				backColor, foreColor, 
				focusBackColor, 
				focusForeColor);
		}
	}
}

-----------------------------------------------------------

LISTING 4. Use a CSS to change style.

INSTRUCTION: put this code in a Web Form class, associate the form with a stylesheet that contains this class
	.ActiveInputControl
	{
	background-color: Red;
	color: Yellow;
	font-weight: bold;
	}



' VB.NET

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  SetAllInputControlsClassName(Me, "", _
	"ActiveInputControl")
End Sub

Sub SetInputControlClassName(ByVal ctl As _
	WebControl, ByVal className As String, _
	ByVal focusClassName As String)
	Dim jsOnFocus As String = String.Format( _
		"this.className = '{0}';", focusClassName)
	Dim jsOnBlur As String = String.Format( _
		"this.className = '{0}';", className)
	ctl.Attributes.Add("onfocus", jsOnFocus)
	ctl.Attributes.Add("onblur", jsOnBlur)
End Sub

Sub SetAllInputControlsClassName(ByVal parent _
	As Control, ByVal className As String, _
	ByVal focusClassName As String)
	For Each ctl As Control In parent.Controls
		If TypeOf ctl Is TextBox OrElse _
			TypeOf ctl Is ListBox OrElse TypeOf 
			ctl Is DropDownList Then
			SetInputControlClassName( _
				DirectCast(ctl, WebControl),_
				className, focusClassName)
		Else
			SetAllInputControlsClassName(ctl, _
				className, focusClassName)   
		End If
	Next
End Sub

// C#
void Page_Load(object sender, EventArgs e) 
{
SetAllInputControlsClassName(this, "", 
	"ActiveInputControl");

}

void SetInputControlClassName(WebControl ctl, 
	string className, string focusClassName)
{
	string jsOnFocus = String.Format( 
		"this.className = '{0}';", focusClassName);
   string jsOnBlur = String.Format( 
		"this.className = '{0}';", className);
	ctl.Attributes.Add("onfocus", jsOnFocus);
	ctl.Attributes.Add("onblur", jsOnBlur);
}

void SetAllInputControlsClassName(Control parent, 
	string className, string focusClassName)
{
	foreach (Control ctl in parent.Controls)
	{
		if (ctl is TextBox || ctl is ListBox || 
			ctl is DropDownList)
		{
			SetInputControlClassName(
				ctl as WebControl, 
				className, focusClassName);
		}
		else
		{
			SetAllInputControlsClassName(ctl, 
				className, focusClassName);
		}

	}
}

