Görsel Programlama-I (Visual Programming)
8.Hafta
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
1
2010-2011 Bahar Dönemi
ComboBox,ListBox,CheckedListBox
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
2
ComboBox Bileşeni(Nesnesi)
Text Özelliği TextBox nesnesi gibi kullanılabilir
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
3
Item Ekleme
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
4
comboBox1.MaxDropDownItems = 5; // görünecek Item syısı
comboBox1.MaxDropDownWidth=200; // görünen Itemlerin genişliği
comboBox1.ItemsHeight=5; // Item yüksekliği
comboBox1.RighToLeft =Yes; //Sağa Dayalı
comboBox1.RighToLeft =No; //Sola Dayalı(Normla değeri)
Sadece seçim yapılabilir
Veri girilebilir, seçim yapılabilir
Sample
DropDown
DropDownList
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
5
ComboBox Özellikleri
string s= comboBox1.Text;
Object x= comboBox1.Items[index] ; // indexi verilen item değeri if (comboBox1.SelectedIndex != -1 ) // seçili item varsa { Object x = comboBox1.Items[comboBox1.SelectedIndex]; this.Text = x.ToString(); } int itemsayisi = comboBox1.Items.Count; int itemIndexi = comboBox1.Items.IndexOf("Mavi"); Örn: MessageBox.Show(" itemin indexi " + comboBox1.Items.IndexOf(textBox1.Text) ); int itemindexi = comboBox1.SelectedIndex;// seçili Item indexini verir Object secilenitem= comboBox1.SelectedItem ; // seçilen itemin değerini verir
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
6
ComboBox Özellikleri
Object item = comboBox1.SelectValue; // seçilen Item Value verir. Ancak burada ComboBox1.ValueMember değerine daha önceden bir veri kaynağı ile ilişkilendirilmiş olmalıdır. bool varmi = comboBox1.Items.Contains(Object değer);// itemin olup olmadığına
bakar varsa true yoksa false değeri döndürür
Örn:
if (comboBox1.Items.Contains("Mavi") ) MessageBox.Show("Bu Item mevcut");
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
7
int indexi= comboBox1.FindString(string değer);
// arar bulur ve indexini verir, bulamaz ise (-1) verir
int indexi=comboBox1.FindString(string değer, başlangıç indexi); //belirli bir indexden sonrasında arar Örn: int i = comboBox1.FindString( textBox1.Text ); MessageBox.Show("itemin indexi " + i ); comboBox1.Dispose(); // nesneyi bellekten atar. Dolayısıyla nesne formdan da gidecektir.
ComboBox Özellikleri
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
8
ComboBox Item Ekleme Metodları
comboBox1.Items.Add(object item ); comboBox1.Items.Insert( index ,object item ); comboBox1.Items.AddRange(object [] dizi );
Örnekler: comboBox1.Items.Add("Mavi" ); comboBox1.Items.Add(textBox1.Text); comboBox1.Items.Insert(2,"Yeşil"); comboBox1.Items.Insert(0, textBox1.Text); string [ ] d = {"Mavi","Yeşil","Sarı","Mor" }; comboBox1.Items.AddRange(d);
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
9
ComboBox Item Silme Metodları
comboBox1.Items.Remove(Object item);
comboBox1.Items.Remove(index);
comboBox1.Items.Clear();
Örnekler:
comboBox1.Items.Remove("Mavi");
comboBox1.Items.RemoveAt(2);
comboBox1.Items.Clear();
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
10
Seçilen item silinsin;
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
Seçilen item silinsin;
comboBox1.Items.Remove(comboBox1.SelectedItem);
En Son item Silinsin;
comboBox1.Items.RemoveAT(comboBox1.Items.Count-1);
ComboBox seçili Item Silme Metodları
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
11
En çok kullanılan olayı SelectedIndexChanged olayıdır. Bu olay seçilen Item değiştiğinde veya bir Item seçtiğimizde geçekleşir. Klavye ve Mouse olayları diğer nesneler gibidir.
ComboBox Olayları
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
12
Ekle butonun klikleyince textBox1 deki bilgiler comboBox1 e aktarılacak. Ancak daha önce girilmiş ise uyarı verecek ve kaydetmeyecek.
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
13
// textBox.Text değeri comboBox içerisinde yoksa ekleyecek private void button1_Click(object sender, EventArgs e) {
bool mevcut= false; for (int i = 0; i < comboBox1.Items.Count; i++) {
if (comboBox1.Items[i].ToString().Trim() == textBox1.Text.Trim()) { mevcut = true; break; }
} if (mevcut) { MessageBox.Show( textBox1.Text +" Bilgisi Mevcut"); return; } comboBox1.Items.Add(textBox1.Text); // sona ekler //comboBox1.Items.Insert(0, textBox1.Text); // başa kaydeder
}
ComboBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
14
AYNI İŞİ şu şekilde de yapabiliriz. // textBox.Text değeri comboBox içerisinde yoksa ekleyecek
private void button1_Click(object sender, EventArgs e) { if (comboBox1.Items.Contains(textBox1.Text.Trim() )) { MessageBox.Show( textBox1.Text +« Bilgisi Mevcut"); return; } comboBox1.Items.Add(textBox1.Text); // sona ekler //comboBox1.Items.Insert(0, textBox1.Text); // başa kaydeder }
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
15
ListBox Bileşeni(Nesnesi)
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
16
ListBox Bileşeni(Nesnesi)
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
17
ListBox Bileşeni(Nesnesi)
Items
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
18
label1.Text = listBox1.Items[0].ToString();
listBox1.Items.Add( string) ; // String değeri Item olarak ekler
listBox1.Items.AddRange(dizi) ; // String diziyi Item olarak ekler
string [ ] s=new string*3+ ,“Ali”,”Can”,”Hande”-;
listBox1.Items.AddRange(s) ; // s dizisindeği değerleri Item olarak ekler
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
19
listBox1.Items.Insert(index, string) ; // String değeri index ile belirtilen yere ekler listBox1.Items.Insert(2,”Derya”)
listBox1.Items.Clear( ) ; // İçeriği temizler
int say= listBox1.Items.Count ; // Item sayısını verir
int index= listBox1.Items.IndexOf(string) ; // string ile belirtilen Item ‘in indexini verir Örn: string bulunacak = textBox1.Text; label1.Text = listBox1.Items.IndexOf(bulunacak) .ToString(); // bulunan değerin indexini verir
listBox1.Items.RemoveAt(index) ; // index’ i verilen Item silinir
listBox1.Items.Remove(string) ; // string’ i verilen Item silinir
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
20
Örn: TextBox1 ‘ e girilen bilgi listBox1 içerisinde bulunup hepsi tek tek silinir
string s; int x , y; s = textBox1.Text; do{ x = listBox1.Items.Count; listBox1.Items.Remove(s); y = listBox1.Items.Count; } while (x != y);
bool b = listBox1.Items.Contains(string) ; // string ile verilen değerin listBox1 içerisinde olup olmadığı araştırılır, varsa true yoksa false değeri geriye döner
int index = listBox1.FindStringExact(string , index); // string ile verilen değerin listBox1 içerisinde index den başlayarak arar. Bulursa index’ini verir bulamaz ise -1 değerini verir. Aranan değer ile itemin tam eşleşmesi gerekir.
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
21
int index = listBox1.FindString( string , index); // string ile verilen değerin listBox1 içerisinde index den başlayarak arar bulursa index’ini verir bulamaz ise -1 değerini veririr. index belirtilmez ise baştan başlar.
listBox1.Sorted = true; // Item’ları küçükten büyüğe alfabetik olarak sıralar
boolean b = listBox1.GetSelected(index); // index ’i verilen Item’in seçili olup olmadığını test eder seçili ise true değilse false değerini verir
int index = listBox1.SelectedIndex;// Seçili olan Item ‘in index’ ini veirir
DialogResult d; d=MessageBox.Show(this,”silme”,”sil”,MessageBoxButtons.YesNo); if(d==DialogResult.Yes) { int k = listBox1.SelectedIndex; listBox1.Items.RemoveAt(k); }
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
22
listBox1.SetSelected( index, true / false); // tindex’ i verilen Item ‘ i seçer veya seçimi bırakır
Örn: listBox1.SetSelected( 3, true); //baştan 4. Item seçilir
One: Bir Item Seçilebilir
None : Seçilemez
MultiSimple:Mouse kliklemesi ile istediğimiz kadar seçilebilir
MultiExtended: Ctrl ve Shift ile istenen Item’ lar seçilebilir
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
23
// ekleme butonu private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Contains(textBox1.Text)) MessageBox.Show(textBox1.Text+ " Mevcut ..."); else listBox1.Items.Add(textBox1.Text); }
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
24
//sil butonu private void button2_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex == -1) { MessageBox.Show("Önce bir Item seçiniz.", "Item seçili Değil", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { listBox1.Items.RemoveAt(listBox1.SelectedIndex); } }
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
25
// Temizle butonu private void button3_Click(object sender, EventArgs e) { listBox1.Items.Clear(); }
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
26
//Ekle Butonu private void button1_Click(object sender, EventArgs e) { for (int i=0; i< listBox1.Items.Count; i++) { if(listBox1.GetSelected(i) ) // i. item seçili ise { string s = listBox1.Items[i].ToString(); if( listBox2.Items.Contains(s)==false ) listBox2.Items.Add(listBox1.Items[i].ToString()); } } }
Örnek:
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
27
// Sil Butonu private void button2_Click(object sender, EventArgs e) { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.GetSelected(i)) // i. item seçili ise listBox1.Items.RemoveAt(i); listBox1.Update(); // içeriği günceller } }
Örnek:
ListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
28
Örnek: listBox1 içerisindeki Item’ lerin yerlerinin mouse ile değiştirilmesi .
private string deger; private void listBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point pt = new Point(e.X, e.Y); int index = listBox1.IndexFromPoint(pt); // koordinattaki item indexi deger = listBox1.Items[index].ToString(); } } private void listBox1_MouseUp(object sender, MouseEventArgs e) { Point pt = new Point(e.X, e.Y); int index = listBox1.IndexFromPoint(pt); listBox1.Items.Remove(deger); listBox1.Items.Insert(index, deger); }
CheckedListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
29
CheckedListBox
CheckedListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
30
listBox ile aynı özelliklere sahip sadece görüntüsü biraz farklı. Her Item’ in yanında bir check kutucuğu bulunmaktadır. ListBox’dan farklı olarak comboBox gibi sadece bir item seçilebilir. check kutucuğu birden çok işaretlenebilir, sadece seçme işlemi tek yapılabilir yada hiç yapılamaz. Yani selectin Mode özelliği sadece None ve One olabilir, MultiSimple, MultiExtenden olamaz.
Check Koyma (işaretleme): checkedListBox1.SetItemCheckState (index, CheckState.Checked); CheckState.Checked → işaretle CheckState.UnChecked → işareti kaldır CheckState.Indeterminate → durumu belli değil checkedListBox1.SetItemChecked (index, true/false);
En Önemli Olayı (events) : SelectedIndexChanged -> seçili item değiştirğinde ItemCheck -> check kutucuğu işaretlendiği zaman
CheckedListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
31
if( checkedListBox1. SetItemCheckState( 2, CheckState.Checked) ) { MessgaBox.Show( 2 + « . İtem seçili»); } if( checkedListBox1.SetItemChecked ( 2, true) ) { MessgaBox.Show( 2 + « . İtem seçili»); }
Örn: İşaretli olan itemlerin sayısı ve indexlerinin elde edilmesi for ( int i = 0; i < checkedListBox1.CheckedIndices.Count; i++) listBox1.Items.Add(checkedListBox1.CheckedIndices[i]. ToString() );
CheckedListBox
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
32
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { checkedListBox1.SetItemChecked( checkedListBox1.SelectedIndex, true ); } private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { string s = checkedListBox1.SelectedItem.ToString() ; if ( e.NewValue == CheckState.Checked) // seçili ise
listBox1.Items.Add( s ); else listBox1.Items.Remove( s ); }
Örn: Seçili olanları ListBox nesnesine aktarılsın, seçimi kaldırınca da listBox nesnesinde silinsin.
Soru ve Görüşleriniz
14
.04
.20
11
M
uh
amm
er İL
KU
ÇA
R, M
AK
Ü-2
01
1 B
UR
DU
R
33
?