C# Listbox ile Form Rengini Değiştirme - Bilişim Konuları

C# Listbox ile Form Rengini Değiştirme

Bu örneğimizde Visual C# programında ekranda bulunan bir listbox ile formun rengini değiştirme işlemini yapacağız. Öncelikle formumuza bir tane label bir tanede listbox ekliyoruz. Label formun başlığı görevini yapıyor. Listbox nesnesine çift tıklayarak listBox1_SelectedIndexChanged özelliğini aktifleştiriyoruz. Daha öncesinde listbox nesnesine items özelliğinden olmasını istediğimiz renkleri ekliyoruz. Son olarakta seçilen renge göre formun renginin değişmesi için gerekli kodu yazıyoruz.

Programın ekran görüntüleri şöyle olacak:

listbox-form-rengi-degistirne-1

listbox-form-rengi-degistirne-2

listbox-form-rengi-degistirne-3

listbox-form-rengi-degistirne-4

Yukarıdaki şekilde formun rengini değiştiren c# kodu da aşağıdaki gibidir.

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;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == 0) 
            { 
                this.BackColor = Color.Red; 
            }
            if (listBox1.SelectedIndex == 1)
            {
                this.BackColor = Color.Blue;
            }
            if (listBox1.SelectedIndex == 2)
            {
                this.BackColor = Color.Green;
            }
            if (listBox1.SelectedIndex == 3)
            {
                this.BackColor = Color.Yellow;
            }
            if (listBox1.SelectedIndex == 4)
            {
                this.BackColor = Color.Black;
            }
        }
    }
}

Bu Yazıya Tepkin Nedir?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
1

Yorum Yap