2009-01-29

Proper Case Or Title Case

In VB6 there was a function called StrConv which we were using to convert a string to Proper Case or Title Case, but there is no direct equivalent in .NET. Yes, we can still use this function in vb.net but it's not a .NET way. I mean, StrConv cannot be used in C# directly. To use it in C# we need to add the reference of Microsoft.VisualBasic.Compatibility.dll. And people will not like to do so just to use a single function of that library.

Some people write their own function to achieve the functionality. They first convert all the letters of the string to lowercase and then loop through all the words and capitalize the first letter of each word of the string. I am not in favor of this approach. I don't like to write long code just to achieve a small functionality. I always look for shortcuts.

Though there is not direct equivalent for StrConv in .NET, .NET provides us System.Globalization.TextInfo class to overcome the problem. We can use ToTitleCase function of TextInfo class to convert the string in proper case.

I have written a small function to do the work.

Public Function ToProperCase( _
ByVal source As String _
) As String

source = source.ToLower
Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source)
End Function
Usage:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click

MessageBox.Show(ToProperCase("HELLO WORLD!"))
End Sub
If you want to use ToProperCase() function like this...

Dim str As String = "HELLO WORLD!"
str = str.ToProperCase()
...then you need to use a concept called Extension Methods. Here it goes:

Module ExtensionMethods

_
Public Function ToProperCase( _
ByVal source As String _
) As String

source = source.ToLower
Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source)
End Function

End Module'
2009-01-21

How To Trap TAB Key

Have you ever faced any situation wherein you wanted to trap the TAB key and do some stuff. Lets say for example, you want to trap the TAB key on a Textbox control and want to process some logic. TAB key cannot be trapped in the KeyrPress event. TAB key can be trapped with ProcessCmdKey and ProcessTabKey methods. But in this case I prefer ProcessCmdKey. In ProcessCmdKey method, we can cancel the processing of TAB key but that is not the case with ProcessTabKey method. It just traps the TAB key, it doesn't gives you any control over TAB key.

In one of my Interop project, I was using True DBGrid control of ComponentOne. In that project, the grid was throwing an exception in some scenarios when TAB key is pressed. The exception was thrown by the control itself, so I didn't had any control on it. That's why I chose ProcessCmdKey to trap the TAB key. I trapped the key and manually set the focus to the next control. This is how ProcessCmdKey came to my rescue. I am posting the code below that I used in my project.

''' 
''' Process the windows messages manually.
''' Trap the TAB key pressed on grid and suppress it.
''' Pressing the TAB key in FilterBar of C1 True DBGrid causes exception.
'''

'''
'''
'''
'''
Protected Overrides Function ProcessCmdKey( _
ByRef msg As Message, ByVal keyData As System.Windows.Forms.Keys _
) As Boolean

' Declare a variable of type Keys enumeration named keyPressed.
' Cast the msg's WParam as a KeyEnum value and assign it to the
' keyPressed variable.
Dim keyPressed As Keys = CType(msg.WParam.ToInt32(), Keys)
Dim ctrlType As String = "C1.Win.C1TrueDBGrid.GridEditor"

' Process keyPressed.
Select Case keyPressed
Case Keys.Tab
If Me.ActiveControl.ToString.StartsWith(ctrlType) Then
If grdSamples.EditActive = False Then
txtInstrument.Focus()
End If
' Cancel the TAB key message
Return True

' You can also trap the TAB key for button or TextBox
ElseIf Me.ActiveControl.Name = "btnClose" Then
grdSamples.Focus()
' Cancel the TAB key message
Return True
End If
Case Else
' Return the key message so it can be processed by this control.
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function