۱۰

کنترل‌های پایه Windows Forms 🎛️

آشنایی با کنترل‌های اساسی برای ساخت رابط‌های کاربری قدرتمند!

Button و Label: پایه‌های رابط کاربری

Button و Label دو کنترل اساسی هستند. Button برای تعامل با کاربر و Label برای نمایش متن استفاده می‌شود. این دو کنترل در تقریباً همه اپلیکیشن‌ها وجود دارند.

استفاده از Button و Label C#
public partial class Form1 : Form
{
    private Button btnClick;
    private Label lblMessage;
    private int clickCount = 0;
    
    public Form1()
    {
        InitializeComponent();
        SetupControls();
    }
    
    private void SetupControls()
    {
        // تنظیمات فرم
        this.Text = "Button و Label";
        this.Size = new Size(350, 200);
        
        // ایجاد Label
        lblMessage = new Label();
        lblMessage.Text = "روی دکمه کلیک کنید!";
        lblMessage.Location = new Point(50, 30);
        lblMessage.Size = new Size(250, 30);
        lblMessage.Font = new Font("Tahoma", 10);
        
        // ایجاد Button
        btnClick = new Button();
        btnClick.Text = "کلیک کنید";
        btnClick.Location = new Point(50, 80);
        btnClick.Size = new Size(120, 35);
        btnClick.BackColor = Color.LightBlue;
        btnClick.Click += BtnClick_Click;
        
        // اضافه کردن کنترل‌ها
        this.Controls.Add(lblMessage);
        this.Controls.Add(btnClick);
    }
    
    private void BtnClick_Click(object sender, EventArgs e)
    {
        clickCount++;
        lblMessage.Text = $"تعداد کلیک‌ها: {clickCount}";
        
        if (clickCount >= 5)
        {
            btnClick.Text = "عالی!";
            btnClick.BackColor = Color.LightGreen;
        }
    }
}

TextBox و کنترل‌های ورودی

TextBox برای دریافت متن از کاربر استفاده می‌شود. همچنین کنترل‌هایی مثل CheckBox و RadioButton برای انتخاب‌های مختلف کاربرد دارند.

  • TextBox: برای ورود متن از کاربر استفاده می‌شود.
  • CheckBox: برای انتخاب چندگانه (می‌تواند چک یا آنچک باشد).
  • RadioButton: برای انتخاب یکی از چند گزینه استفاده می‌شود.
فرم ثبت‌نام ساده C#
public partial class RegistrationForm : Form
{
    private TextBox txtName, txtEmail;
    private CheckBox chkNewsletter;
    private RadioButton rbMale, rbFemale;
    private Button btnSubmit;
    private Label lblResult;
    
    public RegistrationForm()
    {
        InitializeComponent();
        SetupRegistrationForm();
    }
    
    private void SetupRegistrationForm()
    {
        this.Text = "فرم ثبت‌نام";
        this.Size = new Size(400, 350);
        
        // TextBox برای نام
        Label lblName = new Label() { Text = "نام:", Location = new Point(20, 20), Size = new Size(50, 25) };
        txtName = new TextBox() { Location = new Point(80, 20), Size = new Size(200, 25) };
        
        // TextBox برای ایمیل
        Label lblEmail = new Label() { Text = "ایمیل:", Location = new Point(20, 60), Size = new Size(50, 25) };
        txtEmail = new TextBox() { Location = new Point(80, 60), Size = new Size(200, 25) };
        
        // CheckBox برای خبرنامه
        chkNewsletter = new CheckBox() { Text = "عضویت در خبرنامه", Location = new Point(20, 100), Size = new Size(150, 25) };
        
        // RadioButton برای جنسیت
        Label lblGender = new Label() { Text = "جنسیت:", Location = new Point(20, 140), Size = new Size(60, 25) };
        rbMale = new RadioButton() { Text = "مرد", Location = new Point(90, 140), Size = new Size(60, 25) };
        rbFemale = new RadioButton() { Text = "زن", Location = new Point(160, 140), Size = new Size(60, 25) };
        
        // Button برای ثبت
        btnSubmit = new Button() { Text = "ثبت‌نام", Location = new Point(20, 180), Size = new Size(100, 30) };
        btnSubmit.Click += BtnSubmit_Click;
        
        // Label برای نتیجه
        lblResult = new Label() { Location = new Point(20, 220), Size = new Size(350, 80), BackColor = Color.LightYellow };
        
        // اضافه کردن کنترل‌ها
        this.Controls.AddRange(new Control[] { lblName, txtName, lblEmail, txtEmail, 
                                              chkNewsletter, lblGender, rbMale, rbFemale, 
                                              btnSubmit, lblResult });
    }
    
    private void BtnSubmit_Click(object sender, EventArgs e)
    {
        string name = txtName.Text;
        string email = txtEmail.Text;
        string gender = rbMale.Checked ? "مرد" : (rbFemale.Checked ? "زن" : "نامشخص");
        string newsletter = chkNewsletter.Checked ? "بله" : "خیر";
        
        lblResult.Text = $"نام: {name}\nایمیل: {email}\nجنسیت: {gender}\nخبرنامه: {newsletter}";
    }
}

ComboBox: لیست کشویی

ComboBox یک لیست کشویی است که به کاربر امکان انتخاب یکی از گزینه‌های از پیش تعریف شده را می‌دهد. این کنترل برای صرفه‌جویی در فضا و ارائه گزینه‌های محدود مفید است.

استفاده از ComboBox C#
private ComboBox cmbCities;
private Label lblSelectedCity;

private void SetupComboBox()
{
    // ایجاد ComboBox
    cmbCities = new ComboBox();
    cmbCities.Location = new Point(50, 50);
    cmbCities.Size = new Size(150, 25);
    cmbCities.DropDownStyle = ComboBoxStyle.DropDownList; // فقط انتخاب از لیست
    
    // اضافه کردن آیتم‌ها
    cmbCities.Items.AddRange(new string[] { "تهران", "اصفهان", "شیراز", "مشهد", "تبریز" });
    cmbCities.SelectedIndex = 0; // انتخاب اولین آیتم
    
    // رویداد تغییر انتخاب
    cmbCities.SelectedIndexChanged += CmbCities_SelectedIndexChanged;
    
    // Label برای نمایش انتخاب
    lblSelectedCity = new Label();
    lblSelectedCity.Location = new Point(50, 90);
    lblSelectedCity.Size = new Size(200, 25);
    lblSelectedCity.Text = "شهر انتخابی: تهران";
    
    this.Controls.Add(cmbCities);
    this.Controls.Add(lblSelectedCity);
}

private void CmbCities_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cmb = sender as ComboBox;
    lblSelectedCity.Text = $"شهر انتخابی: {cmb.SelectedItem}";
}

تمرین! 🧠

یک فرم ماشین‌حساب ساده بسازید که شامل دو TextBox برای اعداد، یک ComboBox برای انتخاب عملیات (+، -، ×، ÷)، یک Button برای محاسبه و یک Label برای نمایش نتیجه باشد.

جواب تمرین

public partial class CalculatorForm : Form
{
    private TextBox txtNumber1, txtNumber2;
    private ComboBox cmbOperation;
    private Button btnCalculate;
    private Label lblResult;
    
    public CalculatorForm()
    {
        InitializeComponent();
        SetupCalculator();
    }
    
    private void SetupCalculator()
    {
        this.Text = "ماشین‌حساب ساده";
        this.Size = new Size(300, 250);
        
        // TextBox اول
        Label lbl1 = new Label() { Text = "عدد اول:", Location = new Point(20, 20), Size = new Size(60, 25) };
        txtNumber1 = new TextBox() { Location = new Point(90, 20), Size = new Size(100, 25) };
        
        // ComboBox عملیات
        Label lblOp = new Label() { Text = "عملیات:", Location = new Point(20, 60), Size = new Size(60, 25) };
        cmbOperation = new ComboBox() { Location = new Point(90, 60), Size = new Size(100, 25) };
        cmbOperation.Items.AddRange(new string[] { "+", "-", "×", "÷" });
        cmbOperation.SelectedIndex = 0;
        
        // TextBox دوم
        Label lbl2 = new Label() { Text = "عدد دوم:", Location = new Point(20, 100), Size = new Size(60, 25) };
        txtNumber2 = new TextBox() { Location = new Point(90, 100), Size = new Size(100, 25) };
        
        // Button محاسبه
        btnCalculate = new Button() { Text = "محاسبه", Location = new Point(90, 140), Size = new Size(80, 30) };
        btnCalculate.Click += BtnCalculate_Click;
        
        // Label نتیجه
        lblResult = new Label() { Location = new Point(20, 180), Size = new Size(250, 25), BackColor = Color.LightGray };
        
        this.Controls.AddRange(new Control[] { lbl1, txtNumber1, lblOp, cmbOperation, 
                                              lbl2, txtNumber2, btnCalculate, lblResult });
    }
    
    private void BtnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            double num1 = Convert.ToDouble(txtNumber1.Text);
            double num2 = Convert.ToDouble(txtNumber2.Text);
            string operation = cmbOperation.SelectedItem.ToString();
            double result = 0;
            
            switch (operation)
            {
                case "+": result = num1 + num2; break;
                case "-": result = num1 - num2; break;
                case "×": result = num1 * num2; break;
                case "÷": 
                    if (num2 != 0) result = num1 / num2;
                    else { lblResult.Text = "خطا: تقسیم بر صفر!"; return; }
                    break;
            }
            
            lblResult.Text = $"نتیجه: {result}";
        }
        catch (Exception ex)
        {
            lblResult.Text = "خطا: لطفاً اعداد معتبر وارد کنید.";
        }
    }
}