Link textbox1 to filelistbox1??

  • Thread starter Thread starter Mark Ivey
  • Start date Start date
M

Mark Ivey

VS.Net 2002 - Visual Basic

I am pretty new to VB in general, and I am having a bit of difficulty
finding the right method or property to link a textbox to a filelistbox.

Here is the scenario...

I am trying to create a simple interface to speed up the ability to open
specific files.

I have a filelistbox dedicated to a specific directory to display all the
..FMT type files in it. I would like the user to be able to start typing the
filename in a textbox and have the filelistbox match the record from the
input text. I want to filelistbox to highlight the first record that matches
the
criteria from the textbox, but to keep the curser in the textbox to await
further input from the user.

In other words:

If the user types "8" in the textbox, I want the first file with a filename
starting with an "8" to be highlighted in the filelistbox. Then if the next
character input is a "6", then I want the first filename with an "86" to be
highlighted in the filelistbox.

Can anyone help me with a solution to my problem?

Thanks in advance...

Mark Ivey
 
Here is the code for what I have so far. I know I am missing something, but
I am unsure of what property or method to use next.


'**************************************************************************************
'**************************************************************************************
'**************************************************************************************
Imports System.Diagnostics

Public Class Form1

Inherits System.Windows.Forms.Form

Dim myPath As String = "C:\WINEASY\ALL FMTS"

#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 Button1 As System.Windows.Forms.Button

Friend WithEvents FileListBox1 As
Microsoft.VisualBasic.Compatibility.VB6.FileListBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button()

Me.FileListBox1 = New Microsoft.VisualBasic.Compatibility.VB6.FileListBox()

Me.Label1 = New System.Windows.Forms.Label()

Me.Label2 = New System.Windows.Forms.Label()

Me.TextBox1 = New System.Windows.Forms.TextBox()

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Button1.Location = New System.Drawing.Point(328, 192)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(144, 56)

Me.Button1.TabIndex = 2

Me.Button1.Text = "Open Label"

'

'FileListBox1

'

Me.FileListBox1.Font = New System.Drawing.Font("Microsoft Sans Serif",
8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))

Me.FileListBox1.Location = New System.Drawing.Point(40, 48)

Me.FileListBox1.Name = "FileListBox1"

Me.FileListBox1.Pattern = "*.fmt"

Me.FileListBox1.Size = New System.Drawing.Size(168, 251)

Me.FileListBox1.TabIndex = 3

'

'Label1

'

Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Label1.Location = New System.Drawing.Point(256, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(288, 112)

Me.Label1.TabIndex = 4

Me.Label1.Text = "Select the desired label in the list and then select the
Open Label button."

Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label2

'

Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Label2.Location = New System.Drawing.Point(320, 280)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(232, 24)

Me.Label2.TabIndex = 5

'

'TextBox1

'

Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.TextBox1.Location = New System.Drawing.Point(40, 16)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(168, 20)

Me.TextBox1.TabIndex = 6

Me.TextBox1.Text = ""

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(576, 326)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1,
Me.Label2, Me.Label1, Me.FileListBox1, Me.Button1})

Me.Name = "Form1"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "WinEasy Label Launcher"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

System.IO.Directory.SetCurrentDirectory(myPath)

FileListBox1.Path = myPath

TextBox1.Select()

End Sub

Private Sub FileListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles FileListBox1.SelectedIndexChanged

Label2.Text = myPath & "\" & FileListBox1.FileName

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Process.Start(Label2.Text)

End Sub

Private Sub FileListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles FileListBox1.DoubleClick

Process.Start(Label2.Text)

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If TextBox1.Text = "" Then Exit Sub

' ??? I am unsure of what the next step should be here

End Sub



End Class


'**************************************************************************************
'**************************************************************************************
'**************************************************************************************
 
Mark Ivey said:
Here is the code for what I have so far. I know I am missing
something, but I am unsure of what property or method to use next.

May I add some additional hints:
System.IO.Directory.SetCurrentDirectory(myPath)

Is there a reason why you set the current directory? (just out of interest)
TextBox1.Select()

In Form_Load, this isn't useful because the control must be visible to be
selectable.
Label2.Text = myPath & "\" & FileListBox1.FileName

You can use System.IO.Path.Combine instead of manually concatenating path
name and file name because it also handles the root directory correctly. (no
problem in this case, but generally)
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged

If TextBox1.Text = "" Then Exit Sub

' ??? I am unsure of what the next step should be here

You can use the (File)ListBox' FindString method to find the string. Then
call the SetSelected method to select the found item (if found).

BTW, at least for other purposes the compatibility DLL
(microsoft.VisualBasic.Compatibility.dll) should not be used anymore. Only
for upgraded VB6 projects.


Armin
 
I set the current directory so the FileListBox would look in one specific
path. It is the only method I could think to accomplish that part of this
project.

I know what you mean about the FileListBox, but it was just too tempting not
to use on this project.

I appreciate your pointers. I actually happened across another solution on
the web after posting this today. If you were interested here is a copy of
my final version.

Thanks for your help...


'*****************************************************************************************
'*****************************************************************************************
'*****************************************************************************************
Imports System.Diagnostics

Public Class Form1

Inherits System.Windows.Forms.Form

Dim myPath As String = "C:\WINEASY\ALL FMTS"

#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 Button1 As System.Windows.Forms.Button

Friend WithEvents FileListBox1 As
Microsoft.VisualBasic.Compatibility.VB6.FileListBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button()

Me.FileListBox1 = New Microsoft.VisualBasic.Compatibility.VB6.FileListBox()

Me.Label1 = New System.Windows.Forms.Label()

Me.Label2 = New System.Windows.Forms.Label()

Me.TextBox1 = New System.Windows.Forms.TextBox()

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Button1.Location = New System.Drawing.Point(328, 192)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(144, 56)

Me.Button1.TabIndex = 2

Me.Button1.Text = "Open Label"

'

'FileListBox1

'

Me.FileListBox1.Font = New System.Drawing.Font("Microsoft Sans Serif",
8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))

Me.FileListBox1.Location = New System.Drawing.Point(40, 48)

Me.FileListBox1.Name = "FileListBox1"

Me.FileListBox1.Pattern = "*.fmt"

Me.FileListBox1.Size = New System.Drawing.Size(168, 251)

Me.FileListBox1.TabIndex = 3

'

'Label1

'

Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Label1.Location = New System.Drawing.Point(256, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(288, 112)

Me.Label1.TabIndex = 4

Me.Label1.Text = "Select the desired label in the list and then select the
Open Label button."

Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label2

'

Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.Label2.Location = New System.Drawing.Point(320, 280)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(232, 24)

Me.Label2.TabIndex = 5

'

'TextBox1

'

Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))

Me.TextBox1.Location = New System.Drawing.Point(40, 16)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(168, 20)

Me.TextBox1.TabIndex = 6

Me.TextBox1.Text = ""

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(584, 326)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1,
Me.Label2, Me.Label1, Me.FileListBox1, Me.Button1})

Me.Name = "Form1"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "WinEasy Label Launcher"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

System.IO.Directory.SetCurrentDirectory(myPath)

FileListBox1.Path = myPath

TextBox1.Select()

End Sub

Private Sub FileFileListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
FileListBox1.SelectedIndexChanged

Label2.Text = myPath & "\" & FileListBox1.FileName

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Process.Start(Label2.Text)

End Sub

Private Sub FileFileListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles FileListBox1.DoubleClick

Process.Start(Label2.Text)

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If TextBox1.Text = "" Then Exit Sub

Dim listLength As Integer = (FileListBox1.Items.Count - 1)

Dim i, j As Integer

Dim listString, newString As String

For i = 0 To listLength

listString = FileListBox1.Items.Item(i)

If InStr(listString.ToLower, TextBox1.Text.ToLower) Then

FileListBox1.SetSelected(i, True)

End If

Next

listString = Nothing

newString = Nothing

listString = Nothing

End Sub

End Class

'*****************************************************************************************
'*****************************************************************************************
'*****************************************************************************************
 
Back
Top