Friday, December 10, 2010

Friday December 10 2010


Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

If e.KeyCode = Keys.Enter And Not (e.Alt Or e.Control) Then
If Me.ActiveControl.GetType Is GetType(TextBox) Or _
Me.ActiveControl.GetType Is GetType(CheckBox) Or _
Me.ActiveControl.GetType Is GetType(DateTimePicker) Then
If e.Shift Then
Me.ProcessTabKey(False)
Else
Me.ProcessTabKey(True)
End If
End If
End If

End Sub


Here is a program I made myself


Dim masterArray() As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileGrab As New Connection(OpenFileDialog1)
fileGrab.Connect()
fileGrab.buildArray()

masterArray = fileGrab.returnStringArray

Dim sString As String
For Each sString In masterArray
ListBox1.Items.Add(sString)
Next

Dim indexIn As Integer = masterArray.Length - 1

MessageBox.Show(indexIn)

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

End Sub


Public Class Connection
Dim stringFileName As String = ""
Dim holdingString As String = ""
Dim stringArray() As String

Function returnStringArray()

Return stringArray
End Function

Sub buildArray()
Dim index As Integer = 0
Dim holdingArray() = holdingString.Split(vbCrLf)
stringArray = holdingArray

End Sub

Sub Connect()
MessageBox.Show(stringFileName)
Dim fileReader As New System.IO.StreamReader(stringFileName)
holdingString = fileReader.ReadToEnd
fileReader.Close()
End Sub



Sub New(ByVal OpenFileDialog1)

OpenFileDialog1.Title = "Select a text file"
OpenFileDialog1.Filter = "Text files (*.txt) | *.txt"
OpenFileDialog1.InitialDirectory = "C:\Users\Alfred\Documents\VBPractice"
OpenFileDialog1.ShowDialog()
MessageBox.Show(OpenFileDialog1.FileName)
stringFileName = OpenFileDialog1.FileName

End Sub
End Class

1 comment:

Brad Jensen said...

What this is doing is converting the retun key to a tabkey, so if the user fills out a text field on your form, it causes the control to pass to the next field on the form. If the user holds down the shift key as they press enter, it will process the key as an enter key.

Since it handles me.keyup, this handles keypresses for the entire form, not just a single field.