Thursday, December 23, 2010

Thursday 12/23/10

Working on a tutorial by Evangelos Petroutsos.

With this code be sure and set the forms KeyPreview property to true.


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
If Me.ActiveControl.GetType Is GetType(TextBox) Then
e.SuppressKeyPress = True
If e.Shift Then
Me.ProcessTabKey(False)
Else
Me.ProcessTabKey(True)
End If
End If
End If
End Sub


Canceling the closing of a form


Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim reply As MsgBoxResult
reply = MsgBox("Document has been edited. " & _
"OK to terminate application, Cancel to " & _
"return to your document.", MsgBoxStyle.OkCancel)
If reply = MsgBoxResult.Cancel Then
e.Cancel = True
End If
End Sub


Here I am working on The Tim Patrick tutorial - still building the module for the library ap



Public Function LeftAndRightText(ByVal origLeft As String, _
ByVal origRight As String, _
ByVal textWidth As Integer) As String
' Build a string of the indicated width that has both left-justified and right-justified text

Dim leftText As String
Dim rightText As String

leftText = Trim(origLeft)
rightText = Trim(origRight)
If (Len(leftText & rightText) >= textWidth) Then
Return Trim(Left(leftText & rightText, textWidth))
Else
Return leftText & Space(textWidth - Len(leftText & rightText)) & rightText
End If

End Function


Here is a function from the module by Tim Patrick dealing with InStr functions


Public Function CountSubStr(ByVal mainText As String, ByVal subText As String) As Integer
'--- Return a count of the number of times
' that a subText occurs in a string (mainText)
Dim totalTimes As Integer
Dim startPos As Integer
Dim foundPos As Integer

totalTimes = 0
startPos = 1

'keep searching until we don't find any more
Do
'---search for the subText
foundPos = InStr(startPos, mainText, subText)
If (foundPos = 0) Then Exit Do
totalTimes += 1
'--- move to just after the occurrence
startPos = foundPos + Len(subText)
Loop

'---Return the count
Return totalTimes

End Function

1 comment:

Brad Jensen said...

The first of these two code segements changes the handling of the enter key in text controls.