Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Error on randomizing strings in a list C#

CoolaiCoolai Posts: 1Member
I want to create a program that shows a random category (from the list of category I've created) with the right words in a message box whenever I click the button.
[img=http://i717.photobucket.com/albums/ww176/T3ZTAM3NT/1stSSonError_zpsb1007b5d.jpg]
The categories is randomized when I run it but the right word that should be with the category isn't correctly placed.
[img=http://i717.photobucket.com/albums/ww176/T3ZTAM3NT/2ndSSonError_zps1a838970.jpg]
Also, I know that the program will crash once the program reaches a negative index of a category or once all of the category is shown.

Code:
[code]namespace randomCategory
{
public partial class Form1 : Form
{

Random rand = new Random();
List categories = new List { "Book Titles", "Movie Titles", "Car Parts", "Human Body Parts", "Transportations" };


public Form1()
{
InitializeComponent();
listBox1.DataSource = categories;
}

public void selection()
{
// logic for setting a random category
int index = rand.Next(categories.Count);
var category = categories[index];


// logic for assigning the word for a category
switch (index)
{
case 0:
MessageBox.Show(category, "Harry Potter");
break;
case 1:
MessageBox.Show(category, "Summer Wars");
break;
case 2:
MessageBox.Show(category, "Bumper");
break;
case 3:
MessageBox.Show(category, "Eyes");
break;
case 4:
MessageBox.Show(category, "Boat");
break;
default:
MessageBox.Show("Empty!", "!!!");
break;
}

categories.RemoveAt(index);
}

private void button1_Click(object sender, EventArgs e)
{
selection();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
[/code]
Sign In or Register to comment.