需要帮助根据用户的名字和姓氏生成用户ID。 [英] Need help generating user ID based on user's first and last name.
问题描述
我必须为学校创建一个程序,允许用户输入他们的全名和年龄,当他们点击添加按钮时,全名将分为名字和姓氏列表框。年龄也会进入自己的列表框,但我需要帮助的是,当点击添加按钮时,用户ID,例如。 Sally West的ID将是swest001,需要使用函数过程创建,并且每次出现相同的用户ID时,用户名末尾的数字必须使用子过程增加1。此外,程序有删除按钮,清除所选用户的信息,名字/姓氏和年龄和用户ID,但我无法将年龄与其他信息一起删除。顺便说一句,为了说清楚,我只需要帮助用户ID并完成删除按钮。如果有人可以帮我这个,我会非常感激。
我尝试了什么:
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:
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
推荐答案
不确定我是否理解你的问题,但是...
为什么不创建包含给定用户的所有属性的类。物业将包括
- 名称
- 年龄
- 用户ID ...
这可能会简化这种情况,因为您不必单独为不同的用户处理单个数据项。
另一件事是ID增量。同样,用户名将是其中一个属性,当您添加新用户时,您可以使用一个简单的循环来查找相同用户名的最大ID。根据该信息,您可以为新用户分配下一个ID。
有关自定义类的更多信息,请查看如何创建课程和Visual Basic .NET或Visual Basic 2005中的对象 [ ^ ]
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[^]
除了解决方案#href=\"https://www.codeproject.com/script/Membership/View.aspx?mid=3874785\"> Wendelius [ ^ ] ...你必须使用列表(或者T) [ ^ ]来管理用户。例如:
首先,MyUser类声明:
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:
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:
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.
从<$中删除项目c $ c> FirstNameListBox 将改变其Selecte dIndex
。您应该将所选索引存储在变量中,并使用它来删除项目:
Removing an item from theFirstNameListBox
will alter itsSelectedIndex
. You should store the selected index in a variable, and use that to remove the items:
Dim index As Integer = FirstNameListBox.SelectedIndex
If index <> -1 Then
LastNameListBox.Items.RemoveAt(index)
FirstNameListBox.Items.RemoveAt(index)
AgeListBox.Items.RemoveAt(index)
End If
这篇关于需要帮助根据用户的名字和姓氏生成用户ID。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!