13 Replies - 2196 Views - Last Post: 12 October 2012 - 11:25 PM Rate Topic: -----

#1 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Duplicate Elimination/Numeric only

Posted 07 October 2012 - 02:20 PM

I hope someone is out there, I am having multiple problems and have tried 3 different sets of codes. A. I would only like numeric values. Here is the code I'm currently trying:
 

Public Class FrmDuplicateElimination


    Private Sub TxtEnter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtEnter.TextChanged
        If (IsNumeric(TxtEnter.Text)) Then

        Else
            MessageBox.Show = "Enter Only Numeric Values !"
            Exit Sub
        End If
    End Sub



For the second part of my question My TxtEnter is not entering a value into the LstBxNmbrEntered am I to string this to the LstBxNmbrEntered? My assignment to create a single array using only the numbers between 10 and 100 - (20 numbers only) eliminating duplicates. I have looked at other homework on here, and have gone back to my own code to perfect it instead of useing others codes. Thanks


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


        Dim txtEnter(20) As Integer
        Dim LstBxNmbrEntered As Integer
        Dim unique_numbersArray As Integer
        Dim result() As String

        ' Make the result array.  


        ReDim result(0 To UBound(txtEnter))


        ' Copy the first item into the result array.        
        result(1) = txtEnter(0)

        ' Copy the other items        
        unique_numbersArray = 1

        For LstBxNmbrEntered = 2 To UBound(txtEnter)
            If result(unique_numbersArray) <> txtEnter(LstBxNmbrEntered) Then
                unique_numbersArray = unique_numbersArray + 1
                result(unique_numbersArray) = txtEnter(LstBxNmbrEntered)

            End If

        Next LstBxNmbrEntered


        ' Remove unused entries from the result array.        
        ReDim Preserve result(0 To unique_numbersArray)

        ' Return the result array.        
        'RemoveDups = result  


    End Sub

    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click

        LstBxNmbrEntered.Items.Clear() ' clear numbersEnteredListBox 
        LstBxunique_numbersArray.Text = String.Empty ' clear Label  

    End Sub


End Class





I believe I may have posted this in the wrong forum? This could possibly be a vb.net issue or can it be solved in vb6?

Is This A Good Question/Topic? 0
  • +

Replies To: Duplicate Elimination/Numeric only

#2 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 395
  • Joined: 04-January 11

Re: Duplicate Elimination/Numeric only

Posted 07 October 2012 - 08:22 PM

I have created a simple project trying to do what you are trying to do. I hope this is what you need though I am not sure if I understood everything correctly.

so I have 4 main objects.

txtInput - textbox for user input
btnAddToList - button to submit input. The button clicked event will be used for processing and not the textchanged event of the textbox. I don't recommend the textchange for your problem here

lstNumbers-listbox for display. I am not sure what else you want with your listbox. Tell me if there is something else I missed or misunderstood.

lblCount - label to display how many numbers have already been entered so that we can track the current count.

For coding... declare global variables...

    Dim intNumbersEntered As Integer() = New Integer(19) {}
    Dim intTotalEntered As Integer = 0



Then using the btnAddToList click event,

Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click
        If intTotalEntered = 20 Then
            MessageBox.Show("You have already entered 20 numbers. No user input is allowed anymore.")
            txtInput.SelectAll()
            Exit Sub
        End If

        If IsNumeric(txtInput.Text.Trim()) Then
            Dim intUserInput As Integer = txtInput.Text.Trim()
            If intUserInput >= 10 And intUserInput <= 100 Then
                For i As Integer = 0 To intNumbersEntered.GetUpperBound(0)
                    If intUserInput = intNumbersEntered(i) Then
                        MessageBox.Show("This number has already been entered.")
                        txtInput.SelectAll()
                        Exit Sub
                    End If
                Next
                intNumbersEntered(intTotalEntered) = intUserInput
                lstNumbers.Items.Add(intUserInput)
                lblCount.Text = lstNumbers.Items.Count
                intTotalEntered += 1
            Else
                MessageBox.Show("Only numbers from 10 to 100 are allowed.")
            End If
        Else
            MessageBox.Show("Only numeric values are allowed.")
        End If
        txtInput.SelectAll()
    End Sub



Try doing this and tell us when you got it or at least closer to what you need.

This post has been edited by November-06: 07 October 2012 - 08:24 PM

Was This Post Helpful? 1
  • +
  • -

#3 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: Duplicate Elimination/Numeric only

Posted 08 October 2012 - 05:06 AM

For the first problem of Text Validation, i would suggest you to use NumericUpDown Control which is made for this purpose only :)

As for your second problem to remove duplicates, you don't really need that much of your code. LINQ adds a method to do this task for you named Distinct()

Eg:
Dim txtEnter(20) As Integer
'Add all items (including duplicates) to the array here
Dim result() As Integer = txtEnter.Distinct()


Was This Post Helpful? 1
  • +
  • -

#4 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 08 October 2012 - 05:33 AM

Sadly, my email didn't come through in time for me to see this and the other post, but that's beautiful. I'm going to go ahead and redo my project just for memorization and practice, I will be back with questions on how this works later.
Thanks so much! I enjoy seeing things work, more so than stumbling for hours, wondering what I'm doing incorrectly.! Thanks you too. I'll be back.

This post has been edited by AdamSpeight2008: 08 October 2012 - 12:26 PM
Reason for edit:: Snipping out useless quoted post

Was This Post Helpful? 0
  • +
  • -

#5 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 08 October 2012 - 12:13 PM

There should be a second text box that displays all numbers that are not duplicates. So I must somehow compare numbers and send the ones that are not duplicates to the next text box. But I see a code snippit below that the second DIC member (dotInSolution) has used. I'm not sure how to send that array of numbers to the second text box? This information will be helpful for my midterm! I have a spreadsheet/database assignment and I will be sending multiple obsects and creating possibly functions that I should start on this week. Any information on sending the array to the second box would be much appreciated. For now I'm going to play with your code to see what happens and how it works. Thanks!

This post has been edited by AdamSpeight2008: 08 October 2012 - 12:27 PM
Reason for edit:: Snipped Useless quoted post and duplication

Was This Post Helpful? 0
  • +
  • -

#6 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 08 October 2012 - 12:48 PM

Okay, I must have something named incorrectly, which could have been a problem to begin with. I have an evening math class to prepare for.



View PostNovember-06, on 07 October 2012 - 10:22 PM, said:

I have created a simple project trying to do what you are trying to do. I hope this is what you need though I am not sure if I understood everything correctly.

so I have 4 main objects.

txtInput - textbox for user input
btnAddToList - button to submit input. The button clicked event will be used for processing and not the textchanged event of the textbox. I don't recommend the textchange for your problem here

lstNumbers-listbox for display. I am not sure what else you want with your listbox. Tell me if there is something else I missed or misunderstood.

lblCount - label to display how many numbers have already been entered so that we can track the current count.

For coding... declare global variables...

    Dim intNumbersEntered As Integer() = New Integer(19) {}
    Dim intTotalEntered As Integer = 0



Then using the btnAddToList click event,

Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click
        If intTotalEntered = 20 Then
            MessageBox.Show("You have already entered 20 numbers. No user input is allowed anymore.")
            txtInput.SelectAll()
            Exit Sub
        End If

        If IsNumeric(txtInput.Text.Trim()) Then
            Dim intUserInput As Integer = txtInput.Text.Trim()
            If intUserInput >= 10 And intUserInput <= 100 Then
                For i As Integer = 0 To intNumbersEntered.GetUpperBound(0)
                    If intUserInput = intNumbersEntered(i) Then
                        MessageBox.Show("This number has already been entered.")
                        txtInput.SelectAll()
                        Exit Sub
                    End If
                Next
                intNumbersEntered(intTotalEntered) = intUserInput
                lstNumbers.Items.Add(intUserInput)
                lblCount.Text = lstNumbers.Items.Count
                intTotalEntered += 1
            Else
                MessageBox.Show("Only numbers from 10 to 100 are allowed.")
            End If
        Else
            MessageBox.Show("Only numeric values are allowed.")
        End If
        txtInput.SelectAll()
    End Sub



Try doing this and tell us when you got it or at least closer to what you need.

Was This Post Helpful? 0
  • +
  • -

#7 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 10 October 2012 - 09:21 PM

Okay, the code works wonderfully! with one exception. I have not figured out how to send the non-duplicate numbers to the uniquenumber_array box? I know it's lack of knowledge. Here's what I tried (using the deplicit code above it says : Object variable or With block variable not set. I think it's because I don't have my objects set properly? Here's what I tried: What should I do differently? Thanks

'This program should Eliminate Duplicates

Public Class DuplicateElimination
    Dim intunique_numbersArray
    Dim intNumbersEntered As Integer() = New Integer(19) {}
    Dim intTotalEntered As Integer = 0

    Private Sub AddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToList.Click
        If intTotalEntered = 20 Then
            MessageBox.Show("You have already entered 20 numbers. No user input is allowed anymore.")
            txtInput.SelectAll()

            Exit Sub
        End If

        If IsNumeric(txtInput.Text.Trim()) Then
            Dim intUserInput As Integer = txtInput.Text.Trim()
            If intUserInput >= 10 And intUserInput <= 100 Then

                For i As Integer = 0 To intNumbersEntered.GetUpperBound(0)
                    If intUserInput = intNumbersEntered(i) Then
                        'MessageBox.Show("This number has already been entered.")
                        'txtInput.SelectAll()
                        'Add all items (including duplicates) to the array here 
                        Dim result() As Integer = intunique_numbersArray.Distinct()
                    End If


                Next

                intNumbersEntered(intTotalEntered) = intUserInput
                lstNumbers.Items.Add(intUserInput)
                lblCount.Text = lstNumbers.Items.Count
                intTotalEntered += 1

            Else

                MessageBox.Show("Only numbers from 10 to 100 are allowed.")
            End If

        Else

            MessageBox.Show("Only numeric values are allowed.")
        End If

        txtInput.SelectAll()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#8 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 10 October 2012 - 09:35 PM

I need to edit the above post. I changed the name (result). Also you should be able to enter the same number up to 20 times if you want. My next question is: I have never seen the .text.trim before what exactly does that mean? IsNumeric means that it will only be a numeric input correct? And intNumbersEntered is where the array is held until it is placed in the lstNumbers box? I realize I may not have used proper terminology, wich I would also like for you to help me with. I understand what you are saying but learning how to say it back or communicate it on your level is also 1/2 my battle at this moment. I will get better! Promise :).

 For i As Integer = 0 To intNumbersEntered.GetUpperBound(0)
                    If intUserInput = intNumbersEntered(i) Then
                        'MessageBox.Show("This number has already been entered.")
                        'txtInput.SelectAll()
                        'Add all items (including duplicates) to the array here 
                        Dim lstNumbers() As Integer = intunique_numbersArray.Distinct()
                    End If


Was This Post Helpful? 0
  • +
  • -

#9 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Duplicate Elimination/Numeric only

Posted 10 October 2012 - 09:44 PM

I see a lot of problems trickling from the fact that there seems to be no data type conversion. Your complications would best be solved if you just converted data types. You can't compare for a numeric value from a string. Trim takes out the white spaces that are trailing the string entered.
Was This Post Helpful? 1
  • +
  • -

#10 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 10 October 2012 - 09:54 PM

Would your solution be to use a CInt or Val? How would you string a number from one place to another? What command would you use?
Was This Post Helpful? 0
  • +
  • -

#11 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Duplicate Elimination/Numeric only

Posted 11 October 2012 - 12:47 AM

I tend to use Integer.Parse but when I need to catch errors during conversion I use the TryParse function which returns a boolean of success/failure.

I'm not sure I understand what you mean by "string a number".
Was This Post Helpful? 1
  • +
  • -

#12 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Duplicate Elimination/Numeric only

Posted 11 October 2012 - 03:36 PM

string an array, is verbiage I picked up out of my book. It means to take a set of numbers or information and put it somewhere else. Str is the code.
Was This Post Helpful? 0
  • +
  • -

#13 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Duplicate Elimination/Numeric only

Posted 12 October 2012 - 12:22 AM

Okay then I guess I mean to ask, in what context would you need to take a number or set of numbers or information, and put it elsewhere? That might help me understand what you're asking a bit easier.
Was This Post Helpful? 0
  • +
  • -

#14 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: Duplicate Elimination/Numeric only

Posted 12 October 2012 - 11:25 PM

As per your request on the other thread:
I can pretty much explain TryParse here:
Dim result As Integer
Dim input As String = "789823"
Dim success As Boolean = Integer.TryParse(input, result)
If success = True Then
'work what ever you were going to do here
End If



Reference: http://msdn.microsof...02979c7.aspx#Y0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1