C# Textbox'a girilen koda göre resim nasıl verilir?

Tuğra Akın

Decipat
Katılım
18 Temmuz 2022
Mesajlar
2.797
Makaleler
13
Çözümler
1
Daha fazla  
Sistem Özellikleri
İ5 11400h Rtx 3060 6 Gb laptop
Cinsiyet
Erkek
Textbox'a girilen yazıya (sayıya) göre resim gelmesini istiyorum, bunun için hangi kodu kullanmam gerek?
 
Son düzenleyen: Moderatör:
Yukarıdaki gibi Switch-Case veya if ile de yapabilirsin. Aşağıda hazırladığım ufak bir kaç örnek bırakıyorum.

C#:
private void button1_Click(object sender, EventArgs e)
        {
            /* Switch Case Yöntemi */
            switch (textBox1.Text)
            {
                case "1":
                    pictureBox1.ImageLocation = @"C:\resim1.png";
                    break;
                case "2":
                    pictureBox1.ImageLocation = @"C:\resim2.png";
                    break;
                    //...
            }

            /*/
             * Eğer bütün resimler numaralı ise aşağıdaki gibi de kullanılabilir.
             * Bu yöntem ile textbox'un int değişkene çevrilip çevrilemeyeceğini kontrol ediyoruz.
            /*/
            if (int.TryParse(textBox1.Text, out int a))            
                pictureBox1.ImageLocation = $"C:\\resim{textBox1.Text}.png";



            // Eğer kullanmak istenilen resim içe aktarılmış ise;
            if (textBox1.Text == "1")
                pictureBox1.Image = Properties.Resources.resim1;
            else if (textBox1.Text == "2")
                pictureBox1.Image = Properties.Resources.resim2;
        }
 
Yukarıdaki gibi Switch-CAS'e veya if ile de yapabilirsin. Aşağıda hazırladığım ufak birkaç örnek bırakıyorum.

C#:
private void button1_Click(object sender, EventArgs e)
 {
 /* Switch Case Yöntemi */
 switch (textBox1.Text)
 {
 case "1":
 pictureBox1.ImageLocation = @"C:\resim1.png";
 break;
 case "2":
 pictureBox1.ImageLocation = @"C:\resim2.png";
 break;
 //...
 }

 /*/
 * Eğer bütün resimler numaralı ise aşağıdaki gibi de kullanılabilir.
 * Bu yöntem ile textbox'un int değişkene çevrilip çevrilemeyeceğini kontrol ediyoruz.
 /*/
 if (int.TryParse(textBox1.Text, out int a))
 pictureBox1.ImageLocation = $"C:\\resim{textBox1.Text}.png";

 // Eğer kullanmak istenilen resim içe aktarılmış ise;
 if (textBox1.Text == "1")
 pictureBox1.Image = Properties.Resources.resim1;
 else if (textBox1.Text == "2")
 pictureBox1.Image = Properties.Resources.resim2;
 }

Sağ olun hocam Switch CAS'e ile halletim.
 

Geri
Yukarı