Thursday 4 December 2014

C# Window Programming


Combo Box

A combo box is a commonly used graphical user interface widget (or control). Traditionally, it is a combination of a drop-down list or list box and a single-line editable textbox, allowing the user to either type a value directly or select a value from the list. The term "combo box" is sometimes used to mean "drop-down list". In both Java and .NET, "combo box" is not a synonym for "drop-down list". Definition of "drop down list" is sometimes clarified with terms such as "non-editable combo box" (or something similar) to distinguish it from "combo box".




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

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("Plz enter an item");
                textBox1.Focus();
            }
            else
            {
                comboBox1.Items.Add(textBox1.Text);
                textBox1.Text = string.Empty;
                textBox1.Focus();
                lblCount.Text  = "Total Item : " + comboBox1.Items.Count.ToString();
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblText.Text = comboBox1.Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Plz select an item from combobox");
                comboBox1.Focus();
            }
            else
            {
                comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
                if (comboBox1.SelectedIndex == -1) // make combobox empty after deleting an item
                {
                    comboBox1.Text = "";
                }
                lblText.Text = "";
                lblCount.Text = "Total Item : " + comboBox1.Items.Count.ToString();
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}


ListBox

ListBox stores several text items. It can interact with other controls, including Button controls. We use this control in Windows Forms.


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 LIst_Box
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAddList1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Plz enter text to enter list box 1");
                textBox1.Focus();
            }
            else
            {
                listBox1.Items.Add(textBox1.Text);
                lblCountList1.Text = "Total Item : " + listBox1.Items.Count.ToString();
                textBox1.Text = "";
                textBox1.Focus();
            }
        }

        private void btnDeleteList1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Plz select an item from list 1");
                listBox1.Focus();
            }
            else
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                lblCountList1.Text = "Total Item : " + listBox1.Items.Count.ToString();
            }
        }

        private void btnMoveAllList2_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("List 1 is empty nothing to move !");
            }
            else
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                }
                listBox1.Items.Clear();
                lblCount2.Text = "Total Item : " + listBox2.Items.Count.ToString();
                lblCountList1.Text = "Total Item : 0";
            }
        }

        private void btnAddList2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("Plz enter text to enter list box 2");
                textBox2.Focus();
            }
            else
            {
                listBox2.Items.Add(textBox2.Text);
                lblCount2.Text = "Total Item : " + listBox2.Items.Count.ToString();
                textBox2.Text = "";
                textBox2.Focus();
            }
        }

        private void btnDeleteList2_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex == -1)
            {
                MessageBox.Show("Plz select an item from list 2");
                listBox2.Focus();
            }
            else
            {
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
                lblCount2.Text = "Total Item : " + listBox2.Items.Count.ToString();
            }
        }

        private void btnMoveList2_Click(object sender, EventArgs e)
        {
           
                if (listBox1.Items.Count == 0)
                {
                    MessageBox.Show("List 1 is Empty Nothing to move !");
                }
                else
                {
                    if (listBox1.SelectedIndex == -1)
                    {
                        MessageBox.Show("Plz select an item from list 1 ");
                        listBox1.Focus();
                    }
                    else
                    {
                        listBox2.Items.Add(listBox1.Text);
                        listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                        lblCountList1.Text = "Total Item : " + listBox1.Items.Count.ToString();
                        lblCount2.Text = "Total Item : " + listBox2.Items.Count.ToString();
                      
                    }
                }
           
        }

        private void btnMoveList1_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("List 2 is Empty Nothing to move !");
            }
            else
            {
                if (listBox2.SelectedIndex == -1)
                {
                    MessageBox.Show("Plz select an item from list 2 ");
                   
                }
                else
                {
                    listBox1.Items.Add(listBox2.Text);
                    listBox2.Items.RemoveAt(listBox2.SelectedIndex);
                    lblCountList1.Text = "Total Item : " + listBox1.Items.Count.ToString();
                    lblCount2.Text = "Total Item : " + listBox2.Items.Count.ToString();

                }
            }
        }

        private void btnMoveAllLlist1_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("List 2 is empty nothing to move !");
            }
            else
            {
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i]);
                }
                listBox2.Items.Clear();
                lblCountList1.Text = "Total Item : " + listBox1.Items.Count.ToString();
                lblCount2.Text = "Total item : 0";
            }
        }         


    }
}


Radio Button

A radio button is a round circle representing choices in a common option list form in a graphical user interface.Radio buttons are commonly used when only one option is selectable.

Check Box

Alternatively referred to as a selection box, a check box is a square box where the user can click to say that they want or have a particular setting.Check boxes are commonly used when more than one option may need to be selected.



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

        private void btnSubmitCheck_Click(object sender, EventArgs e)
        {
            if (chkBlue.Checked == false && chkGreen.Checked == false && chkRed.Checked == false)
            {
                lblCheckText.Text = "Please select a color";
            }
            else if (chkRed.Checked == true && chkGreen.Checked == true && chkBlue.Checked == true)
                lblCheckText.Text = "Red,Green,Blue";
            else if (chkBlue.Checked == true && chkGreen.Checked == true && chkRed.Checked == false)
                lblCheckText.Text = "Blue,Green";
            else if (chkBlue.Checked == false && chkGreen.Checked == true && chkRed.Checked == true)
                lblCheckText.Text = "Red,Green";
            else if (chkBlue.Checked == true && chkGreen.Checked == false && chkRed.Checked == true)
                lblCheckText.Text = "Blue,Red";
            else if (chkBlue.Checked == true)
                lblCheckText.Text = "Blue";
            else if (chkGreen.Checked == true)
                lblCheckText.Text = "Green";
            else if (chkRed.Checked == true)
                lblCheckText.Text = "Red";
           
        }

        private void btnSubmitRadio_Click(object sender, EventArgs e)
        {
            if (rdoBlue.Checked == true)
            {
                this.BackColor = Color.Blue;
                btnSubmitCheck.BackColor = Color.Blue;
                btnSubmitRadio.BackColor = Color.Blue;
            }
            if (rdoGreen.Checked == true)
            {
                this.BackColor = Color.Green;
                btnSubmitCheck.BackColor = Color.Green;
                btnSubmitRadio.BackColor = Color.Green;
            }
            if (rdoRed.Checked == true)
            {
                this.BackColor = Color.Red;
                btnSubmitCheck.BackColor = Color.Red;
                btnSubmitRadio.BackColor = Color.Red;
            }
               
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
                MessageBox.Show("Red is selected");
            else
                MessageBox.Show("Red is Unselected");
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
                MessageBox.Show("Green is selected");
          
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
                MessageBox.Show("Red is selected");
           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
                MessageBox.Show("Green is selected");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

No comments:

Post a Comment