Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,470 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,993 people online right now. Registration is fast and FREE... Join Now!




List Box selection

 
Reply to this topicStart new topic

List Box selection, Odd/Even add them together

candicemae
21 Jan, 2008 - 08:35 PM
Post #1

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 2


My Contributions
hello, my problem is with the calcbutton. I want it to take all of the numbers inputed by the users and give me a running total in the evenlabel and oddlabel labels I have created. I need for the code to select all the items in the listbox, decide which is odd and which is even and add the odds and evens.

I have given up now after hours of tries, here is what I am left with. Thanks.
Candice


CODE
Public Class Form6

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

    End Sub

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()

    End Sub

    Private Sub clearbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearbutton.Click

        ListBox1.Items.Clear()


    End Sub

    Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
        If ListBox1.SelectedIndex >= 0 Then
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
    End Sub

    Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click
        Dim isconverted As Boolean
        Dim val As Integer
        isconverted = Integer.TryParse(inputtextbox.text, val)
        If isconverted Then
            ListBox1.Items.Add(inputtextbox.Text)
            inputtextbox.Clear()
            inputtextbox.Focus()
        Else : MessageBox.Show("Invalid Input. Enter and Integer >0.", "Invalid Input", MessageBoxButtons.OK)
            inputtextbox.Focus()
            inputtextbox.Clear()


        End If
    End Sub

    Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim val As Integer
        Dim totalevens As Integer = 0
        Dim totalodds As Integer = 0
        Dim max As Integer = ListBox1.Items.Count - 1

        For i As Integer = 0 To max
            ListBox1.SelectedIndex = 1
            val = Integer.Parse(ListBox1.SelectedItem.ToString)
            If val Mod 2 = 0 Then
                evenlabel.Text = 1
            Else
                oddlabel.Text = 1
            End If



        Next


    End Sub
End Class

User is offlineProfile CardPM
+Quote Post

Redian
RE: List Box Selection
21 Jan, 2008 - 09:49 PM
Post #2

New D.I.C Head
*

Joined: 28 Dec, 2007
Posts: 22

CODE
Public Class Form6

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

    End Sub

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()

    End Sub

    Private Sub clearbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearbutton.Click

        ListBox1.Items.Clear()


    End Sub

    Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
        If ListBox1.SelectedIndex >= 0 Then
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
    End Sub

    Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click
        Dim isconverted As Boolean
        Dim val As Integer
        isconverted = Integer.TryParse(inputtextbox.text, val)
        If isconverted Then
            ListBox1.Items.Add(inputtextbox.Text)
            inputtextbox.Clear()
            inputtextbox.Focus()
        Else : MessageBox.Show("Invalid Input. Enter and Integer >0.", "Invalid Input", MessageBoxButtons.OK)
            inputtextbox.Focus()
            inputtextbox.Clear()


        End If
    End Sub

    Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim val As Integer
        Dim totalevens As Integer = 0
        Dim totalodds As Integer = 0
        Dim max As Integer = ListBox1.Items.Count - 1

        For i As Integer = 0 To max
            ListBox1.SelectedIndex = i
            val = Integer.Parse(ListBox1.SelectedItem.ToString)
            If val Mod 2 = 0 Then
                val = val(evenlabel.Text) + val
                evenlabel.Text = val
            Else
                val = val(oddlabel.Text) + val
                oddlabel.Text = val
            End If



        Next


    End Sub
End Class


I think that should do it for you. It looks like you had just confused what you wanted it to set to. You were selecting index 1 which meant you were only taking the second item in the list box, and then continuously writing "1" to the textboxes. I think what I did matched everything up the way you need t.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: List Box Selection
21 Jan, 2008 - 11:21 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,300



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That will most certainly work. The only correction I would make to what Redian suggested is to avoid using the Val() function as this is a leftover remnant of VB6. I would instead suggest using the Integer.Parse or Convert.ToInt32 method.

It is also good programming to cast the result back to a String, since this is what the Text property is expecting.
CODE

    Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim val As Integer
        Dim totalevens As Integer = 0
        Dim totalodds As Integer = 0
        Dim max As Integer = ListBox1.Items.Count - 1

        For i As Integer = 0 To max
            ListBox1.SelectedIndex = i
            val = Integer.Parse(ListBox1.SelectedItem.ToString)
            If val Mod 2 = 0 Then
                evenlabel.Text = (Integer.Parse(evenlabel.Text) + val).ToString
            Else
                oddlabel.Text = (Integer.Parse(oddlabel.Text) + val).ToString
            End If
        Next i
    End Sub

User is online!Profile CardPM
+Quote Post

Redian
RE: List Box Selection
22 Jan, 2008 - 06:00 AM
Post #4

New D.I.C Head
*

Joined: 28 Dec, 2007
Posts: 22

I blame my 9th grade Computer Programming VB teacher. >.>
User is offlineProfile CardPM
+Quote Post

candicemae
RE: List Box Selection
22 Jan, 2008 - 03:45 PM
Post #5

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 2


My Contributions
QUOTE(jayman9 @ 22 Jan, 2008 - 12:21 AM) *

That will most certainly work. The only correction I would make to what Redian suggested is to avoid using the Val() function as this is a leftover remnant of VB6. I would instead suggest using the Integer.Parse or Convert.ToInt32 method.

It is also good programming to cast the result back to a String, since this is what the Text property is expecting.
CODE

    Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim val As Integer
        Dim totalevens As Integer = 0
        Dim totalodds As Integer = 0
        Dim max As Integer = ListBox1.Items.Count - 1

        For i As Integer = 0 To max
            ListBox1.SelectedIndex = i
            val = Integer.Parse(ListBox1.SelectedItem.ToString)
            If val Mod 2 = 0 Then
                evenlabel.Text = (Integer.Parse(evenlabel.Text) + val).ToString
            Else
                oddlabel.Text = (Integer.Parse(oddlabel.Text) + val).ToString
            End If
        Next i
    End Sub



So I tried this but makes the program crash, it tells me "Input string was not in a correct format." Any ideas?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: List Box Selection
22 Jan, 2008 - 05:07 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,300



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That is because you need to check if there is value in the TextBoxes before performing the calculation.

If the TextBox is empty then you cannot parse a number that isn't there and it will throw that error message.

Anytime that you parse a value from something, you need to make sure there is actually a value there to parse. Otherwise you will encounter this problem often.

Do you see where else in your code you will possibly run into the same problem besides the two Textboxes??

I will show you how to fix one part of the If statement, apply the same logic to the Else portion.
CODE

            If val Mod 2 = 0 Then
                If evenlabel.Text.Equals("") Then
                    evenlabel.Text = val.ToString
                Else
                    evenlabel.Text = (Integer.Parse(evenlabel.Text) + val).ToString
                End If

            Else

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 02:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month