C# 2 adet combobox'da ki saatleri alma

notify (538) 7 yıl önce sordu

Merhaba,

C#'Da bir proje üzerinde çalışıyorum ve yapamadığım bir konu var formumda 2 adet combobax  var bu combobax lardan saat seçimi yaptırıyorum ikisindede aynı saatler var 11.00 , 12.00 ,12:30 gibi... benim yapmaya çalıştığım şey ise şu, 1.combox'dan 11:00 saati seçtim ikinci combobax'Dan ise 13:00'ı seçtim bu arada kalan comboax'a yerleştirdiğim saatler yani 11:00 ile 13:00 arasında olan saatler de seçilmiş olacak ki dolu oalrak veritabanımda gözüksün. Birdaha seçim yapılmasın. Umarım anlatabilmişimdir.

notify 7 yıl önce

Ben size gerekli açıklamayı mesaj olarak yolladım. Lütfen bu yorumunuzu kaldırın.

Toplam 2 cevap


alattin (16999) 7 yıl önce cevapladı

Aşağıda bir örnek bulabilirsin.

Emocan diye bir veri tabanı ve bu veritabanında Rezerve adlı bir tablon olmalı. Bu tabloda Saat(nvarchar(50) türünde bir alana ihtiyacın var.

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;
namespace emocan
{
    public partial class Form2 : Form
    {
        string baglantiCumlesi = "server=.\\SQLEXPRESS; database=emocan; integrated security=SSPI";
        List<string> RezerveSaatler;
        public Form2()
        {
            InitializeComponent();
            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
            this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
            this.comboBox2.DrawMode = DrawMode.OwnerDrawFixed;
            this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem);


        }
        private void Form2_Load(object sender, EventArgs e)
        {
            RezerveSaatler = GetirRezerveSaatler();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var t1 = comboBox1.SelectedItem.ToString();
            var t2 = comboBox2.SelectedItem.ToString();
            bool flag = false;
            foreach (var i in comboBox2.Items)
            {
                if (i.ToString() == t1)
                    flag = true;
                if (flag)
                {
                    if (!DahaOnceRezerveEdilmisMi(i.ToString()))
                        SaatKaydet(i.ToString());
                    else
                    {
                        MessageBox.Show("Bu saat daha önce rezerve edilmiş");
                    }
                }
                if (i.ToString() == t2)
                    flag = false;
            }

        }

        Font myFont = new Font("Arial", 10, FontStyle.Underline | FontStyle.Regular);
        Font myFont2 = new Font("Arial", 10, FontStyle.Italic | FontStyle.Strikeout);

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            string saat = comboBox1.Items[e.Index].ToString();
            if (RezerveSaatler.Contains(saat))
            {
                e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont2, Brushes.LightSlateGray, e.Bounds);


            }
            else
            {
                e.DrawBackground();
                e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), myFont, Brushes.Black, e.Bounds);
                e.DrawFocusRectangle();
            }


        }
        private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            string saat = comboBox2.Items[e.Index].ToString();
            if (RezerveSaatler.Contains(saat))
            {
                e.Graphics.DrawString(comboBox2.Items[e.Index].ToString(), myFont2, Brushes.LightSlateGray, e.Bounds);


            }
            else
            {
                e.DrawBackground();
                e.Graphics.DrawString(comboBox2.Items[e.Index].ToString(), myFont, Brushes.Black, e.Bounds);
                e.DrawFocusRectangle();
            }


        }


        void SaatKaydet(string saat)
        {
            SqlConnection baglanti = new SqlConnection(baglantiCumlesi);
            SqlCommand komut = new SqlCommand("Insert Into Rezerve (Saat) Values (@prmSaat)", baglanti);
            komut.Parameters.AddWithValue("@prmSaat", saat);
            baglanti.Open();
            komut.ExecuteNonQuery();
            baglanti.Close();
        }
        bool DahaOnceRezerveEdilmisMi(string saat)
        {
            SqlConnection baglanti = new SqlConnection(baglantiCumlesi);
            SqlCommand komut = new SqlCommand("Select count(*) from Rezerve Where Saat=@prmSaat", baglanti);
            komut.Parameters.AddWithValue("@prmSaat", saat);
            baglanti.Open();
            int sonuc = (int)komut.ExecuteScalar();
            baglanti.Close();
            if (sonuc == 0)
                return false;
            else
                return true;
        }

        List<string> GetirRezerveSaatler()
        {
            List<string> rezerveler = new List<string>();
            using (SqlConnection connection = new SqlConnection(baglantiCumlesi))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("SELECT Saat from Rezerve", connection))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        rezerveler.Add(reader.GetString(0));

                    }
                }
            }
            return rezerveler;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                if (RezerveSaatler.Contains(comboBox1.SelectedItem.ToString()))
                    comboBox1.SelectedIndex = -1;
            }


        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex != -1)
            {
                if (RezerveSaatler.Contains(comboBox2.SelectedItem.ToString()))
                    comboBox2.SelectedIndex = -1;
            }

        }
    }
}

 

notify (538) 7 yıl önce cevapladı

Alaattin bey sağolsun yardım etti ve tam da istediğim şekilde yaptık. Ama yukarıda ki kod ile kaydederken combobox'daki saatler alt alta yani satır satır veritabanına kaydoluyor. Şöyle bir şey yapmaya çalıştım ama c# bilgim  o kadarına yetmediğinden yapamadım. Saat adlı kolona 11:00 - 15:00 diye kaydetsem ve form açıldığın da comobox'a çekerken yine aynı yukarıda ki koddaki gibi düzenli bir şekilde çekmeye çalışıyorum. Bunu nasıl yapabilirim? Bilgisi olan varsa ve paylaşırsa çok mutlu olurum. İyi çalışmalar.