C# Rastgele Üretilen Sayıları Listbox’ta Ters Çevirme ve Sıralama

R

Rooster

only business
Super Moderator
Joined
Aug 10, 2019
Messages
3,215
Reaction score
4,728
Location
Milano
Bu yazımızda C# Windows Form‘da 10 adet rastgele sayı oluşturarak, bu sayıları ters çevirme, küçükten büyüğe sıralama ve büyükten küçüğe sıralama işlemlerini gerçekleştirip listbox içinde göstereceğiz. Örneğimizde 10 adet sayının dizi içine atılmasını sağlayacağız. Sıralama ve ters çevirme işlemlerinin ise basit bir şekilde Sort ve Reverse metotlarıyla gerçekleşmesini sağlayacağız.
Örneğimize ait form tasarımı aşağıdaki gibi olacaktır. Rastgele Dizi Oluştur butonuna basıldığında 1-20 arası sayıların public olarak oluşturduğumuz sayilar dizisine atılmasını sağlayacağız. Aynı zamanda bu oluşturulan sayıların listbox içine eklenmesini sağlayacağız. İşlemleri Gerçekleştir butonu ise oluşturulan bu dizinin ters çevrilmiş halini ListBox2‘de, Küçükten büyüğe sıralanmış halini ListBox3‘te, Büyükten küçüğe sıralanmış halini ise ListBox4‘ te listeleyecek.

1585852476460.png

-
Kodlarımız aşağıdaki gibi olacaktır.
Code:
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;
 
namespace listbox_rasgele_sayi_siralama
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        int[] sayilar = new int[10]; //Public olarak tanımlıyoruz.
 
        private void button1_Click(object sender, EventArgs e) //Rastgele Dizi Oluştur Butonu
        {
            Random rnd = new Random();
            for(int i=0;i<10;i++)
            {
                sayilar[i] = rnd.Next(1, 20);
                listBox1.Items.Add(sayilar[i]);
            }
        }
 
        private void button2_Click(object sender, EventArgs e)//İşlemleri Gerçekleştir Butonu
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            listBox3.Items.Clear();
            listBox4.Items.Clear();
            Array.Reverse(sayilar); //Ters Çevir
            foreach(int sayi in sayilar)
            {
                listBox2.Items.Add(sayi);
            }
 
            Array.Sort(sayilar); //Küçükten büyüğe sırala
            foreach (int sayi in sayilar)
            {
                listBox3.Items.Add(sayi);
            }
 
            Array.Reverse(sayilar);//Büyükten küçüğe Sırala
            foreach (int sayi in sayilar)
            {
                listBox4.Items.Add(sayi);
            }
 
        }
 
    }
}

1585852534591.png
 
SPAM IS FORBIDDEN!
  • SPAMMERS ARE BANNED FROM THE FORUM AND CANNOT USE ANY OF THE CHEATS
  • For example: thanks, thx, very good, asdqwe, working, ty and so on!
  • For example: Writing the same message over and over. thanks, thx and so on!
  • Copying and copying someone else's message is prohibited.
  • It is forbidden to send messages to increase the number of comments on threads that you have no knowledge of.
  • Write your own opinion when commenting!
  • If you see spam message, please let us know with the REPORT button!

Tema düzenleyici

Top Bottom