Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to create a program for school that allows the user to type their full name and age and when they click the add button the full name will be separated into first and last name listboxes. The age will also go into its own listbox but what i need help with is that when the add button is clicked a user ID, ex. Sally West's ID would be swest001, needs to be created using a function procedure and every time the same user ID appears the number at the end of the username has to be increased by one using a sub procedure. also, the program has remove button that clears the selected user's info, first/last name and age and user ID, but i can't get the age to be removed together with the other info. Btw, just to make it clear, i just need help with the user ID and completing the remove button. If anyone can help me with this, i would greatly appreciate it.

What I have tried:

VB
Public Class LA55Form
    Private NumberOfUsers As Integer
    Private AgeInteger As Integer
    Private TotalAge As Integer


    Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
        Dim FullNameString As String
        Dim UserIDNameString As String
        Dim FirstNameString As String
        Dim LastNameString As String
        Dim AgeString As String

        Dim CommaPositionInteger As Integer



        'Read fullname
        FullNameString = FullNameTextBox.Text

        'Read age
        AgeString = AgeTextBox.Text

        If Integer.TryParse(AgeTextBox.Text, AgeInteger) Then
            If AgeInteger < 15 Or AgeInteger > 90 Then
                MessageBox.Show("Age must be greater than 15 or less than 90.", "Data Entry Error",
                 MessageBoxButtons.OK, MessageBoxIcon.Information)
                With AgeTextBox
                    .Focus()
                    .SelectAll()
                End With
                Exit Sub
            End If
        Else
            MessageBox.Show("Quantity must be numeric.", "Data Entry Error",
             MessageBoxButtons.OK, MessageBoxIcon.Information)
            With AgeTextBox
                .Focus()
                .SelectAll()
            End With
            Exit Sub
        End If

        'Trim fullname
        FullNameString = FullNameString.Trim()

        'Check for no input
        If FullNameString = String.Empty Then
            '   Display error message
            MessageBox.Show("No name entered - retry", "input error", MessageBoxButtons.OK, MessageBoxIcon.Error)


            Exit Sub
        End If

        'Search for ", "
        CommaPositionInteger = FullNameString.IndexOf(", ")

        'Check for missing comma and space
        If CommaPositionInteger = -1 Then
            '   Display error message
            MessageBox.Show("Missing comma and space in name", "input error", MessageBoxButtons.OK, MessageBoxIcon.Error)


            Exit Sub

        End If

        'Extract LastName
        LastNameString = FullNameString.Substring(0, CommaPositionInteger)

        'Extract Firstname
        FirstNameString = FullNameString.Substring(CommaPositionInteger + 2)


        'place names in list boxes
        LastNameListBox.Items.Add(LastNameString)
        FirstNameListBox.Items.Add(FirstNameString)

        'place Age in list box
        AgeListBox.Items.Add(AgeString)

        

        NumberOfUsers += 1
        TotalAge += AgeInteger



    End Sub

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
        Dim averageDecimal As Decimal
        Dim output As String = ""

        averageDecimal = TotalAge / NumberOfUsers

        output = "Total Number of Users: " & NumberOfUsers.ToString() & vbCrLf &
            "Average Age of Users: " & averageDecimal.ToString()


        MessageBox.Show(output, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Close()
    End Sub

    Private Sub FirstNameListBox_Click(sender As Object, e As EventArgs) Handles FirstNameListBox.Click
        'activate remove button
        RemoveButton.Enabled = True

    End Sub


    Private Sub RemoveButton_Click(sender As Object, e As EventArgs) Handles RemoveButton.Click
        'check for item selected
        If FirstNameListBox.SelectedIndex <> -1 Then
            LastNameListBox.Items.RemoveAt(FirstNameListBox.SelectedIndex)
            FirstNameListBox.Items.RemoveAt(FirstNameListBox.SelectedIndex)
            AgeListBox.Items.RemoveAt(FirstNameListBox.SelectedIndex)
        End If



        RemoveButton.Enabled = False
    End Sub
End Class
Posted
Updated 25-Apr-18 8:10am

Not sure if I understood your question correctly, but...

Why not create a class that holds all the properties for a given user. The properties would include
- name
- age
- user id ...

This would probably simplify the situation since you don't have to handle individual data items for different users separately.

Another thing is the ID incremental. Again the user name would be one of the properties and when you add a new user, you could use a simple loop to find maximum id for the same usernames. Based on that info you can assign the next ID to the new user.

For more info about the custom classes, have a look at How to create classes and objects in Visual Basic .NET or in Visual Basic 2005[^]
 
Share this answer
 
In addition to solution #1 by Wendelius[^]... you have to use List(Of T)[^] to manage users. For example:

First of all, MyUser class declaration:
VB.NET
Public Class MyUser
	Private sfullName As String = String.Empty
	Private sfirstName As String = String.Empty
	Private slastName As String = String.Empty
	Private slogin As String = String.Empty
	Private iage As Byte = 0

	Public Sub New(_fullName As String, _age As Integer)
		sfullName = _fullName
		iage = _age
		FullNameToParts()
	End Sub
	
	Public Property FullName As String
		Get
			Return sfullName
			FullNameToParts()
		End Get
		Set (value As String)
			sfullname = value
		End Set
	End Property

	Private Sub FullNameToParts()
		Dim sParts As String() = sfullName.Split(New String(){", "}, StringSplitOptions.RemoveEmptyEntries) 
		If sParts.Length < 2 Then
			Throw New Exception("A user's full name have to contain ', ' (comma and space)!")
		Else
			sfirstName = sParts(1)
			slastName = sParts(0)
			slogin = String.Concat(sfirstName.ToLower().Substring(0,1), slastName.ToLower())
		End If
	End Sub

	Public Property Age As Byte
		Get
			Return iage
		End Get
		Set (value As Byte)
			iage = value
		End Set
	End Property
	
	Public ReadOnly Property Login As String
		Get
			Return slogin
		End Get
	End Property
End Class


Usage:

VB.NET
Sub Main
	
	Dim users As List(Of MyUser)  = New List(Of MyUser)
	users.Add(New MyUser("Doe, John", 22))
	users.Add(New MyUser("Doe, Joe", 19))
	
	Console.WriteLine("Count of users: {0}", users.Count)
	For Each mu As MyUser In users
		Console.WriteLine("{0} (login: {1})", mu.FullName , mu.Login)
	Next
	Console.WriteLine("Average age: {0}", users.Count)
	Console.WriteLine("Total age: {0}", users.Sum(Function(x) x.Age))
	
End Sub


Output:
Count of users: 2
Doe, John (login: jdoe)
Doe, Joe (login: jdoe)
Average age: 2
Total age: 41


This is for beginning.
 
Share this answer
 
Removing an item from the FirstNameListBox will alter its SelectedIndex. You should store the selected index in a variable, and use that to remove the items:
VB.NET
Dim index As Integer = FirstNameListBox.SelectedIndex
If index <> -1 Then
    LastNameListBox.Items.RemoveAt(index)
    FirstNameListBox.Items.RemoveAt(index)
    AgeListBox.Items.RemoveAt(index)
End If
 
Share this answer
 
Thanks for the ideas, i can definitely work with that.
 
Share this answer
 
Comments
Maciej Los 24-Apr-18 2:50am    
This is not an answer. Please delete it to avoid down-voting.
To post comment, use "Have a question or comment" widget under the solution.

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