Wednesday, May 28, 2014

C# ProgressBar Control

C# ProgressBar Control

A progress bar is a control that an application can use to indicate the progress of a lengthy operation such as calculating a complex result, downloading a large file from the Web etc.
ProgressBar controls are used whenever an operation takes more than a short period of time. The Maximum and Minimum properties define the range of values to represent the progress of a task.

  Minimum : Sets the lower value for the range of valid values for progress.
  Maximum : Sets the upper value for the range of valid values for progress.
  Value   : This property obtains or sets the current level of progress.

By default, Minimum and Maximum are set to 0 and 100. As the task proceeds, the ProgressBar fills in from the left to the right. To delay the program briefly so that you can view changes in the progress bar clearly.
The following C# program shows a simple operation in a progressbar 


 using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            int i;

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 200;

            for (i = 0; i <= 200; i++)
            {
                progressBar1.Value = i;
            }

        }

    }
}

C# DateTimePicker Control

C# DateTimePicker Control

The DateTimePicker control allows you to display and collect date and time from the user with a specified format.
The DateTimePicker control has two parts, a label that displays the selected date and a popup calendar that allows users to select a new date. The most important property of the DateTimePicker is the Value property, which holds the selected date and time.

  dateTimePicker1.Value = DateTime.Today;

The Value property contains the current date and time the control is set to. You can use the Text property or the appropriate member of Value to get the date and time value.

  DateTime iDate;
  iDate = dateTimePicker1.Value;

The control can display one of several styles, depending on its property values. The values can be displayed in four formats, which are set by the Format property: Long, Short, Time, or Custom.

  dateTimePicker1.Format = DateTimePickerFormat.Short;

The following C# program shows how to set and get the value of a DateTimePicker1 control.
using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Short;
            dateTimePicker1.Value = DateTime.Today;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime iDate;
            iDate = dateTimePicker1.Value;
            MessageBox.Show("Selected date is " + iDate);
        }
    }

}

C# ComboBox Control(C#.NET)

C# ComboBox Control(C#.NET)


C# controls are located in the Toolbox of the development environment, and you use them to create objects on a form with a simple series of mouse clicks and dragging motions. A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value .
C# combobox
The user can type a value in the text field or click the button to display a drop down list. You can add individual objects with the Add method. You can delete items with the Remove method or clear the entire list with the Clear method.
Add item to combo box
  comboBox1.Items.Add("Sunday");
How to retrieve value from ComboBox
If you want to retrieve the displayed item to a string variable , you can code like this
  string var;
var = comboBox1.Text;
Or
  var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
MessageBox.Show(item);
How to remove an item from ComboBox

You can remove items from a combobox in two ways. You can remove item at a the specified index or giving a specified item by name.
  comboBox1.Items.RemoveAt(1);
The above code will remove the second item from the combobox.
  comboBox1.Items.Remove("Friday");
The above code will remove the item "Friday" from the combobox.

DropDownStyle


The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The DropDownStyle property also specifies whether the text portion can be edited.
C# dropdownstyle
  
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;

How to set the selected item in a comboBox
You can display selected item in a combobox in two ways.


<span style="font-family: Times, Times New Roman, serif;"> comboBox1.Items.Add("test1"); comboBox1.Items.Add("test2"); comboBox1.Items.Add("test3"); comboBox1.SelectedItem = "test3"; or comboBox1.SelectedIndex = comboBox1.FindStringExact("test3"); </span>
How to populate a combo box with a DataSet
You can Programmatically Binding DataSource to ComboBox in a simple way..
Consider an sql string like...."select au_id,au_lname from authors";
Make a datasource and bind it like the following...


comboBox1.DataSource = ds.Tables[0]; comboBox1.ValueMember = "au_id"; comboBox1.DisplayMember = "au_lname";

Combobox SelectedIndexChanged event

The SelectedIndexChanged event of a combobox fire when you change the slected item in a combobox. If you want to do something when you change the selection, you can write the program on SelectedIndexChanged event. From the following code you can understand how to set values in the SelectedIndexChanged event of a combobox. Drag and drop two combobox on the Form and copy and paste the following source code.



using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.Items.Add("weekdays"); comboBox1.Items.Add("year"); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox2.Items.Clear(); if (comboBox1.SelectedItem == "weekdays") { comboBox2.Items.Add("Sunday"); comboBox2.Items.Add("Monday"); comboBox2.Items.Add("Tuesday"); } else if (comboBox1.SelectedItem == "year") { comboBox2.Items.Add("2012"); comboBox2.Items.Add("2013"); comboBox2.Items.Add("2014"); } } } }