I have been working on a pythagorean theorem calculator and am actually stuck on the final bit of code. What I am trying to do is check the values (integers) inside a listbox, remove duplicate number (both values), and input one of the values into a textbox.

Here is an example of what I am doing:

LISTBOX VALUES
2
2
3
5
5
7

When the program runs, check listbox value #1 and listbox value #2, if listbox value #1 and #2 match (2 & 2), input that value (not both duplicates) into a variable, remove both of the duplicate values, continue checking the rest of the values, and finally multiply the leftover values. I am sure that sounds confusing. Here is what the final output will look like

LISTBOX VALUES
3
7

Variable1 = 2 * 5
Variable2 = 3 * 7

I am sorry if that is difficult but I cannot figure out the proper syntax to get it working.

Thanks in advance for any help.

## All 11 Replies

And whai in case if you have numbers:
2,2,2,3,3,3,4

In that case, just the listbox would look as follows:

2 - (Removed)
2 - (Removed)
2
3 - (Removed)
3 - (Removed)
3
4

Variable1 = 2 * 3
Variable2 = 2 * 3 * 4

I did a code, but according to your last example this is not just it:

Take a look:

``````Public Sub New()
InitializeComponent()
listBox1.Items.AddRange(New Object() {2, 2, 3, 5, 5, 7})
End Sub

Private Sub button1_Click(sender As Object, e As EventArgs)
Dim list As New List(Of Integer)()

For i As Integer = 0 To listBox1.Items.Count - 1
Dim value1 As Integer = Integer.Parse(listBox1.Items(i).ToString())
For j As Integer = i + 1 To listBox1.Items.Count - 1
Dim value2 As Integer = Integer.Parse(listBox1.Items(j).ToString())
If value1 = value2 Then
RemoveItems(value1)
End If
Next
Next

'for the end, you do the math operations over the values:
Dim result1 As Integer = 1
For Each number As Integer In list
result1 *= number
Next
Dim result2 As Integer = 1
For i As Integer = 0 To listBox1.Items.Count - 1
result2 *= Integer.Parse(listBox1.Items(i).ToString())
Next

MessageBox.Show("Variable 1 = " & result1 & vbLf & "Variable 2 = " & result2)
End Sub

Private Sub RemoveItems(value As Integer)
For i As Integer = 0 To listBox1.Items.Count - 1
listBox1.Items.Remove(value)
Next
End Sub``````

2 - (Removed)
2 - (Removed)
2
3 - (Removed)
3 - (Removed)
3
4

If I understood, it should be like that:
Variable1 = 2 * 3
Variable2 = 3 (only 3 left)

----------------------------------
In your upper example there were 3 and 7 left, and you multiplay them and got 21!

Ok... The listbox is going to be populated via a loop. The numbers in the listbox are going to be prime factors, e.g. 312 will populate listbox with 2, 2, 2, 3, 13.

Listbox Values
2
2
2
3
13

When you run the next (For, If, whatever the proper syntax is) block of code, it will check the listbox value #1 (2) against listbox value #2 (2).

If they are the same, input value #1 into a variable and then remove value #1 and value #2 from the listbox.

Listbox Values
2
3
13

Variable1 = 2

Then the listbox will be left with 2, 3, 13. Once again, check value #1 (2) and value #2 (3) to see if they are equal. Since they are not equal, input value #1 (2) into a separate variable.

Listbox Values
2
3
13

Variable1 = 2
Variable2 = 2

Move on and check value #2 (3) and value #3 (13). Since they are not equal, input value #2 into Variable2 and move on.

Listbox Values
2
3
13

Variable1 = 2
Variable2 = 2 * 3

Check value #3 (13) and next value (No value). Since no value #4, input value #3 into Variable2.

Variable1 = 2
Variable2 = 2 * 3 * 13

I hope this explains what I am trying to do better.

Heres another version.

``````Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Use linq to get the non duplicated list
Dim Distinct = From Num In ListBox1.Items _
Distinct

'Copy the distinct list of numbers
Dim Items() As Object = Distinct.ToArray

'Clear the listbox and add all the numbers except the
'first number.
ListBox1.Items.Clear()

If Items.Length > 0 Then
'Get the first number in the list that is not
'in the textbox.
Dim FirstInList As Integer = CInt(Items(0))
Dim TheRemaining As Integer
For i = 1 To UBound(Items)
'Multiply all the remaining numbers in the listbox
'To the first number
TheRemaining = CInt(FirstInList * CInt(Items(i)))
Next
End If

End Sub``````

O`right, this will do it:

``````Public Sub New()
InitializeComponent()
listBox1.Items.AddRange(New Object() {2, 2, 2, 3, 13})
End Sub

Private Sub button2_Click(sender As Object, e As EventArgs)
Dim list As New List(Of Integer)()
For i As Integer = 0 To listBox1.Items.Count - 1
Dim value1 As Integer = Integer.Parse(listBox1.Items(i).ToString())
For j As Integer = i + 1 To listBox1.Items.Count - 1
Dim value2 As Integer = Integer.Parse(listBox1.Items(j).ToString())
If value1 = value2 Then
RemoveItems(value1, i, j)
End If
Next
Next

'for the end, you do the math operations over the values:
Dim result1 As Integer = 1
For Each number As Integer In list
result1 *= number
Next
Dim result2 As Integer = 1
For i As Integer = 0 To listBox1.Items.Count - 1
result2 *= Integer.Parse(listBox1.Items(i).ToString())
Next

MessageBox.Show("Variable 1 = " & result1 & vbLf & "Variable 2 = " & result2)
End Sub

Private Sub RemoveItems(value As Integer, a As Integer, b As Integer)
For i As Integer = 0 To listBox1.Items.Count - 1
If i = a Then
listBox1.Items.RemoveAt(a)
ElseIf i = b Then
listBox1.Items.RemoveAt(b)
End If
Next
End Sub``````

Let me know if its working (it does here).

I read the post wrong. My post is not what your looking for :(

Actually

After reading over the original post again, What does this have to do with a Pythag Thereom.

``````Private Function GetLength(ByVal sideA As Double, ByVal sideB As Double)
Return Math.Sqrt(Math.Pow(sideA, 2) + Math.Pow(sideB, 2))
End Function``````

What does all this list box removal and reading have to do with 2 numbers?

I haven't seen one instance of the pythag thereom in it.

Try this one

``````Dim Complete As Boolean = False
Dim Com As Boolean= False
Dim PlaceNum As String
Dim i As Integer
Dim j As Integer

Dim Num1 As Integer=1
Dim Num2 As Integer=1

Do While Complete = False
If listbox1.Items.Count > 1 Then
For j = 0 To listbox1.Items.Count - 2 Step 1
PlaceNum = listbox1.Items(j)
For i = j + 1 To listbox1.Items.Count - 1 Step 1
If PlaceNum = listbox1.Items (i) Then
Num1 = Num1 * Val(listbox1.Items (i))
listbox1.items.RemoveAt (i)
listbox1.items.RemoveAt (j)
Complete = False
Com = True
Exit For
Else
Com = False
End If
Next i

If listbox1.Items.Count = 1 Then
Complete = True
Exit For
End If

If Com = True Then
Exit For
End If

If j = listbox1.Items.Count - 2 Then
Complete = True
End If
Next j
Else
Complete = True
End If
Loop

For i = 0 To listbox1.Items.Count - 1 Step 1
Num2 = Num2 * listbox1.Items (i)
Next i

Textbox1.Text = Num1
Textbox2.Text = Num2``````
commented: The code was right on the nose. He figured out exactly what I needed. +1

The only reason I am using the listboxes and remove is because I want the unknown value to come out as 3 (sqrt) 2 instead of (sqrt) 18.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.