C# Combobox'da seçilen değeri Listbox'a eklemek

emreaymkk

Femtopat
Katılım
6 Ekim 2023
Mesajlar
4
Daha fazla  
Cinsiyet
Erkek
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace *******
{
 
    public partial class Form1 : Form
    {

     
        SqlCommand drKategori;
        SqlCommand KategoriAdi;
        SqlCommand urunAdi;
        SqlCommand drUrun;
        SqlCommand drPersonel;
        SqlCommand personel;
        SqlConnection con = new SqlConnection("Server=***********;Database=Northwind;Trusted_Connection=True;");
        public Form1()
        {
            InitializeComponent();
       
        }
        int id;
        private void Form1_Load(object sender, EventArgs e)
        {
          SqlCommand ListboxGetir = new SqlCommand("select * from Kategoriler", con);
          SqlCommand cmdPersonelGetir = new SqlCommand("Select * from Personeller", con);
          SqlCommand cmdUrunGetir = new SqlCommand("select * from Urunler", con);
          SqlCommand cmdKategoriGetir = new SqlCommand("select * from Kategoriler", con);
            id = 0;
         

            if (con.State == ConnectionState.Closed)
            {
                con.Open();

            }
            SqlDataReader drPersonel = cmdPersonelGetir. ExecuteReader();
            while (drPersonel.Read())
            {
                string personel = (string)drPersonel["Adi"];
                comboBox1.Items.Add(personel);
            }

            if (con.State == ConnectionState.Open)
            {
                con.Close();


            }

            if (con.State == ConnectionState.Closed)
            {

                con.Open();

            }




       
            SqlDataReader drUrun = cmdUrunGetir.ExecuteReader();
            while (drUrun.Read())
            {
                string urunAdi = (string)drUrun["urunAdi"];
                   listBox1.Items.Add(urunAdi);
            }
            if(con.State == ConnectionState.Open)
            {
                con.Close();

            }




            if (con.State == ConnectionState.Closed)
            {

                con.Open();
            }

            SqlDataReader drKategori = cmdKategoriGetir.ExecuteReader();
            while (drKategori.Read())
            {
                string KategoriAdi = (string)drKategori["KategoriAdi"];
                comboBox2.Items.Add(KategoriAdi);
            }


            if (con.State == ConnectionState.Open)
            {
             
                con.Close();
       
            }

            comboBox2.SelectedIndex = 0;



            id = comboBox2.SelectedIndex + 1;
            cmdKategoriGetir = new SqlCommand("select * from kategoriler where KategoriID=", con);
            listBox2.Items.Clear();
         

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

   

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

         
        }

     
        private void listBox2_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            listBox2.Items.Add(id);
        }
    }
}
 
Son düzenleme:
C#:
if (comboBox1.SelectedItem != null)
 {
 listBox1.Items.Add(comboBox1.SelectedItem);
 }

Aynı zamanda comboBox seçili öge boş ise ekleme yapmaz.
 
C#:
if (comboBox1.SelectedItem != null)
 {
 listBox1.Items.Add(comboBox1.SelectedItem);
 }

Aynı zamanda comboBox seçili öge boş ise ekleme yapmaz.
sorun da orada listboxa değeri atmıyor yardımcı olursanız sevinirim

C#:
if (comboBox1.SelectedItem != null)
 {
 listBox1.Items.Add(comboBox1.SelectedItem);
 }

Aynı zamanda comboBox seçili öge boş ise ekleme yapmaz.



sorun da orada listboxa değeri atmıyor yardımcı olursanız sevinirim




taslak burada combobox2de seçilen kategoriyi listboxa atmak istiyorum fakat atamadım
 
Hocam çok eksiğiniz var hangi birini açıklasam bilemedim o yüzden direk kodunuzu bununla değiştirin.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace *******
{

 public partial class Form1 : Form
 {

 SqlCommand drKategori;
 SqlCommand KategoriAdi;
 SqlCommand urunAdi;
 SqlCommand drUrun;
 SqlCommand drPersonel;
 SqlCommand personel;
 SqlConnection con = new SqlConnection("Server=***********;Database=Northwind;Trusted_Connection=True;");
 public Form1()
 {
 InitializeComponent();

 }
 int id;
 private void Form1_Load(object sender, EventArgs e)
 {
 SqlCommand ListboxGetir = new SqlCommand("select * from Kategoriler", con);
 SqlCommand cmdPersonelGetir = new SqlCommand("Select * from Personeller", con);
 SqlCommand cmdUrunGetir = new SqlCommand("select * from Urunler", con);
 SqlCommand cmdKategoriGetir = new SqlCommand("select * from Kategoriler", con);
 id = 0;

 if (con.State == ConnectionState.Closed)
 {
 con.Open();

 }
 SqlDataReader drPersonel = cmdPersonelGetir. ExecuteReader();
 while (drPersonel.Read())
 {
 string personel = (string)drPersonel["Adi"];
 comboBox1.Items.Add(personel);
 }

 if (con.State == ConnectionState.Open)
 {
 con.Close();

 }

 if (con.State == ConnectionState.Closed)
 {

 con.Open();

 }

 SqlDataReader drUrun = cmdUrunGetir.ExecuteReader();
 while (drUrun.Read())
 {
 string urunAdi = (string)drUrun["urunAdi"];
 listBox1.Items.Add(urunAdi);
 }
 if(con.State == ConnectionState.Open)
 {
 con.Close();

 }

 if (con.State == ConnectionState.Closed)
 {

 con.Open();
 }

 SqlDataReader drKategori = cmdKategoriGetir.ExecuteReader();
 while (drKategori.Read())
 {
 string KategoriAdi = (string)drKategori["KategoriAdi"];
 comboBox2.Items.Add(KategoriAdi);
 }

 if (con.State == ConnectionState.Open)
 {

 con.Close();

 }

 comboBox2.SelectedIndex = 0;

 id = comboBox2.SelectedIndex + 1;
 cmdKategoriGetir = new SqlCommand("select * from kategoriler where KategoriID=", con);
 listBox2.Items.Clear();

 }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {

 }

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {

 }

 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
 {
 listBox1.Items.Clear();
 listBox1.Items.Add(comboBox2.SelectedItem);
 }

 private void listBox2_SelectedIndexChanged_1(object sender, EventArgs e)
 {

 }
 }
}
 

Button kullanarak kendimce çözdüm ,salı günü hocayla düzeltip paylaşırım.
 
Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…