Group,

I'm taking a text file that is storing ID numbers in a single column that looks like this:

123
3050
3971

I'm looping through the text file line for line and putting each ID number into a textbox. My question is: Am I writing this code the most efficient way? Here's my code:

Dim FILE_NAME As String = "C:\Restran Conversion\RestranFileConversion.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim txtProps As String
Dim i As Integer = 1
If System.IO.File.Exists(FILE_NAME) = True Then

     Do While objReader.Peek() <> -1
        txtProps = objReader.ReadLine()
            If i = 1 Then
               prop1 = txtProps
            End If
            If i = 2 Then
               prop2 = txtProps
            End If
            If i = 3 Then
               prop3 = txtProps
            End If
        i = i + 1
        Loop

tbxProp1.Text = prop1
tbxProp2.Text = prop2
tbxProp3.Text = prop3

I'm thinking there is a programtically more efficient way to write this. If you have some thoughts, please do share!

As always, thanks for your responses.

Don

Hi Don,
I would remove lines 21,22 and 23.
Change line 10 to tbxProp1.Text = txtProps
Do the same on line 13 and 16. Well... watch out for the "index" of course.
Perhaps an if else if construct would be better here, but not sure about that.

ddanbe,

Your suggestions make sense. I should have thought of inserting the data directly into the textboxes.

I'm not sure using If and Else is needed. It's going to be 1, 2 or 3 no matter what, albeit I'm actually having to insert data into 20 textsboxes (there are 20 different ID's in the actual text file).

As always, thanks for the help.

Don

You can reference controls dynamically by name by using

Me.Controls(name)

So you could do something like

Dim data() As String = {"box1", "box2", "box3"}

For i = 0 To 2
    Me.Controls("textBox" & (i + 1)).Text = data(i)
Next

If you name your controls tbxProp0, tbxProp1, tbxProp2 then you can do

Dim data() As String = {"box1", "box2", "box3"}

For i = 0 To 2
    Me.Controls("tbxProp" & i).Text = data(i)
Next

Not exactly like your code (I'm not doing any input) but you should get the idea.

DO you have a limited numbers of IDNos? Like only Two Or Three IDNos.
If you know you have only one, two or three,,,,,,,ten IDNos, You can do this by above processes by taking textboxes at designtime.

But if you do not know how many IDNos you have? How do you manage it?
To solve this you must create textboxes dynamically. Create TextBox Dynamically and show it.

Public Class Form1

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

        Dim szX As New Size(120, 30)
        Dim ptX As New Point(10, 10)

        For i As Integer = 1 To 10
            Dim txt As New TextBox

            txt.Name = "TextBox" & i
            txt.Size = szX
            txt.Location = ptX
            ptX.Y += txt.Height
            Me.Controls.Add(txt)
            txt.Text = txt.Name
        Next
    End Sub
End Class

This is an example.
Hope this idea can help you.

Just a thought, ID numbers aren't usually edited unless you're creating them. This would lead me to postulate that you're only displaying them. Perhaps a multiline textbox or a listbox to display all the ID numbers in one control would work, or if other data needs to be displayed with them, a grid control like a listview or datagridview would work. One advantage is that the ID numbers will be in a collection and much easier to access and maintain if the number of ID numbers changes later.

Group, there are actually a total of 20 ID's. Consequently I have 20 textboxes. Since I last updated, I've also created 20 labels. I've done this as I want to display the 20 ID's (as labels) and also to allow them to edit the list (via textboxes).

Here's what I've written:

        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        Dim txtProps As String
        Dim i As Integer = 1
        If System.IO.File.Exists(FILE_NAME) = True Then

            Do While objReader.Peek() <> -1
                txtProps = objReader.ReadLine()
                If i = 1 Then
                    tbxProp1.Text = txtProps
                    lblProp1.Text = txtProps
                End If
                If i = 2 Then
                    tbxProp2.Text = txtProps
                    lblProp2.Text = txtProps
                End If
                If i = 3 Then
                    tbxProp3.Text = txtProps
                    lblProp3.Text = txtProps
                End If
                i = i + 1
            Loop
        Else
            MsgBox("File Does Not Exist")
        End If
        objReader.Close()

My code works fine, but I'm assuming there might be a better, more efficient way to do it (I really want to learn to be a good code writer!).

As always, thanks for your input. You guys are the best!

Don

Your codes are quite right. But for 20 Labels & TextBoxes, the sub procedure would be very lengthy. So here a loop like @rev.Jim, would be very effective.
The codes should be like

        Dim txtProps As String
        Dim i As Integer = 1
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)

            Do While objReader.Peek() <> -1
                txtProps = objReader.ReadLine()

                Me.Controls(tbxProp & i+1).Text = txtProps
                Me.Controls(lblProp & i+1).Text = txtProps

                i = i + 1
            Loop

            objReader.Close()
        Else
            MsgBox("File Does Not Exist")
        End If

Rev. Jim, you indeed have the right idea. I'm going to use it. It's perfect. I knew there was a better way to write this. Thanks for the advice!

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.