Bildiğiniz üzere listbox nesnesi alt alta satırların girilebildiği birçok özelliğe sahip bir nesnedir.
listsepet.item : Listbox daki nesnelerimizle ilgili her işlemi yapmaya yarar
listsepet.Items.Add : Listbox'a satır eklemek icin kullanırız ve eklenen her satır string türünde olmalıdır.
listsepet.Items.Clear : Listbox nesnemizi temizlemeye yarar.
listsepet.Items.Count : Listbox'da kac item oldugunu gösterir dolayısıyla döngülerde işimize yarar.
Bu kucuk bilgilendirmeyi yaptıkdan sonra yukarıda gösterilen resimin kodunu paylasayım sizlerle pek acıklama yapmadan kodu yapıstırsamda yorum olarak her türlü soruyu sorabilirsiniz tabii tavsiyem kopyala yapıstır yapmadan incelemenızdır :
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;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace admin_paneli
{
public partial class urunler : Form
{
public urunler()
{
InitializeComponent();
}
MySqlConnection bagln = new MySqlConnection("server=ip adres;allow user variables=true;database=db adı;user="kullanıcı" ;password=;");
private void urunler_Load(object sender, EventArgs e)
{// burada sayfa ilk acıldıgında datagridview ve comboBox nesnlerimize yuklenecek bilgiler kodlanıyor
listsepet.Text = "";
bagln.Open();
MySqlCommand kmm = new MySqlCommand("select * from urunler", bagln);
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = kmm;
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "urunadi";
comboBox1.ValueMember = "urunadi";
string sql = "Select * from urunler";
MySqlDataAdapter adaptor = new MySqlDataAdapter(sql, bagln);
DataSet memet = new DataSet();
adaptor.Fill(memet, "xx");
dataurunler.DataSource = memet.Tables[0];
bagln.Close();
}
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection baglan = new MySqlConnection("server=;database=dbname;user=;password=;");
baglan.Open();
string sql = "Select * from urunler where urunadi like '%" +txturunbul.Text+ "%'"; // ürün araması
MySqlDataAdapter adaptor = new MySqlDataAdapter(sql, baglan);
DataSet memet = new DataSet();
adaptor.Fill(memet, "xx");
dataurunler.DataSource = memet.Tables[0];
baglan.Close();
}
int toplamurunadedi = 0;
int toplamfiyat = 0;
private void btnurunekle_Click(object sender, EventArgs e)
{
if (txturunfiyat.Text == "") { MessageBox.Show("Choose at least one product..!"); }
else if (numericUpDown1.Value==0){ MessageBox.Show("You cannot buy 0 number of product)");}
else
{
int tutar = Convert.ToInt32(txturunfiyat.Text) * Convert.ToInt32(numericUpDown1.Value);
listsepet.Items.Add("Product Name:" + comboBox1.Text + " Number of Prod. :" + numericUpDown1.Value.ToString() + " Unit Price:" + txturunfiyat.Text + " Total : " + tutar + "TL");
toplamfiyat += tutar;
toplamurunadedi += Convert.ToInt32(numericUpDown1.Value);
}
numericUpDown1.Value = 0;
}
private void btnhesapla_Click(object sender, EventArgs e)
{
txttoplamfiyat.Text = toplamfiyat.ToString();
txttoplamurun.Text = toplamurunadedi.ToString();
}
private void btnkaydet_Click(object sender, EventArgs e)
{
if (msktc.Text == "") { MessageBox.Show("Please Enter your ID number.."); }
else{
bagln.Open();
MySqlCommand km = new MySqlCommand("update borclar set borc= borc+'" + toplamfiyat + "'where tcno='" + msktc.Text + "'",bagln);
MySqlCommand kk = new MySqlCommand("update borclar set uruntoplam= uruntoplam+'" + toplamurunadedi + "'where tcno='" + msktc.Text + "'", bagln);
km.ExecuteNonQuery();
kk.ExecuteNonQuery();
bagln.Close();
MessageBox.Show("You totally bought " + toplamurunadedi + " number of product");
MessageBox.Show("You paid totally " + toplamfiyat + " TL. We wish to see you again :) ");
}
listsepet.Items.Clear();
txttoplamurun.Clear();
txttoplamfiyat.Clear();
txturunfiyat.Clear();
}
private void btniptal_Click(object sender, EventArgs e)
{
MessageBox.Show("Your shopping has cancelled...");
listsepet.Items.Clear();
txttoplamurun.Clear();
txttoplamfiyat.Clear();
txturunfiyat.Clear();
toplamurunadedi = 0;
toplamfiyat = 0;
}
private void button3_Click(object sender, EventArgs e) //Back butonu
{
adminanaekran addm = new adminanaekran();
addm.Show();
this.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection baglan = new MySqlConnection("server=;database=;user=;password=;");
baglan.Open();
MySqlCommand mm = new MySqlCommand("select urunfiyati from urunler where urunadi='" + comboBox1.Text + "'", baglan);
MySqlDataReader read = mm.ExecuteReader();
while (read.Read())
{
txturunfiyat.Text = read[0].ToString(); // ComboBox'ta o an secili olan ürünün fiyatı cekiliyor
}
baglan.Close();
}
private void btnremove_Click(object sender, EventArgs e)
{
int i;
for (i = listsepet.SelectedIndices.Count - 1; i >= 0; i--)
{
listsepet.Items.RemoveAt(listsepet.SelectedIndices[i]); // Sectigimiz elemanları remove butonuyla //silmemizi sağlar
}
}
}
}