My message wont show on my textbox? - c#

I am trying to fill a form (Movie information such as Title, Year and Genre) and then show it in a textbox (enable=false).
I have 2 classes and a Form (with 2 textboxes (for user input), 2 buttons, 1 to register the movie infos, and 1 to show the infos in a 3rd textbox (the one that is Disabled).
class Movies
{
public string Title { get; set; }
public int Year { get; set; }
public string ReturnMovie()
{
return Title + " (" + Year.ToString() + ")";
}
}
class Genres
{
public string Genre { get; set; }
public string ReturnGenre()
{
return Genre;
}
}
MainForm:
bool btnRegisterClicked = false;
Movies m1 = new Movies();
Genres g1 = new Genres();
private void btnRegister_Click(object sender, EventArgs e)
{
btnRegisterClicked = true;
if (btnRegisterClicked == true)
{
if (txtTitle.Text.Length == 0)
{
MessageBox.Show("Enter a title for the movie.");
btnRegisterClicked = false;
}
else if (txtYear.Text.Length < 4)
{
MessageBox.Show("Enter a valid year.");
btnRegisterClicked = false;
}
else if (chkGenre.SelectedIndex == -1)
{
MessageBox.Show("Select the genre(s) of the movie.");
btnRegisterClicked = false;
}
else
{
txtTitle.Text = m1.Title;
txtYear.Text = m1.Year.ToString();
chkGenre.Text = g1.Genre;
}
txtTitle.Clear();
txtYear.Clear();
chkGenre.ClearSelected();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
txtShow.Text = m1.ReturnMovie() + g1.ReturnGenre();
}
}
What am I doing wrong ?

The assignments in your else block are backwards. You're setting the TextBox to the un-initalized contents of m1 and g1, and then immediately .Clear()ing them out.
else
{
m1.Title = txtTitle.Text;
m1.Year = int.Parse(txtYear.Text);
g1.Genre = chkGenre.Text
}

You need to assign values to variables not to controls..., change following
else
{
txtTitle.Text = m1.Title;
txtYear.Text = m1.Year.ToString();
chkGenre.Text = g1.Genre;
}
to
else
{
m1.Title = txtTitle.Text;
m1.Year = int.Parse(txtYear.Text);
g1.Genre = chkGenre.Text;
}

Related

Double Linked List C#. How do I delete all the nodes in my double linked list

Im trying to delete all the nodes in my double linked list but am having trouble. my variable this.Start is null when debugging which causes my code not to work. I am unable to figure out a solution. Is there an alternate way to delete my nodes or fix my current code. Please help Thank you:D
public partial class Form2 : Form
{
SeatManager _seatManager = new SeatManager();
DoubleLinkedList seatList = new DoubleLinkedList();
Seat _seat = new Seat();
public Form2()
{
InitializeComponent();
}
private void buttonCreateAndDisplay_Click(object sender, EventArgs e)
{
Label labelSeat = new Label();
labelSeat.Click+=new EventHandler(HandleLabelClick);
Seat seat = _seatManager.InsertOneSeat(y, x);
labelSeat.Tag=seat;
labelSeat.Text=seat.ComputeSeatLabel();
}
}
}
}
}
private void HandleLabelClick(object sender, EventArgs e)
{
string labelName = "";
Label labelSeat = (Label)sender;
Seat seat = (Seat)labelSeat.Tag;
labelMessage.Text=labelSeat.Name;
if (seat.BookStatus==false)
{
seat=_seatManager.FindOneSeatToBook(seat.Row, seat.Column);
}
else
{
seat=_seatManager.FindOneSeatToUnbook(seat.Row, seat.Column);
}
if (seat.BookStatus==false)
{
labelSeat.BackColor=Color.LightGray;
}
else
{
labelSeat.BackColor=Color.LightGoldenrodYellow;
}
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
seatList.ClearNodes(maxRow,maxColumn);
seatList.deleteAllNodes();
_resetbuttonclicked=true;
panelSeats.Controls.Clear();
buttonCreateAndDisplay_Click(sender, e);
}
DoubleLinkedList
class DoubleLinkedList
{
public Node Start { get; set; }
public DoubleLinkedList()
{
this.Start=null;
}
public void InsertAtEnd(Seat seatData)
{
Node newNode = new Node(seatData);
if (this.Start==null)
{
this.Start=newNode;
return;
}
Node p = this.Start;
while (p.next!=null)
{
p=p.next;
}
p.next=newNode;
newNode.prev=p;
}
public Seat SearchByRowAndColumn(int pRow, int pColumn)
{
Node p = this.Start;
while (p!=null)
{
if ((p.seat.Column == pColumn) && (p.seat.Row == pRow))
{
break;
}
p=p.next;
}
if (p==null)
{
return null;
}
else
{
return p.seat;
}
}
public Seat ClearNodes(int pRow,int pColumn)
{
Node p = this.Start;
while (p != null)
{
p=p.prev;
p.next=null;
}
return null;
}
public void deleteAllNodes()
{
Node seat = new Node();
while (this.Start != null)
{
seat = this.Start;
this.Start = this.Start.next;
seat = null;
}
this.Start=null;
}
SeatManager
class SeatManager
{
DoubleLinkedList _seats;
public SeatManager()
{
_seats= new DoubleLinkedList();
}
public Seat InsertOneSeat(int row, int column)
{
Seat newSeat = new Seat();
newSeat.Row = row;
newSeat.Column = column;
newSeat.CanBook = true;
newSeat.Bookedby = "";
_seats.InsertAtEnd(newSeat);
return newSeat;
}
public Seat FindOneSeatToBook(int row, int column)
{
Seat seat = _seats.SearchByRowAndColumn(row, column);
seat.BookStatus=true;
return seat;
}
public Seat FindOneSeatToUnbook(int row, int column)
{
Seat seat = _seats.SearchByRowAndColumn(row, column);
seat.BookStatus = false;
return seat;
}
Can't you just write
public void deleteAllNodes()
{
this.Start = null;
}
and let the garbage collector clean up the mess.

how to add instance to the list and then to the listbox in windows form

I don't know how to add Instances of my Album class from TextBoxes to a List<Album> and then to a ListBox in my WindowsForm.
Here is the code I have written so far but I'm stuck here and I don't know what to do next.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Album> AlbumList = new List<Album>();
private void TrackButton_Click(object sender, EventArgs e)
{
addTracks settingsForm = new addTracks();
settingsForm.ShowDialog();
}
private void CreateButton_Click(object sender, EventArgs e)
{
Album g = new Album (ASINtextBox.Text, AlbumNametextBox.Text, ArtisttextBox.Text,
ReleaseDatePicker.Text, LabeltextBox.Text, ImagetextBox.Text);
AlbumList.Add(g);
}
}
I dont have eny error i just think it is not creating a new list when I'm debuging the program.
This is the code for the class:
eclass Album
{
private string ASIN;
private string AlbumName;
private string Artist;
private string ReleaseDate;
private string Label;
private string Image;
public Album(int ASIN)
{
this.ASIN = "no value";
this.AlbumName = "no value";
this.Artist = "no value";
this.ReleaseDate = "no value";
this.Label = "no value";
this.Image = "no value";
}
public Album(string ASIN, string AlbumName, string Artist, string ReleaseDate,
string Label, string Image)
{
this.ASIN = ASIN;
this.AlbumName = AlbumName;
this.Artist = Artist;
this.ReleaseDate = ReleaseDate;
this.Label = Label;
this.Image = Image;
}
public string aSIN
{
get { return this.ASIN; }
set { ASIN = value; }
}
public string albumName
{
get { return this.AlbumName; }
set { AlbumName = value; }
}
public string artist
{
get { return this.Artist; }
set { Artist = value; }
}
public string createDate
{
get { return this.ReleaseDate; }
set { ReleaseDate = value; }
}
public string label
{
get { return this.Label; }
set { Label = value; }
}
public string image
{
get { return this.Image; }
set { Image = value; }
}
}
Okay, you've got a few different options, but the easiest is something like this:
private void RefreshListBox()
{
myGuiListBox.Items.Clear();
foreach(Album loopAlbum in this.AlbumList)
{
myGuiListBox.Items.Add(loopAlbum.ToString());
}
}
... then, whenever something would change the list box (such as creating a new album and adding it to your List<>), you simply call the RefreshListBox() function.
I solved the listbox by not adding in the list box but just showing the values through the labels.
private void ShowAlbumsButton_Click(object sender, EventArgs e)
{
int temp = AlbumList.Count();
string talbumname = "";
string talbumasin = "";
string talbumartist = "";
string talbumrelease = "";
string talbumlabel = "";
for (int n =0; n<temp;n++)
{
talbumname = talbumname + AlbumList[n].albumName;
talbumname = talbumname + '\n';
talbumasin = talbumasin + AlbumList[n].aSIN;
talbumasin = talbumasin + '\n';
talbumartist = talbumartist + AlbumList[n].artist;
talbumartist = talbumartist + '\n';
talbumrelease = talbumrelease + AlbumList[n].createDate;
talbumrelease = talbumrelease + '\n';
talbumlabel = talbumlabel + AlbumList[n].label;
talbumlabel = talbumlabel + '\n';
}
label8.Text = talbumname;
label7.Text = talbumasin;
label9.Text = talbumartist;
label10.Text = talbumrelease;
label11.Text = talbumlabel;
}
thanks everyone for help.

i want to update the quantity on a list box c# winform

how to update e specific value on a list..
for example when i click a button it adds the product on the list
name: coffe || quantity:1 || Price:2$
and when i click angain the same product the quantity increases by 1
i used this code but it doesnt change the number of the quantity.
private BindingList<recipt> Lista2 = new BindingList<recipt>();
private void addtolist(object sender, EventArgs e)
{
Button b = (Button)sender;
Product p = (Product)b.Tag;
recipt fat = new recipt ()
{
Name= p.Name,
quantity= 1,
price = p.Cmimi
};
bool found = false;
if (listBox1.Items.Count > 0)
{
foreach (var pr in Lista2)
{
if (pr.Name== p.Name)
{
pr.quantity= pr.quantity+ 1;
found = true;
}
}
if (!found)
{
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
}
}
else
{
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
}
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
pe.Faturs.Add(fat);
pe.SaveChanges();
Total = Total + (int)fat.price;
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
For updating values in ListBox automatically you need set BindingList of receipts to the ListBox.DataSource and make Receipt class implement INotifyPropertyChanged
public class Receipt : INotifyPropertyChanged
{
public string Name { get; }
public int Quantity { get; private set; }
public decimal Price { get; }
public string BillNumber { get; private set; }
public decimal Total => Price * Quantity;
public string Info => $"{nameof(Name)}: {Name} || {nameof(Quantity)}: {Quantity} || {nameof(Price)}: {Price:C} || {nameof(Total)}: {Total:C}";
public Receipt(string name, decimal price, string billNumber)
{
Name = name;
Price = price;
BillNumber = billNumber;
Quantity = 1;
}
public void AddOne()
{
Quantity += 1;
RaisePropertyChanged(nameof(Info));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then in the form
public class YourForm : Form
{
private readonly BindingList<Receipt> _Receipts;
public YourForm()
{
_Receipts = new BindingList<Receipt>();
listBox1.DisplayMember = "Info";
listBox1.DataSource = _Receipts;
}
private void AddToList(object sender, EventArgs e)
{
var button = (Button) sender;
var product = (Product) button.Tag;
var receiptInfo = _Receipts.FirstOrDefault(receipt => receipt.Name.Equals(product.Name));
if (receiptInfo == null)
{
receiptInfo = new Receipt(product.Name, product.Cmimi, txtNrbill.Text);
_Receipts.Add(receiptInfo);
}
else
{
receiptInfo.AddOne();
}
}
}

ListBox doesnt show any results

I'm trying to add an array of strings that holds information about people to a ListBox but i cant get it to show anything and i really dont know why.
This is the first class that is called when the button to add contacts to the list is clicked.
public partial class MainForm : Form
{
private ContactManager m_contacts;
public MainForm()
{
InitializeComponent();
m_contacts = new ContactManager();
InitializeGUI();
}
private void InitializeGUI()
{
txtFirstName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtStreet.Text = string.Empty;
txtCity.Text = string.Empty;
txtZipCode.Text = string.Empty;
cmbCountry.Items.AddRange(Enum.GetNames(typeof(Countries)));
cmbCountry.DropDownStyle = ComboBoxStyle.DropDownList;
cmbCountry.SelectedIndex = (int)Countries.Sverige;
UpdateGUI();
}
private bool ReadInput(out Contact contact)
{
// skapar ett lokalt objekt av Contact-klassen
contact = new Contact();
// Lägger in ReadAdress till ett objekt i klassen Adress.
Address adr = ReadAddress();
contact.AddressData = adr; // skickar adress till contact-objekt
//bool readNameOK = ReadName();
// ReadName är OK så skickas det till AddContact.
if (ReadName())
{
m_contacts.AddContact(contact);
}
return ReadName();
} // ReadInput
private bool ReadName()
{
Contact contact = new Contact();
contact.FirstName = txtFirstName.Text;
contact.LastName = txtLastName.Text;
bool firstname = false;
bool lastname = false;
if (!InputUtility.ValidateString(contact.FirstName))
{
MessageBox.Show("You must enter a first name with atleast one character (not a blank)", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
txtFirstName.Focus();
txtFirstName.Text = " ";
txtFirstName.SelectAll();
firstname = false;
}
else if (!InputUtility.ValidateString(contact.LastName))
{
MessageBox.Show("You must enter a last name with atleast one character (not a blank)", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
txtLastName.Focus();
txtLastName.Text = " ";
txtLastName.SelectAll();
lastname = false;
}
return firstname && lastname;
}
private Address ReadAddress()
{
Address address = new Address();
address.Street = txtStreet.Text;
address.City = txtCity.Text;
address.ZipCode = txtZipCode.Text;
address.Country = (Countries)cmbCountry.SelectedIndex;
return address;
}
private void button1_Click(object sender, EventArgs e)
{
Contact contact;
if (ReadInput(out contact))
{
UpdateGUI();
}
}
private void UpdateGUI()
{
lstContacts.Items.Clear();
lstContacts.Items.AddRange(m_contacts.GetContactsInfo());
lblCount.Text = m_contacts.Count().ToString();
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateContactInfoFromRegistry();
}
private void UpdateContactInfoFromRegistry()
{
Contact contact = m_contacts.GetContact(lstContacts.SelectedIndex);
cmbCountry.SelectedIndex = (int)contact.AddressData.Country;
txtFirstName.Text = contact.FirstName;
txtLastName.Text = contact.LastName;
txtCity.Text = contact.AddressData.City;
txtStreet.Text = contact.AddressData.Street;
txtZipCode.Text = contact.AddressData.ZipCode;
}
}
This class then calls this class
public class ContactManager
{
private List<Contact> m_contactRegistry;
public ContactManager()
{
m_contactRegistry = new List<Contact>();
}
public int Count()
{
int count = m_contactRegistry.Count();
return count;
}
public bool CheckIndex(int index)
{
if (index >= 0 && index < m_contactRegistry.Count())
return true;
else return false;
}
public bool AddContact(string firstName, string lastName, Address addressIn)
{
Contact contactObj = new Contact();
bool result = false;
if (!result)
{
contactObj.FirstName = firstName;
contactObj.LastName = lastName;
contactObj.AddressData = addressIn;
m_contactRegistry.Add(contactObj);
result = true;
}
return result;
}
public bool AddContact(Contact contactIn)
{
if (contactIn == null)
{
return false;
}
else
{
m_contactRegistry.Add(contactIn);
return true;
}
}
public bool changeContact(Contact contactIn, int index)
{
if (CheckIndex(index))
{
Contact contact = (Contact)m_contactRegistry[index];
//contact.ToString = contactIn;
return true;
}
else return false;
}
public bool DeleteContact(int index)
{
if (CheckIndex(index))
{
m_contactRegistry.RemoveAt(index);
return true;
}
else return false;
}
public Contact GetContact(int index)
{
if (!CheckIndex(index))
return null;
else return m_contactRegistry[index];
}
public string[] GetContactsInfo()
{
string[] strInfoStrings = new string[m_contactRegistry.Count];
int i = 0;
foreach (Contact contactObj in m_contactRegistry)
{
strInfoStrings[i++] = contactObj.ToString();
}
return strInfoStrings;
}
}
Any help regarding why the arrays wont show up in the listbox would be much appriciated, thanks.
Your ReadName() always returns false, there for it never adds the contacts.
EDIT:
A clearer code
private Contact ReadInput()
{
Contact contact = new Contact();
contact.FirstName = txtFirstName.Text;
contact.LastName = txtLastName.Text;
contact.AddressData = new Address
{
Street = txtStreet.Text,
City = txtCity.Text,
ZipCode = txtZipCode.Text,
Country = (Countries) cmbCountry.SelectedIndex
};
return contact;
}
private bool ValidateContact(Contact contact)
{
if ( !InputUtility.ValidateString( contact.FirstName ) )
{
MessageBox.Show( "You must enter a first name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error );
txtFirstName.Focus();
txtFirstName.Text = " ";
txtFirstName.SelectAll();
return false;
}
else if ( !InputUtility.ValidateString( contact.LastName ) )
{
MessageBox.Show( "You must enter a last name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error );
txtLastName.Focus();
txtLastName.Text = " ";
txtLastName.SelectAll();
return false;
}
return true;
}
private void button1_Click( object sender, EventArgs e )
{
Contact contact = ReadInput();
if ( ValidateContact( contact ) )
{
m_contacts.AddContact(contact);
UpdateGUI();
}
}

I want to edit my program so rather than having user enter seat number in a text box to make reservation they can select it in the listbox

How would I go about changing to code to eliminate the textbox that takes the value of the seatnumber and makes the reservation they can simply select which seat they want from the seating chart in the listbox and click make reservation. I know there's some unused code in here that needs cleaned up just ignore it i've been playing around with different ways to do this.
Heres the form code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Reservations
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Flight curFlight;
List<Flight> flightlist = new List<Flight>();
Flight flight1 = new Flight("Cessna Citation X", "10:00AM", "Denver", 6, 2);
Flight flight2 = new Flight("Piper Mirage", "10:00PM", "Kansas City", 3, 2);
private void Form1_Load(object sender, EventArgs e)
{
MakeReservations();
DisplayFlights();
SetupFlights();
}
private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
{
curFlight = (Flight)lstFlights.SelectedItem;
txtDepart.Text = curFlight.DepartureTime;
txtDestination.Text = curFlight.Destination;
string[] seatChart = curFlight.SeatChart;
DisplaySeatChart(seatChart);
}
private void DisplaySeatChart(string[] seating)
{
lstSeatChart.Items.Clear();
for (int seat = 0; seat <= seating.GetUpperBound(0); seat++)
{
lstSeatChart.Items.Add("Seat: " + (seat + 1) + " " + seating[seat]);
}
}
private void SetupFlights()
{
//flightlist.Add(flight1);
//flightlist.Add(flight2);
}
private void MakeReservations()
{
flight1.MakeReservation("Dill", 12);
flight1.MakeReservation("Deenda", 3);
flight1.MakeReservation("Schmanda", 11);
flight2.MakeReservation("Dill", 4);
flight2.MakeReservation("Deenda", 2);
}
private void DisplayFlights()
{
lstFlights.Items.Clear();
lstFlights.Items.Add(flight1);
lstFlights.Items.Add(flight2);
}
private void btnMakeReservation_Click(object sender, EventArgs e)
{
string name;
int seatNum;
string[] seatChart = curFlight.SeatChart;
if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
{
name = txtCustomerName.Text;
seatNum = Convert.ToInt16(txtSeatNum.Text);
curFlight.MakeReservation(name, seatNum);
lstSeatChart.Items.Clear();
DisplaySeatChart(seatChart);
}
else
{
MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
string name;
int seatNum;
string[] seatChart = curFlight.SeatChart;
if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
{
name = txtCustomerName.Text;
seatNum = Convert.ToInt16(txtSeatNum.Text);
curFlight.EditReservation(name, seatNum);
lstSeatChart.Items.Clear();
DisplaySeatChart(seatChart);
}
else
{
MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
}
}
}
}
HERES THE CLASS CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Reservations
{
class Flight
{
private string mPlane;
private string mDepartureTime;
private string mDestination;
private int mRows;
private int mSeats;
private string[] mSeatChart;
public Flight()
{
}
public Flight(string planeType, string departureTime, string destination, int numRows, int numSeatsPerRow)
{
this.Plane = planeType;
this.DepartureTime = departureTime;
this.Destination = destination;
this.Rows = numRows;
this.Seats = numSeatsPerRow;
// create the seat chart array
mSeatChart = new string[Rows * Seats];
for (int seat = 0; seat <= mSeatChart.GetUpperBound(0); seat++)
{
mSeatChart[seat] = "Open";
}
}
public string Plane
{
get { return mPlane; }
set { mPlane = value; }
}
public string DepartureTime
{
get { return mDepartureTime; }
set { mDepartureTime = value; }
}
public string Destination
{
get { return mDestination; }
set { mDestination = value; }
}
public int Rows
{
get { return mRows; }
set { mRows = value; }
}
public int Seats
{
get { return mSeats; }
set { mSeats = value; }
}
public string[] SeatChart
{
get { return mSeatChart; }
set { mSeatChart = value; }
}
public override string ToString()
{
return this.Plane;
}
public void MakeReservation(string name, int seat)
{
if (IsFull(seat) == false)
{
if (seat <= (Rows * Seats) && mSeatChart[seat - 1] == "Open")
{
mSeatChart[seat - 1] = name;
}
else
{
MessageBox.Show("Seat is taken.", "Reservation Error");
}
}
else
{
MessageBox.Show("Plane is full please choose another flight.", "Reservation Error");
}
}
public void EditReservation(string name, int seat)
{
if (seat <= (Rows * Seats) && mSeatChart[seat - 1] != "Open")
{
mSeatChart[seat - 1] = name;
}
if (seat > mSeatChart.GetUpperBound(0))
{
MessageBox.Show("Invalid seat number.", "Reservation Error");
}
else
{
if (mSeatChart[seat - 1] == "Open")
{
MessageBox.Show("No such reservation exists.", "Reservation Error");
}
}
}
public bool IsFull(int seat)
{
if (seat >= (Rows * Seats) && mSeatChart[seat - 1] != "Open")
{
return true;
}
return false;
}
}
}
An item in your seat chart collection should first have an identifier. Once you have that, that identifier (or a description, name, etc.) All 'Open' seats should be added to a list box that the user can select. Upon reservation, you simply determine which list box items are selected. You can do this with or without data binding.
List<Seat> seats;
private void button6_Click(object sender, EventArgs e)
{
seats = new List<Seat>
{
new Seat { Identifier = "A1", Description = "A1 - Window" },
new Seat { Identifier = "A2", Description = "A2 - Center" },
new Seat { Identifier = "A3", Description = "A3 - Aisle" }
};
listBox1.DataSource = seats;
listBox1.DisplayMember = "Description";
listBox1.ValueMember = "Identifier";
}
private void button7_Click(object sender, EventArgs e)
{
// get selected seat
foreach (Seat selectedSeat in listBox1.SelectedItems)
{
MessageBox.Show(selectedSeat.Identifier);
}
}
Of course you can play around the example and display only those seats that are free. You can do that several ways, one of them is by adding a flag on your Seat class.
Answer is simple rather than
seatNum = Convert.ToInt16(txtSeatNum.Text);
use
seatNum = lst.SeatChart.SelectedIndex + 1;

Categories