Question Creating a textbox list using textboxes as input

grammasta

New member
Joined
Aug 19, 2011
Messages
1
Location
Larvik, Norway, Norway
Programming Experience
Beginner
Hey guys, sorry for the noob question...but just getting into visual studio programming.

What I'm trying to do is make a list of members in a read only textbox getting the data from two textboxes (first and last name) and a 'save' button.
I've made a class named 'Deltaker' (Members in english) to store data and a list 'resultater' (eng: results) using the 'Deltaker' class.

I'm trying to get user input from two textboxes 'tbxFornavn' and 'tbxEtternavn' and I use my class' public function 'Navn' to display it in the textbox 'tbxHistory'.

I've got a sub procedure to add the input to the list and clear the textboxes and enable another reset button.

So for the save button click event I run the sub procedure and I've written a for next loop to display the results from the list. The first name I enter is all good, but with the second name entry both the first and the second name gets listed as well as the first name that is already there. I thought about using the .clear function to clear the list after each input, but I also want to incorporate laptimes in the list and display the best time in a lable at the bottom of the form. Maybe also sort the list, so I need the data in the list to stay intact.

Can someone help me out (I'm sure it's a simple solution, I just feel like I'm going in circles here). Thanks guys.

VB.NET:
Public Class Form1
    Dim resultater As New List(Of Deltaker)


    Private Sub AddDeltaker()
        resultater.Add(New Deltaker(tbxFornavn.Text, tbxEtternavn.Text)) ', CDec(tbxRundetider.Text)))
        btnReset.Enabled = True
        tbxFornavn.Text = ""
        tbxEtternavn.Text = ""
        tbxRundetider.Text = ""
        tbxFornavn.Focus()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        AddDeltaker()
        For n As Integer = 0 To resultater.Count - 1
            tbxHistory.Text += resultater(n).Navn & vbCrLf
            '    'resultater(n).Tid & "   " &
        Next


        lblTotLopere.Text = "Løpere Totalt: " & resultater.Count
    End Sub
End Class

My class :

VB.NET:
Public Class Deltaker
    Private _fnavn As String
    Private _enavn As String
    'Private _tid As Decimal


    Public Sub New(ByVal fnavn As String, ByVal enavn As String) ', ByVal tid As Decimal)
        _fnavn = fnavn
        _enavn = enavn
        '_tid = tid
    End Sub


    Public Property Fornavn() As String
        Get
            Return _fnavn
        End Get
        Set(ByVal value As String)
            _fnavn = value
        End Set
    End Property


    Public Property Etternavn() As String
        Get
            Return _enavn
        End Get
        Set(ByVal value As String)
            _enavn = value
        End Set
    End Property


    'Public Property Tid() As Decimal
    '    Get
    '        Return _tid
    '    End Get
    '    Set(ByVal value As Decimal)
    '        _tid = value
    '    End Set
    'End Property


    Public Function Navn() As String
        Return _enavn & ", " & _fnavn
    End Function
End Class
 
What is happening is that you have not cleared the 'tbxHistory' before you are adding to it again. You don't need to clear the list, but you need to clear the textbox just before you loop through the list again.
VB.NET:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        AddDeltaker()
        tbxHistory.Text = ""
        For n As Integer = 0 To resultater.Count - 1
            tbxHistory.Text += resultater(n).Navn & vbCrLf
            '    'resultater(n).Tid & "   " &
        Next


        lblTotLopere.Text = "Løpere Totalt: " & resultater.Count
    End Sub
 
Back
Top