Results 1 to 10 of 10

Thread: Two Dimentional Array....HELp?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    Two Dimentional Array....HELp?

    I am having difficulties grasping the concept / context of two dimensional arrays. For the assignment I am working on I am required to use radio buttons to allow a user to vote if they like or dislike an item from a combo box. The contents of the combo box have been populated through the IDE via tht item property. I understand how to set up my rows and columns, which will be, (0 to 4), (0 to 1). The trouble I am having is how to record and display the votes and have them correspond to the selected item from the combo box. Can anyone give me an example of how something like this works?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Two Dimentional Array....HELp?

    i don't understand how the array corresponds to the combobox items or how you intend to store votes in it?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Two Dimentional Array....HELp?

    try this, but i'm not sure how your voting system will work:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim votes(4, 1) As Object
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim items() As String = {"item1", "item2", "item3", "item4", "item5"}
    7.         For x As Integer = 0 To 4
    8.             votes(x, 0) = items(x) 'item name
    9.             votes(x, 1) = 0 'score
    10.         Next
    11.         ComboBox1.DataSource = items
    12.     End Sub
    13.  
    14. End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    Re: Two Dimentional Array....HELp?

    Ok here is a clearer example of what I am trying to do. The combobox has the following items: "Fillet Mignon", "Sirloin", and "Hamburger". I have a form that allows the user to choose one of these items from a comboBox then select "like" or "dislike" via a radioButton, then click on a button to submit their vote. The votes are then displayed in a listBox with the comboBox item on the left and the number of likes and dislikes to the right.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Two Dimentional Array....HELp?

    It'd be better to use a datatable for this. You should have 3 columns in the datatable: Name (string), LikeCount (integer), DislikeCount (integer). Populate the datatable with the names and set the like/dislike count all to zero at the beginning. You then can increment the count of the like or dislike when the user votes.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Two Dimentional Array....HELp?

    is this a homework question?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Two Dimentional Array....HELp?

    here's my solution using a 2d array:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim votes(4, 1) As Object
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim items() As String = {"item1", "item2", "item3", "item4", "item5"}
    7.         For x As Integer = 0 To 4
    8.             votes(x, 0) = items(x) 'item name
    9.             votes(x, 1) = 0 'score
    10.             ListBox1.Items.Add(String.Format("{0}{1}{2}{3}{4}", votes(x, 0).ToString, vbTab, "(", votes(x, 1).ToString, ")"))
    11.         Next
    12.         ComboBox1.DataSource = items
    13.     End Sub
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         Dim arrayFirstDIndex As Integer = -1
    17.         For x As Integer = 0 To 4
    18.             If votes(x, 0).ToString = ComboBox1.Text Then
    19.                 arrayFirstDIndex = x
    20.                 Exit For
    21.             End If
    22.         Next
    23.         If RadioButton1.Checked Then
    24.             votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) + 1
    25.         ElseIf RadioButton2.Checked Then
    26.             votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) - 1
    27.         End If
    28.         ListBox1.Items(arrayFirstDIndex) = String.Format("{0}{1}{2}{3}{4}", votes(arrayFirstDIndex, 0).ToString, vbTab, "(", votes(arrayFirstDIndex, 1).ToString, ")")
    29.         RadioButton1.Checked = False
    30.         RadioButton2.Checked = False
    31.     End Sub
    32.  
    33. End Class

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Two Dimentional Array....HELp?

    found a mistake. here's an improvement:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim arrayFirstDIndex As Integer = ComboBox1.SelectedIndex
    3.     If RadioButton1.Checked Then
    4.         votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) + 1
    5.     ElseIf RadioButton2.Checked Then
    6.         votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) - 1
    7.     End If
    8.     ListBox1.Items(arrayFirstDIndex) = String.Format("{0}{1}{2}{3}{4}", votes(arrayFirstDIndex, 0).ToString, vbTab, "(", votes(arrayFirstDIndex, 1).ToString, ")")
    9.     RadioButton1.Checked = False
    10.     RadioButton2.Checked = False
    11. End Sub

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    Re: Two Dimentional Array....HELp?

    Here is what I have came up with so far. It should provide some context as to where my intellect/logic is lacking.
    Code:
    Public Class FoodSurveryForm
        Dim votes As String(,) = New String(0 To 3, 0 To 1) {}
        Dim displayCount As Integer = 0
    
        ' handles Food Survey Form's Load event
        Private Sub FoodSurveryForm_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
    
            foodsComboBox.SelectedIndex = 0 ' select first food in list
    
        End Sub ' FoodSurveryForm_Load
    
        Private Sub voteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles voteButton.Click
    
            ' local integer variable index
            Dim index As Integer = 0
            Dim count As Integer = 1
            Dim foods As String() = New String() {"Cheese Pizza", "Hamburger", _
                                                 "Fish Sticks", "Mystery Meat"}
            'clear the resultsListBox
            resultsListBox.Items.Clear()
    
            ' populate header
            resultsListBox.Items.Add("Food" & ControlChars.Tab & ControlChars.Tab _
                                    & "Like" & ControlChars.Tab & ControlChars.Tab & "Dislike")
    
            ' make a For Loop to count items Liked or Disliked
            For count = 0 To 3
    
                ' If statement that checks radio button
                If likeRadioButton.Checked Then
                    resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab & index)
                    index = (count + 1)
                ElseIf dislikeRadioButton.Checked Then
                    resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab & _
                       ControlChars.Tab & ControlChars.Tab & index)
                    index = (count + 1)
                End If
            Next
        End Sub
    End Class ' FoodSurveryForm

  10. #10
    Member
    Join Date
    Sep 2010
    Posts
    38

    Re: Two Dimentional Array....HELp?

    Just modified a little in .paul.'s Code..


    Code:
     Dim votes As String(,) = New String(3, 2) {}
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim items() As String = {"Cheese Pizza", "Hamburger", "Fish Sticks", "Mystery Meat"}
            ListBox1.Items.Add("Food" & ControlChars.Tab _
                                           & ControlChars.Tab & "Like" & ControlChars.Tab & "Dislike")
            For x As Integer = 0 To 3
                votes(x, 0) = items(x) 'item name
                votes(x, 1) = 0 'Like score
                votes(x, 2) = 0 'Dislike Score
    
                ListBox1.Items.Add(String.Format("{0}{1}{2}{3}{4}{5}{6}", votes(x, 0).ToString, vbTab, "(", votes(x, 1).ToString, ")", vbTab, "(" & votes(x, 2).ToString & ")"))
            Next
            ComboBox1.DataSource = items
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not ComboBox1.SelectedIndex = -1 Then 'Check if there is selected item in combo box
                Dim index As Integer = ComboBox1.SelectedIndex
                If rbLike.Checked Then
                    votes(index, 1) += 1
                ElseIf rbDisLike.Checked Then
                    votes(index, 2) += 1
                End If
            End If
            ListBox1.Items.Clear()
            ListBox1.Items.Add("Food" & ControlChars.Tab & ControlChars.Tab _
                                    & "Like" & ControlChars.Tab & "Dislike")
            For i As Integer = 0 To 3
                ListBox1.Items.Add(String.Format("{0}{1}{2}{3}{4}{5}{6}", votes(i, 0).ToString, vbTab, "(", votes(i, 1).ToString, ")", vbTab, "(" & votes(i, 2).ToString & ")"))
            Next
    
        End Sub
    because walkntall wants to view the number of likes and dislike... and I prefer you use listview instead of a listbox...
    Last edited by palautot; Oct 30th, 2010 at 12:12 AM. Reason: Modified the Code for better UI..used some code of walkntall
    Please Rate me If I helped.

    Do not just copy paste codes, try to understand and learn from it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width