How do I pass a textbox reference to a sub in Visual Basic

  • Thread starter Thread starter TJBlues
  • Start date Start date
T

TJBlues

Guest
I am building a form in Visual Studio 2013 that has multiple textboxes and combo boxes. I place directory names in the textboxes and I have my software populate the dropdown combo boxes with the sub-directories that match a search term. I built a Private Sub to enumerate the directories. I call that from various parts of my main code.

I go about this is in the main code by reading the text from the textbox (this is the directory I want to search) and passing that to my Private Sub which in return passes me back an array of strings (the sub-directories) and a counter to tell me how many sub-directories there are and I then populate the corresponding combo box.

I would prefer to streamline this by passing the reference to the textbox and combo box into my sub and have it enumerate the directory and then populate the combo box.

In my example "txtBoxPathLocal" is the textbox that holds the directory path and "comboBoxPathLocal" contains the results from my search.

I will be expanding this out to include remote paths and archive paths with their own textboxes and combo boxes.

My code for the generic Enumerate Sub looks like this:

Private Sub EnumFolders(ByRef TopFolder As String, _
ByVal SearchWords As String, _
ByVal SearchWordsCount As Integer, _
ByRef i As Integer, _
ByRef SubPaths As Array) 'Enumerate the folders to find matches to the search words
i = 0
Try
Dim dirPath As String = TopFolder
Dim dirs = From folder In _
Directory.EnumerateDirectories(dirPath, SearchWords, SearchOption.AllDirectories)
For Each folder In dirs
' Remove main path information from string.

If i < MaxSetsNames Then
SubPaths(i) = folder.Substring(dirPath.Length)
i += 1
End If
Console.WriteLine("{0}", _
folder.Substring(dirPath.Length)) 'For Test purposes only
Next

Catch UAEx As UnauthorizedAccessException
Console.WriteLine(UAEx.Message)
MsgBox("Error of UAEx Occured " & UAEx.Message, MsgBoxStyle.Critical)
Catch PathEx As PathTooLongException
MsgBox("Error of PathEx Occured " & PathEx.Message, MsgBoxStyle.Critical)
Console.WriteLine(PathEx.Message)
Catch ex As Exception
Console.WriteLine("The error is Ex " & ex.Message)
MsgBox("Some Unknown ErrorOccured " & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub



My code to call that sub looks like:

SetsCount = 0
If txtBoxPathLocal.Text <> "" Then
TstString0 = "*house*"
EnumFolders(txtBoxPathLocal.Text, TstString0, MaxSetSearchWords, NumberOfSets, SubFoldersNames)
For SetsCount = 1 To NumberOfSets
comboBoxPathLocal.Items.Add(SubFoldersNames(SetsCount - 1))
Next
End If

Does anyone know how to pass the references to the textboxes like "txtBoxPathLocal" (not the text contained in that textbox) and the combo boxes like "comboBoxPathLocal" into the "EnumFolders" sub so it will populate the combo boxes and I don't have to duplicate the main code for each textbox?

Continue reading...
 
Back
Top