Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is probably a real easy one, but I can't seem to work out how to code it. I know the logic I want it to follow though.

I have written a program that receives text via tcp port, and is formatted, and once formatted a textbox is created and the text is appended to the textbox.

Obviously if I close the program, those textboxes and information will disappear, so I have reprogrammed the flow of the program so once it receives all of the data, prior to doing anything else it saves those files to a directory.

on form_load I want to search the directory, and then for each file that is read go through the sub routines for formatting, and textbox creation.

I just can't think of how to do this correctly.

in my head it is basically like this.

VB
for each "file" in "directory"
         file.text = datastring
         file.name = filename
         currenttb = currenttb
       createtxtbox(datastring filename, currenttb)
next
Posted
Updated 24-Nov-15 17:24pm
v2

1 solution

You're not too far off. The following would work
VB
'You will need Imports System.IO

'These can be stored in config or passed as parameters
Const folderName As String = "c:\temp"
Const searchPattern As String = "*.txt"

Dim fileNames As String() = Directory.GetFiles(folderName, searchPattern)

'For Each f As String In fileNames 
'You can use For Each if you don't want to use the counter - see below
For i As Long = 0 To fileNames.GetUpperBound(0)
    createtxtbox(fileNames(i), i)
Next

Then the sub-routine could look something like this
C#
Private Sub createtxtbox(ByVal fileName As String, ByVal i As Long)

    Dim newTxtBox As TextBox = New TextBox()

    newTxtBox.Name = i.ToString()
    newTxtBox.Multiline = True
    newTxtBox.ScrollBars = ScrollBars.Both

    'You'll need to position the text box in some way - I've used the file "number"
    newTxtBox.Top = (i * 20) + 1
    newTxtBox.Left = (i * 20) + 1

    newTxtBox.Text = File.ReadAllText(fileName)
    Me.Controls.Add(newTxtBox)

End Sub

You should include some try-catch exception checking and whatever your formatting requirements are.

Note I used ReadAllText just to get the entire file as a single string, if you want to get it line-by-line use ReadAllLines instead and loop through the contents with a For Each or For loop
 
Share this answer
 
Comments
Maciej Los 25-Nov-15 8:21am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900