|
-
Oct 29th, 2010, 03:39 PM
#1
Thread Starter
Junior Member
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?
-
Oct 29th, 2010, 03:43 PM
#2
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 29th, 2010, 03:51 PM
#3
Re: Two Dimentional Array....HELp?
try this, but i'm not sure how your voting system will work:
vb Code:
Public Class Form1
Dim votes(4, 1) As Object
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim items() As String = {"item1", "item2", "item3", "item4", "item5"}
For x As Integer = 0 To 4
votes(x, 0) = items(x) 'item name
votes(x, 1) = 0 'score
Next
ComboBox1.DataSource = items
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 29th, 2010, 03:52 PM
#4
Thread Starter
Junior Member
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.
-
Oct 29th, 2010, 04:01 PM
#5
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 -
-
Oct 29th, 2010, 04:09 PM
#6
Re: Two Dimentional Array....HELp?
is this a homework question?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 29th, 2010, 04:11 PM
#7
Re: Two Dimentional Array....HELp?
here's my solution using a 2d array:
vb Code:
Public Class Form1
Dim votes(4, 1) As Object
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim items() As String = {"item1", "item2", "item3", "item4", "item5"}
For x As Integer = 0 To 4
votes(x, 0) = items(x) 'item name
votes(x, 1) = 0 'score
ListBox1.Items.Add(String.Format("{0}{1}{2}{3}{4}", votes(x, 0).ToString, vbTab, "(", votes(x, 1).ToString, ")"))
Next
ComboBox1.DataSource = items
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arrayFirstDIndex As Integer = -1
For x As Integer = 0 To 4
If votes(x, 0).ToString = ComboBox1.Text Then
arrayFirstDIndex = x
Exit For
End If
Next
If RadioButton1.Checked Then
votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) + 1
ElseIf RadioButton2.Checked Then
votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) - 1
End If
ListBox1.Items(arrayFirstDIndex) = String.Format("{0}{1}{2}{3}{4}", votes(arrayFirstDIndex, 0).ToString, vbTab, "(", votes(arrayFirstDIndex, 1).ToString, ")")
RadioButton1.Checked = False
RadioButton2.Checked = False
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 29th, 2010, 04:17 PM
#8
Re: Two Dimentional Array....HELp?
found a mistake. here's an improvement:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arrayFirstDIndex As Integer = ComboBox1.SelectedIndex
If RadioButton1.Checked Then
votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) + 1
ElseIf RadioButton2.Checked Then
votes(arrayFirstDIndex, 1) = CInt(votes(arrayFirstDIndex, 1)) - 1
End If
ListBox1.Items(arrayFirstDIndex) = String.Format("{0}{1}{2}{3}{4}", votes(arrayFirstDIndex, 0).ToString, vbTab, "(", votes(arrayFirstDIndex, 1).ToString, ")")
RadioButton1.Checked = False
RadioButton2.Checked = False
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 29th, 2010, 06:41 PM
#9
Thread Starter
Junior Member
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
-
Oct 29th, 2010, 11:49 PM
#10
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|