C# Windows - Lecture
C# Windows - Lecture
C# Windows - Lecture
If you want to change the display text of the Label, you have to set a new
text to the Text property of Label.
label1.Text = "This is my first Label";
Label control can also display an image using the Image property, or a
combination of the ImageIndex and ImageList properties.
label1.Image = Image.FromFile("C:\\testimage.jpg");
The following C# source code shows how to set
some properties of the Label through coding.
Textbox BorderStyle
textBox1.BorderStyle = BorderStyle.Fixed3D;
TextBox Events
Keydown event
You can capture which key is pressed by the user using KeyDown event
e.g
TextChanged Event
When user input or setting the Text property to a new value raises the TextChanged event
e.g
How to Newline in a TextBox
textBox1.Text += "your text" + "\r\n";
Or
textBox1.Text += "your text" + Environment.NewLine;
How to retrieve integer values from textbox ?
int i;
i = int.Parse (textBox1.Text);
C# ComboBox Control
• 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.
• 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.
comboBox1.Items.Add("Sunday"); comboBox1.Items.RemoveAt(1);
comboBox1.Items.Add("Monday"); comboBox1.Items.Remove("Friday");
comboBox1.Items.Add("Tuesday");
ComboBox SelectedItem
string var;
var = comboBox1.Text;
Or
var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
MessageBox.Show(item);
checkedListBox1.Items.Add("Sunday"
, CheckState.Checked);
checkedListBox1.Items.Add("Monday"
, CheckState.Unchecked);
checkedListBox1.Items.Add("Tuesday
", CheckState.Indeterminate);
AddRange method
string[] days = new[] { "Sunday",
"Monday", "Tuesday" };
checkedListBox1.Items.AddRange(days);
C# 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.
Example
private void button1_Click(object sender, EventArgs e)
{ if (radioButton1.Checked == true) {
MessageBox.Show ("You are selected Red !! ");
return; }
else if (radioButton2.Checked == true) {
MessageBox.Show("You are selected Blue !! "); return; }
else {
MessageBox.Show("You are selected Green !! "); return;
}
}
C# CheckBox Control
• allows the user to make multiple selections
from a number of options.
• You can use the CheckBox control ThreeState
property to direct the control to return the
Checked, Unchecked, and Indeterminate
values.
checkBox1.ThreeState = true;
C# PictureBox Control
• The Windows Forms PictureBox control is used to
display images in bitmap, GIF , icon , or JPEG formats.
The following C# program first set its view property as Details and GridLines
property as true and FullRowSelect as true.
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
Example
Cont…
C# Menu Control
• A Menu on a Windows Form is created with a
MainMenu object, which is a collection of
MenuItem objects.
• MainMenu is the container for the Menu
structure of the form and
• menus are made of MenuItem objects that
represent individual parts of a menu.
Example
C# Color Dialog Box
• There are several classes that implement
common dialog boxes, such as color selection ,
print setup etc.
• You can invite a color dialog box by calling
ShowDialog() method.
ColorDialog dlg = new ColorDialog();
dlg.ShowDialog();
Example
C# Font Dialog Box
• Font dialog box represents a common dialog
box that displays a list of fonts that are
currently installed on the system.
• The Font dialog box lets the user choose
attributes for a logical font, such as font family
and associated font style, point size, effects ,
and a script .
Example
C# OpenFile Dialog Box
• The OpenFileDialog component allows users
to browse the folders of their computer or any
computer on the network and select one or
more files to open.
• The dialog box returns the path and name of
the file the user selected in the dialog box.
Example
C# Print Dialog Box
• A user can use the Print dialog box to select a
printer, configure it, and perform a print job.
• Print dialog boxes provide an easy way to
implement Print and Print Setup dialog boxes
in a manner consistent with Windows
standards.
Example
C# Timer Control
• With the Timer Control we can raise events at
a specific interval of time without the
interaction of another thread.
• We have to use Timer Object when we want
to set an interval between events, periodic
checking, to start a process at a fixed time
schedule, to increase or decrease the speed in
an animation graphics with time schedule etc.
How to use C# Timer Control ?
• The Timer Control allows us to set Interval
property in milliseconds.
• one second is equal to 1000 milliseconds.
• By default the Enabled property of Timer
Control is False. So before running the
program we have to set the Enabled property
is True , then only the Timer Control starts its
function.
Example
display the current time in a Label Control.
Database connection