Formu Ön Tanımlı Değerlerle Yeniden Boyutlandır - CSharp


Bir metin dosyasında tanımlı ebat bilgilerinin bir ListBox nesnesinde listelendiği, bu listedeki bir öğeye çift tıklandığında da sahip olduğu değere göre formu yeniden boyutlandırdığı csharp kodu alternatifidir.

Kod


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormuOnTanimliDegerlerleYenidenBoyutlandirCSharp
{
    public partial class AnaForm : Form
    {
        public AnaForm()
        {
            InitializeComponent();
        }

        //--------------------------------------------------
        //Form ile ilgili istenen ebat bilgilerinin 
        //kayıtlı olduğu metin belgesi.
        //
        //Örnek bir liste aşağıda verilmiştir.
        //300;250
        //600;400
        //800;600
        //500;500
        //753;494
        //--------------------------------------------------
        public static string FormEbatlari_Txt { get; set; } =
            Path.Combine(Application.StartupPath, "FormEbatlari.txt");

        //--------------------------------------------------
        //Metin dosyasına en;boy şeklinde alt alta 
        //sıralanmış olan ebat bilgilerini verilen 
        //ListBox nesnesine listelemeye yarıyor.
        //--------------------------------------------------
        void FormEbatlariniYukle(ListBox listBox)
        {
            string[] dizi = File.ReadAllLines(FormEbatlari_Txt);
            for (int i = 0; i < dizi.Length; i++)
            {
                listBox.Items.Add(dizi[i]);
            }
        }

        //--------------------------------------------------
        //ListBox içindeki herhangi bir ebat bilgisine
        //çift tıklandığında form bu ebat bilgilerine
        //göre yeniden boyutlandırılacaktır.
        //--------------------------------------------------
        private void ebatlarListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string[] ebatDizi = ebatlarListBox.SelectedItem.ToString().Split(';');

            this.Width = Convert.ToInt32(ebatDizi[0]);
            this.Height = Convert.ToInt32(ebatDizi[1]);
        }

        private void AnaForm_Load(object sender, EventArgs e)
        {
            FormEbatlariniYukle(ebatlarListBox);
        }
    }
}

Etiketler
csharp