List View Control in Windows Forms
I want to create a menu of dishes with cost displaying beside each item. Whenever I select some dishes and Click on OK button. It has to display the total cost of the selected dishes in the label declared. Here, in the background it has to add the values of the selected dishes and it should display. Please post the code for this task in C#.
Prem Anandh JPosted Sep 11, 2015, 12:08 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Prem Anandh Natchatran
namespace ListVew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0";
textBox1.Enabled = false;
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("ProductName", 100);
listView1.Columns.Add("Price", 70);
//Add items in the listview
string[] arr = new string[2];
ListViewItem itm;
//Add first item
arr[0] = "Tea";
arr[1] = "20";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
//Add second item
arr[0] = "Coffee";
arr[1] = "25";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
//Add Third item
arr[0] = "CoolDrinks";
arr[1] = "35";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
//Add fouth item
arr[0] = "Juice";
arr[1] = "55";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection breakfast =
this.listView1.SelectedItems;
double price = 0.0;
foreach (ListViewItem item in breakfast)
{
price += Double.Parse(item.SubItems[1].Text);
}
price=int.Parse(textBox1.Text) + price;
// Output the price to TextBox1.
textBox1.Text = price.ToString();
}
}
}
Prem Anandh JPosted Sep 11, 2015, 4:26 AM
Sarva SiddarthaPosted Sep 11, 2015, 3:45 AM