C# Pratical
C# Pratical
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 Combo Box displays a text box combined with a List Box, which enables
the user to select items from the list or enter a new value
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
A ComboBox in C# is a graphical user interface (GUI) element that combines the features of a
text box and a drop-down list.
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
//OR
this.comboBox1.Items.Add("A");
this.comboBox1.Items.Insert(0, "B");
this.comboBox1.Items.Insert(1, "C");
this.comboBox1.Items.Insert(2, "D");
//step no-5
this.comboBox2.Items.Add("Week");
this.comboBox2.Items.Add("Month");
}
comboBox1.Items.Clear();
if (this.comboBox2.SelectedItem == "Week")
// if (comboBox2.SelectedItem.Equals ("week") )
{
this.comboBox1.Items.Add("Sunday");
this.comboBox1.Items.Add("Monday");
this.comboBox1.Items.Add("Tuesday");
this.comboBox1.Items.Add("Wednesday");
this.comboBox1.Items.Add("Thursday");
this.comboBox1.Items.Add("Friday");
}
else if(comboBox2.SelectedItem =="Month")
// else if (comboBox2.SelectedItem.Equals ("Month") )
{
this.comboBox1.Items.Add ("January");
this.comboBox1.Items.Add ("February");
this.comboBox1.Items.Add ("March");
this.comboBox1.Items.Add ("April");
this.comboBox1.Items.Add ("May");
} }
-------------------------------------------------------------------------------------------------------------------------------
RadioButton Control
A radio button or option button enables the user to select a single option
from a group of choices when paired with other RadioButton controls. When
a user clicks on a radio button, it becomes checked, and all other radio buttons with
same group become unchecked
First step:
this.redlbl.Visible = false;
bluelbl.Visible = false;
greenlbl.Visible = false;
}
Second step:
if (redrbtn.Checked == true)
{
redlbl.Visible = true;
bluelbl.Visible = false;
greenlbl.Visible = false;
}
}
Third step:
bluelbl.Visible = true;
redlbl.Visible = false;
greenlbl.Visible = false;
}
}
Fourth step:
The ListBox control is the simplest of the list-based controls and serves primarily to display a
simple list of items in an easy-to-navigate user interface from which users can select one or
more items.
First step:
Second step: write the following code under form load event
Step no-3:
}
else if (this.textBox1.Text != "")
{
this.listBox1.Items.Add(this.textBox1.Text);
this.textBox1.Clear();
this.textBox1.Focus();
}
Step no-4:
Step no-5:
listBox1.DataSource = strlist;
}
-------------------------------------------------------------------------------------------------------------------------------
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 WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// OR
//firsttxt.Clear();
//secondtxt.Clear();
//resulttxt.Clear();
}
} } }
------------------------------------------------------------------------------------------------------------------------------------------
The following figure illustrate the way of adding new form to the existing form
When we want to display the new Form from existing Form write the following code under click envent.
Message Box
Write the following code under button click event. This code will reveals different aspects of Message
Box.
//second step
// MessageBox.Show("this is seventh semester","Semester",
MessageBoxButtons.YesNo);
// third step
if (MessageBox.Show("information", "semester",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
// Application.Exit();
this.Close();
}
if (Convert.ToBoolean(DialogResult.No ))
{
MessageBox.Show("please, try again");
-------------------------------------------------------------------------------------------------------------------------------
CheckedListBox Control
The CheckedListBox control gives you all the capability of a list box and
also allows you to display a check mark next to the items in the list box.
The user can place a check mark by one or more items and the checked items can
be navigated with the CheckedListBox.CheckedItemCollection and
CheckedListBox.CheckedIndexCollection.
Write the following code under form load event when CheckedListBox controls added to window
form.
this.checkedListBox1.Items.Add("Sunday");
this.checkedListBox1.Items.Add("Monday");
this.checkedListBox1.Items.Add("Tuesday");
Second step:
You can add individual items to the list with the Add method. The
CheckedListBox object supports three states through the CheckState
enumeration: Checked, Indeterminate, and Unchecked.
this.checkedListBox1.Items.Add("Sunday", CheckState.Checked );
this.checkedListBox1.Items.Add("Monday",CheckState.Indeterminate
);
this.checkedListBox1.Items.Add("Tuesday",CheckState.Unchecked );
---------------------------------------------------------------------------------------------
PictureBox Control
You can set the Image property to the Image you want to display, either at design
time or at run time. You can programmatically change the image displayed in a
picture box, which is particularly useful when you use a single form to display
different pieces of information.
this.pictureBox1.Image = Image.FromFile("C:\\Users\\Airplane_img.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
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.
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.
this.progressBar1.Value = x;
-------------------------------------------------------------------------------------
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.
The following code illustrate the above design which, is used to find out difference
between two selected date or time.
from = dateTimePicker1.Value;
to = dateTimePicker2.Value;
}
Treeview Control
The TreeView control contains a hierarchy of TreeViewItem controls. It
provides a way to display information in a hierarchical structure by using
collapsible nodes . The top level in a tree view are root nodes that can be expanded
or collapsed if the nodes have child nodes.
You can explicitly define the TreeView content or a data source can provide the
content. The user can expand the TreeNode by clicking the plus sign (+) button.
Write the following code under form load event which reveals above design
treeView1.Nodes.Add("Classes");
treeView1.Nodes[0].Nodes.Add("Fist Year");
treeView1.Nodes[0].Nodes[0].Nodes.Add("Early Morning");
treeView1.Nodes[0].Nodes[0].Nodes.Add("Late Morning");
treeView1.Nodes[0].Nodes[0].Nodes.Add("Evening");
treeView1.Nodes[0].Nodes.Add("Second Year");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Early Morning");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Late Morning");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Evening");
------------------------------------------------------------------------------------
ListView Control
listView1.GridLines = true;
// listView1.FullRowSelect = true;
// First Step:the following code shows adding columns to listview at run time
//this.listView1.View = View.Details;
//this.listView1.Columns.Add("ID", 100);
//this.listView1.Columns.Add("Name", 100);
//Second Step
//ListViewItem litem;
//litem =listView1.Items.Add("1");
//litem.SubItems.Add("asad");
//litem.SubItems.Add("SE");
//litem.SubItems.Add("A");
//litem = listView1.Items.Add("2");
//litem.SubItems.Add("ajmal");
//litem.SubItems.Add("SE");
//litem.SubItems.Add("B");
listView1.Items.Add(litem);
}
private void listView1_Click(object sender, EventArgs e)
{
//Fourth Step
this.idtxt.Text = listView1.SelectedItems[0].SubItems[0].Text;
this.nametxt.Text = listView1.SelectedItems[0].SubItems[1].Text;
this.depttxt.Text = listView1.SelectedItems[0].SubItems[2].Text;
this.gradetxt.Text = listView1.SelectedItems[0].SubItems[3].Text;
}
this.listView1.Items.Clear();
------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------
namespace WindowsFormsApplication76
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
this.richTextBox1.Undo();
}
}
}
else
{
this.cutToolStripMenuItem.Enabled = false;
this.copyToolStripMenuItem.Enabled = false;
this.deleteToolStripMenuItem.Enabled = false;
}
}
}
}
}
}
}
}
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;
using System.Data.SqlClient;
namespace WindowsFormsApplication86
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
public Form1()
{
con.ConnectionString = "data source=.; initial catalog=jahan; integrated
security=true";
InitializeComponent();
}
}
con.Close();
}
}
}