Public Class Form5
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents chkAppendSpace As System.Windows.Forms.CheckBox
Friend WithEvents chkPrependSpace As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.chkAppendSpace = New System.Windows.Forms.CheckBox
Me.chkPrependSpace = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(16, 24)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(464, 288)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'ListBox1
'
Me.ListBox1.Items.AddRange(New Object() {"Fred", "John", "Sue", "Anne", "Roy", "Alison", "Richard", "Robert", "Adam", "James"})
Me.ListBox1.Location = New System.Drawing.Point(96, 88)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120, 95)
Me.ListBox1.TabIndex = 1
Me.ListBox1.Visible = False
'
'chkAppendSpace
'
Me.chkAppendSpace.Location = New System.Drawing.Point(528, 56)
Me.chkAppendSpace.Name = "chkAppendSpace"
Me.chkAppendSpace.TabIndex = 2
Me.chkAppendSpace.Text = "Append Space"
'
'chkPrependSpace
'
Me.chkPrependSpace.Location = New System.Drawing.Point(528, 104)
Me.chkPrependSpace.Name = "chkPrependSpace"
Me.chkPrependSpace.TabIndex = 3
Me.chkPrependSpace.Text = "Prepend Space"
'
'Form5
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(704, 341)
Me.Controls.Add(Me.chkPrependSpace)
Me.Controls.Add(Me.chkAppendSpace)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.RichTextBox1)
Me.KeyPreview = True
Me.Name = "Form5"
Me.Text = "Form5"
Me.ResumeLayout(False)
End Sub
#End Region
Private SearchString As String
Private CurrentPosition As Integer
Private PrependSpace As Boolean
Private AppendSpace As Boolean
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
ListBox1.Visible = False
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
CurrentPosition = RichTextBox1.SelectionStart
SearchString = ""
AppendSpace = chkAppendSpace.Checked
PrependSpace = chkPrependSpace.Checked
Dim curPoint As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
curPoint.Offset(RichTextBox1.Left, RichTextBox1.Top + RichTextBox1.SelectionFont.Height)
ListBox1.Location = curPoint
ListBox1.Visible = True
ListBox1.BringToFront()
' Added a focus event
ListBox1.Focus()
End If
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
RichTextBox1.AppendText(ListBox1.SelectedItem.ToString)
ListBox1.Visible = False
End Sub
Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
'Holding place for Cursor Key handler
Select Case e.KeyCode
Case Keys.Left
e.Handled = False
Case Keys.Right
e.Handled = False
Case Keys.Back
e.Handled = True
Case Keys.Delete
e.Handled = True
Case Else
e.Handled = False
End Select
End Sub
Private Sub ListBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListBox1.KeyPress
'Accept Enter or Space as selection key
If e.KeyChar = Convert.ToChar(Keys.Enter) Or e.KeyChar = Convert.ToChar(Keys.Space) Then
Dim x As Integer = RichTextBox1.SelectionStart - CurrentPosition
RichTextBox1.SelectionStart = CurrentPosition
RichTextBox1.SelectionLength = x
RichTextBox1.Cut()
Dim TailSpace As String = IIf(AppendSpace, " ", "").ToString
Dim StartSpace As String = IIf(PrependSpace, " ", "").ToString
RichTextBox1.AppendText(StartSpace + ListBox1.SelectedItem.ToString + TailSpace) 'add a space to the end of the string
ListBox1.Visible = False
RichTextBox1.Focus()
' Let the user go back to the RTB by pressing escape
ElseIf e.KeyChar = Convert.ToChar(Keys.Escape) Then
ListBox1.Visible = False
RichTextBox1.Focus()
Else
'Keep it simple for now(Delete, Left and Right can be handled later if anyone is brave enough)
'We'll just assume the character is proper text
RichTextBox1.AppendText(e.KeyChar)
SearchString += e.KeyChar
ListBox1.SelectedIndex = ListBox1.FindString(SearchString, 0)
End If
End Sub
Private Sub ListBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.LostFocus
ListBox1.Visible = False
End Sub
' Add a click event to hide the ListBox as per E&F's comments
' This still needs to be added to other panels/controls that exist
Private Sub Form5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
ListBox1.Visible = False
End Sub
' Set a default entry for the ListBox so when it pops up
' the user can use up/down arrows to navigate the items
Private Sub Form5_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.SelectedItem = ListBox1.Items(0)
End Sub
Private Sub chkPrependSpace_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPrependSpace.CheckedChanged
RichTextBox1.Focus()
End Sub
Private Sub chkAppendSpace_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAppendSpace.CheckedChanged
RichTextBox1.Focus()
End Sub
End Class