I'm still fairly new to C# programming language. I have to create a airplane reservation program in which user will input the name and select the item (seat number) from each of two listboxes. First listbox include items namely A,B,C,D,E & second list box include items 1,2,3. There are total 15 seats-A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,C1,C2,C3,C4,C5,D1,D2,D3,D4,D5,E1,E2,E3,E4,E5. Program demands each seat to be selected once.If one seat is selected more than one time, message should pop up which says- this seat is already chosen.When I click on Book button -The passenger name and seat number should be printed in richtextbox1(one below "Show all" button).
I have coded an array to accept the listbox items to print the items in richtextbox along with passenger name. Everything is working fine except with code part(part which I have made comment) where I have coded if else inside for loop to check array elements with same values and message if condition is satisfied.
Here's my code:
private void book_button_Click(object sender, EventArgs e)
{
string[,] newarray = new string[5, 3];
if (textBox1.Text == "")
{
MessageBox.Show("Name is required");
}
if (listBox1.SelectedIndex == -1 && listBox2.SelectedIndex == -1)
{
MessageBox.Show("Seat Number is required");
}
else if (listBox1.SelectedIndex == -1 || listBox2.SelectedIndex == -1)
{
MessageBox.Show("Seat Number is required");
}
else
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
newarray[i, j] = listBox1.GetItemText(listBox1.SelectedItem) + listBox2.GetItemText(listBox2.SelectedItem);
listBox1.ClearSelected();
listBox2.ClearSelected();
richTextBox1.Text += "" + textBox1.Text + " " + newarray[i, j] + "";
textBox1.Text = string.Empty;
}
}
richTextBox1.Text += "\n";
/*for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (newarray[i, 0] == newarray[j, 1])
{
MessageBox.Show("This seat is already chosen");
}
if (newarray[i, 1]==newarray[j,2])
{
MessageBox.Show("This seat is already chosen");
}
}
}/*
}
}
It's not working and now every time I click on book button- it shows popup message saying -this seat is already chosen.I don't know where's the problem. This is my own approach so I'm not sure where I'm doing wrong.I would appreciate any help.
Here's the link the GUI- If you see the users John and Jon have been assigned same seat(D3) number which should not be the case.