11 Replies - 4456 Views - Last Post: 11 February 2013 - 12:21 PM Rate Topic: -----

#1 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

No duplicates allowed in listbox

Posted 08 October 2011 - 07:28 PM

Hi i am currently having two problems with this assignment. The program allows the user to add new students to a listbox with a unique Id, lastname and test average. When adding a new student to the listbox, part of the assignment is to not have duplicate students added. I am currently stuck at trying to figure that out. I have figured out how not to have duplicate courses under the course information form but when trying similar code for adding a student i cant get anything i try to work. the next problem is when searching for a student from the list and clicking on the Course Information button, the course information added for that student needs to be displayed. My problem is getting the student searched information to show up right, but if i add three students and search for the first one i added and click on course information button it only shows the last student i added. this is my code for the whole program
Public Class Form1

	Private allStudent As New List(Of Student)
	Private currStudent As Student

	Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

		If txtIdNumber.Text.Length = 0 Then
			lblStatus.Text = "ID number cannot be blank"
			Return
		End If

		If txtLastName.Text.Length = 0 Then
			lblStatus.Text = "Last name cannot be blank"
			Return
		End If

		If txtAverage.Text.Length = 0 Then
			lblStatus.Text = "Invalid test average"
			Return
		End If

		Try
			currStudent = New Student(txtIdNumber.Text,
			txtLastName.Text, CDbl(txtAverage.Text))

			allStudent.Add(currStudent)
			btnInfo.Enabled = True
		Catch
			lblStatus.Text = "Cannot add duplicate Student ID to the collection"
		End Try

	End Sub

	Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click

		lstStudents.Items.Clear()

		For Each stu As Student In allStudent
			lstStudents.Items.Add(stu.ToString)
		Next
		lblStatus.Text = "Listing all Students"
	End Sub

	Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

		lstStudents.Items.Clear()

		Dim S As New Student(txtFindId.Text)
		Dim index As Integer = allStudent.IndexOf(S)
		If index <> -1 Then
			S = CType(allStudent(index), Student)
			lstStudents.Items.Add(allStudent(index).ToString)
			' update the text boxes, to be consistent 
			txtIdNumber.Text = S.IdNumber
			txtAverage.Text = S.TestAverage.ToString("n")
			txtLastName.Text = S.LastName

			lblStatus.Text = "Student ID " & txtFindId.Text & " was found"
		Else
			lblStatus.Text = "Student ID " & txtFindId.Text & " was not found"

		End If
	End Sub

	Private Sub btnInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo.Click

		CourseInformationForm.CurrStudent = currStudent
		CourseInformationForm.ShowDialog()

	End Sub
End Class

Public Class Student

	Public Property IdNumber As String
	Public Property LastName() As String
	Public Property TestAverage As Double

	Public Property Courses As Dictionary(Of String, Course)

	Public Sub New(ByVal pId As String,
	 Optional ByVal pLastName As String = "",
	 Optional ByVal pTestAverage As Double = 0.0)
		IdNumber = pId
		LastName = pLastName
		TestAverage = pTestAverage
		Courses = New Dictionary(Of String, Course)
	End Sub

	Public Overrides Function ToString() As String
		Return IdNumber & ", " & LastName _
		& ", Test average = " & TestAverage.ToString("n2")
	End Function

	Public Overrides Function Equals(ByVal obj As Object) As Boolean
		Dim other As Student = CType(obj, Student)
		Return IdNumber = other.IdNumber
	End Function
End Class

Public Class Course

	Public Property Id As String
	Public Property Credits As Integer
	Public Property Grade As Double

	Public Sub New(ByVal pCourseId As String,
		  ByVal pCredits As Integer,
		  ByVal pGrade As Double)
		Id = pCourseId
		Credits = pCredits
		Grade = pGrade
	End Sub

	Public Overrides Function ToString() As String
		Return String.Format("{0}, {1} CR, grade:{2}", Id, Credits, Grade)
	End Function
End Class

Public Class CourseInformationForm

	Public Property CurrStudent As Student

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

		lblStudent.Text = CurrStudent.IdNumber & ": " & CurrStudent.LastName
		FillCourseList()
	End Sub

	Private Sub FillCourseList()

		lstCourses.Items.Clear()
		txtCourseNumber.Text = String.Empty
		txtCredits.Text = String.Empty
		txtGrade.Text = String.Empty

		For Each pair As KeyValuePair(Of String, Course) In CurrStudent.Courses
			lstCourses.Items.Add(pair.Value.ToString)
		Next
	End Sub

	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

		Dim credits As Integer, grade As Double

		If txtCourseNumber.Text.Length = 0 Then

			errProvider.SetError(txtCourseNumber, "Course number cannot be blank")
			Return
		Else
			errProvider.SetError(txtCourseNumber, "")
		End If

		If Not (Integer.TryParse(txtCredits.Text, credits) _
		 AndAlso (credits >= 0 And credits <= 6)) Then

			errProvider.SetError(txtCredits, "Credits must be an integer between 0 and 6")
			Return
		Else
			errProvider.SetError(txtCredits, "")
		End If

		If Not (Double.TryParse(txtGrade.Text, grade) _
		  AndAlso (grade >= 0.0 And grade <= 4.0)) Then
			errProvider.SetError(txtGrade, "The grade must be a numeric value between 0.0 and 4.0")
			Return
		Else
			errProvider.SetError(txtGrade, "")
		End If

		Try
			CurrStudent.Courses.Add(txtCourseNumber.Text,
			 New Course(txtCourseNumber.Text, credits, grade))
		Catch
			errProvider.SetError(btnSave, "Cannot add a duplicate course")
		End Try

		FillCourseList()
	End Sub

	Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: No duplicates allowed in listbox

#2 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 08:15 PM

i figured out my second problem.
currStudent = S

solved the problem when put under btnFind right below txtLastName.text
Was This Post Helpful? 1
  • +
  • -

#3 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 08:37 PM

If your trying to avoid duplicate entries then you could simply loop through the list and check to see if they currently exist. If they do the set an error flag and notify the user. Something like this might work.

For Each item As Student In allStudent
   If item.IdNumber = txtIdNumber.Text Or item.LastName = txtLastName.Text Then
      blnDuplicateError = True
   End If
Next



It's not very efficient if you have a large list but if it's just a small one that it would do the trick.
Was This Post Helpful? 1
  • +
  • -

#4 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 09:05 PM

View PostJeremyC, on 08 October 2011 - 08:37 PM, said:

If your trying to avoid duplicate entries then you could simply loop through the list and check to see if they currently exist. If they do the set an error flag and notify the user. Something like this might work.

For Each item As Student In allStudent
   If item.IdNumber = txtIdNumber.Text Or item.LastName = txtLastName.Text Then
      blnDuplicateError = True
   End If
Next



It's not very efficient if you have a large list but if it's just a small one that it would do the trick.



thanks for the replay but i cant get that to work in my program. i am a little confused as to where to put it. and if possible i would like the error to be displayed as
lblStatus.Text = "Cannot add a duplicate Student ID to the collection."

Was This Post Helpful? 1
  • +
  • -

#5 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 09:25 PM

It would have to go in the btnAdd click handler. Use the loop to see if there is a duplicate, if there is the set a boolean error flag to true.

Based on the status of the error flag you can then choose a course of action with a simple if/else block. If it is true then display the error in whatever manner you want. If it is false then there is no duplicate entry and you can go ahead and add the new student to the list and the listbox.

Does that help any?
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 09:39 PM

This could aid your ListBox duplicate entries:
If Not ListBox1.Items.Contains(myvalues) Then
ListBox1.Items.Add(myvalues)
Else
ErrorFlag = True
'Label1.Text = "Error! Duplicate Entry"
End If


Regards,
J-Bo

This post has been edited by jimmyBo: 08 October 2011 - 09:42 PM

Was This Post Helpful? 1
  • +
  • -

#7 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 11:07 PM

hey guys i appreciate the feedback but my fault that i didnt realize this earlier. i need to use
Public Overrides Function Equals(ByVal obj As Object) As Boolean
		Dim other As Student = CType(obj, Student)
		Return IdNumber = other.IdNumber
	End Function

under my student class in order to compare the IdNumber trying to be added to see if it exists so no duplicates are allowed. what im having trouble with now is figuring out how to use that to check for no dupes.
Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: No duplicates allowed in listbox

Posted 08 October 2011 - 11:53 PM

Sorry, I'm confused.
Can you please clarify your question? I'm not sire I get you.
I did post the method to check if a box has duplicates, it should be simple enough to adapt it.
NC
Was This Post Helpful? 0
  • +
  • -

#9 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

Re: No duplicates allowed in listbox

Posted 09 October 2011 - 09:15 AM

what i mean is. i used this method to add students
Public Overrides Function ToString() As String
		Return IdNumber & ", " & LastName _
		 & ", Test average = " & TestAverage.ToString("n2")
	End Function


and i have to use this method to check for duplicates
Public Overrides Function Equals(ByVal obj As Object) As Boolean
		Dim other As Student = CType(obj, Student)
		Return IdNumber = other.IdNumber
	End Function

when a new student is added from this code
Try
			currStudent = New Student(txtIdNumber.Text,
			txtLastName.Text, CDbl(txtAverage.Text))

			allStudent.Add(currStudent)
			btnInfo.Enabled = True
		Catch
			lblStatus.Text = "Cannot add a duplicate Student ID to the collection."
		End Try

i need to figure out what to put in there to check for duplicates. i hope that cleared stuff up
i also attached my program if you want to see how it works.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#10 Pandop  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 08-October 11

Re: No duplicates allowed in listbox

Posted 09 October 2011 - 10:38 AM

i figured it out. sorry guys, now i have another problem. im trying to make the erroprovider go away in this statement
Try
			CurrStudent.Courses.Add(txtCourseNumber.Text,
			 New Course(txtCourseNumber.Text, credits, grade))
		Catch
			errProvider.SetError(btnSave, "Cannot add a duplicate course")
		End Try


once the course trying to be added is not a duplicate
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: No duplicates allowed in listbox

Posted 09 October 2011 - 11:05 AM

View PostPandop, on 09 October 2011 - 07:07 AM, said:

hey guys i appreciate the feedback but my fault that i didnt realize this earlier. i need to use
Public Overrides Function Equals(ByVal obj As Object) As Boolean
		Dim other As Student = CType(obj, Student)
		Return IdNumber = other.IdNumber
	End Function

You're misusing that overload (it used for check if two instances are the same, not comparison equality, for which you need to implement IComparable(Of T). See Tutorial. Replacing T with your class. Secondly you are also assuming the object being passed is always going to be a student, it can be any thing (a string, a integer, a whatimacallit.). I would suggest you use a TryCast instead.
Was This Post Helpful? 0
  • +
  • -

#12 prince0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 13

Re: No duplicates allowed in listbox

Posted 11 February 2013 - 12:21 PM

View PostPandop, on 09 October 2011 - 10:38 AM, said:

i figured it out. sorry guys, now i have another problem. im trying to make the erroprovider go away in this statement
Try
			CurrStudent.Courses.Add(txtCourseNumber.Text,
			 New Course(txtCourseNumber.Text, credits, grade))
		Catch
			errProvider.SetError(btnSave, "Cannot add a duplicate course")
		End Try


once the course trying to be added is not a duplicate



i have the same problem, could i see what u have done pls...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1