0% found this document useful (0 votes)
2 views

Chapter2-Windows Forms and Windows Forms Controls-Part 2

The document provides an overview of using DateTimePicker, ListBox, CheckedListBox, and Menu components in a programming context. It includes code snippets for setting formats, adding items, and handling events for these controls. Additionally, it discusses creating menus at runtime and using ToolStrip and ContextMenuStrip for user interface design.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter2-Windows Forms and Windows Forms Controls-Part 2

The document provides an overview of using DateTimePicker, ListBox, CheckedListBox, and Menu components in a programming context. It includes code snippets for setting formats, adding items, and handling events for these controls. Additionally, it discusses creating menus at runtime and using ToolStrip and ContextMenuStrip for user interface design.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

DateTimePicker & MonthCalendar

Name Description
Format Gets or sets the format of the date
and time displayed in the control
CustomFormat Gets or sets the custom date/time
format string
Value Gets or sets the date/time value
assigned to the control.

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
Next Slide to see list Custom Format
Format string Description
d The one- or two-digit day.
The two-digit day. Single-digit day values
dd are preceded by a 0.
The three-character day-of-week
ddd abbreviation.
dddd The full day-of-week name.
The one- or two-digit hour in 12-hour
h format.
The two-digit hour in 12-hour format.
hh Single digit values are preceded by a 0.
The one- or two-digit hour in 24-hour
H format.
Format string Description
The two-digit hour in 24-hour format. Single
HH digit values are preceded by a 0.
m The one- or two-digit minute.
The two-digit minute. Single digit values are
mm preceded by a 0.
M The one- or two-digit month number.
The two-digit minute. Single digit values are
mm preceded by a 0.
M The one- or two-digit month number.
The two-digit month number. Single digit
MM values are preceded by a 0.
MMM The three-character month abbreviation.
Format string Description
MMMM The full month name.
s The one- or two-digit seconds.
The two-digit seconds. Single digit values are
ss preceded by a 0.
The one-letter A.M./P.M. abbreviation (A.M.
t is displayed as "A").
The two-letter A.M./P.M. abbreviation (A.M.
tt is displayed as "AM").
y The one-digit year (2001 is displayed as "1").
The last two digits of the year (2001 is
yy displayed as "01").
yyyy The full year (2001 is displayed as "2001").
ListBox private void
frmListBox_Load(object
sender, EventArgs e)
{ listBox1.Items.Clear();
for (int i = 0; i < 10; i++)
listBox1.Items.Add("Item " +
i);
}
private void
listBox1_SelectedIndexChanged
(object sender, EventArgs e)
{
lblMessage.Text =
listBox1.Text
+" was clicked!";
}
And We can use AddRange method to
add data:
private void
frmListBox_Load(object sender,
EventArgs e)
{
string[] strArr = new string[]
{ "Tèo","Tí","Bin","Bo"};

listBox1.Items.AddRange(strArr);
}
public class CStudent
{ private string m_strID;
private string m_strName;
public CStudent(string strID,
string strName)
{ this.m_strID = strID;
this.m_strName = strName;
}
public string ID
{ get { return this.m_strID; }
set { this.m_strID = value; }
}
public string Name
{ get { return this.m_strName; }
set { this.m_strName = value; }
}
}
using System.Collections;
Also We can use DataSource to
display data
ArrayList arr = new
ArrayList();
for(int i=0;i<10;i++)
{arr.Add(new
CStudent("ID_"+i,"Name "+i));
}
listBox1.DataSource = arr;
listBox1.ValueMember = "ID";
listBox1.DisplayMember =
To get object from Listbox, We
could use coding below:
if (listBox1.SelectedItem !=
null)
{
CStudent svTeo = (CStudent )

listBox1.SelectedItem;
lblMessage.Text =
aStudent.Name
CheckedListBox
btnAdd btnAddAll

chklbLeft
chklbRight

btnRemove btnRemoveAll
Use Items to add data
private void
frmCheckListBox_Load
(object sender, EventArgs e)
{ chklbLeft.Items.Add("Tèo");

chklbLeft.Items.Add("Tí");

chklbLeft.Items.Add("Bin");

chklbLeft.Items.Add("Bo");
}
Or we could use AddRange
How to process Items
Checked???
Case 1:
CheckedListBox.CheckedIndexCollection
indexCollection = chklbLeft.CheckedIndices;
string strChecked = "";
foreach (int i in indexCollection)
{
strChecked += i + ";";
}
MessageBox.Show(strChecked);
How to process Items Checked???
Case 2:
CheckedListBox.CheckedItemCollecti
on items = chklbLeft.CheckedItems;
string strChecked = "";
foreach (string s in items)
{
strChecked += s + ";";
}
MessageBox.Show(strChecked);
How to process Items
Checked???
Case 3:
string strChecked = "";
for (int i = 0; i <
chklbLeft.Items.Count; i++){
if
(chklbLeft.GetItemChecked(i))
{
//Process Item checked here
}
}
Go back ChecklistBox Example:
private void btnAdd_Click
(object sender, EventArgs e)
{
foreach(int i in
chklbLeft.CheckedIndices)
{

chklbRight.Items.Add(chklbLeft.Items[i]
);
}
foreach (string s in chklbRight.Items)
{chklbLeft.Items.Remove(s);}
}
private void btnAddAll_Click
(object sender, EventArgs e)
{
chklbRight.Items.AddRange
(chklbLeft.Items);

chklbLeft.Items.Clear();
}
private void btnRemove_Click
(object sender, EventArgs e)
{
foreach (string s in
chklbRight.CheckedItems)
chklbLeft.Items.Add(s);
foreach(string s in
chklbLeft.Items)

chklbRight.Items.Remove(s);
}
private void
btnRemoveAll_Click
(object sender, EventArgs e)
{
chklbLeft.Items.AddRange
(chklbRight.Items);

chklbRight.Items.Clear();
}
Menu (2 ways to use)

MainMenu MenuStrip

MenuItem ToolStripMenuItem
Menu MainMenuStrip
Property Property
I would like to tell you : using
MainMenu is the basic Menu.
In this case, I will demo add
Menu at Runtime for you.

Please see next slide !


Coding to create Menu at Runtime
private MainMenu mainMenuBar;
private MenuItem menuFile, menuEdit,
menuFileNew, menuFileOpen, menuFileExit,
menuEditCut, menuEditCopy,
menuEditPaste;
private void createMenu()
{
mainMenuBar = new MainMenu();
this.Menu = mainMenuBar;
menuFile=new MenuItem("File");
menuFileNew = new
MenuItem("New");
menuFileOpen = new
MenuItem("Open");
menuFileExit = new
private void createMenu(){……
menuFile.MenuItems.Add("-");
menuFile.MenuItems.Add(menuFileExit);
mainMenuBar.MenuItems.Add(menuFile);
menuEdit = new MenuItem("Edit");
menuEditCut = new MenuItem("Cut");
menuEditCopy = new MenuItem("Copy");
menuEditPaste = new
MenuItem("Paste");
menuEdit.MenuItems.AddRange(new
MenuItem[]
{ menuEditCut,menuEditCopy,menuEditPa
ste});
private void attachEvents()
{
menuFileNew.Click += process_MenuClick;
menuFileOpen.Click += process_MenuClick;
menuFileExit.Click += process_MenuClick;
menuEditCut.Click += process_MenuClick;
menuEditCopy.Click += process_MenuClick;
menuEditPaste.Click += process_MenuClick;
}
private void process_MenuClick
(object sender, EventArgs e)
{
if
(sender.Equals(menuFileExit))
{
Application.Exit();
}
}

private void frmMenuBasic_Load


(object sender, EventArgs e)
{createMenu();
MenuStrip
Designer

Click on button to
add MenuItem
Click Add button to add new MenuItem

Text to Display

Click here to add


Sub MenuItem

MenuItem’s Name
Image Icon

Text to Display

Click here to add


Sub MenuItem

MenuItem’s Name
We could add MenuItem direct
on the Windows Form. Enter
Name in the “Type Here”
Add Event
for each
MenuItem
Image List

Drag & Drop


Gets the color
depth of the
image list

Gets the
ImageList.
ImageCollection
for this image list.
See Next Slide

Gets or sets the


size of the images
in the image list
Click Add button
to insert Image
into Collection
Select image &
click Open
MenuStrip
At
Runtime
private MenuStrip
menuBar;
private
ToolStripMenuItem
menuFile, menuEdit,
menuFileNew,
menuFileOpen,
menuFileExit,
private void createMenu()
{menuBar = new MenuStrip();
menuBar.Font = new
Font("arial", 36,
FontStyle.Bold,
GraphicsUnit.Pixel);
this.MainMenuStrip = menuBar;
menuFile = new
ToolStripMenuItem("File");
menuFileNew = new
ToolStripMenuItem("New");
menuFileNew.Image =
menuFileOpen = new
ToolStripMenuItem("Open");
ToolStripSeparator sp = new
ToolStripSeparator();
menuFileExit = new
ToolStripMenuItem("Exit");
menuFileExit.Image =
imageList1.Images[1];
menuFile.DropDownItems.Add(
menuFileNew);
menuFile.DropDownItems.Add(
menuFileOpen);
menuFile.DropDownItems.Add(sp)
;
menuFile.DropDownItems.Add(
menuFileExit);
menuEdit = new
ToolStripMenuItem("Edit");
menuEditCut = new
ToolStripMenuItem("Cut");
menuEditCopy = new
ToolStripMenuItem("Copy");
menuEditPaste = new
menuEdit.DropDownItems.AddRang
e(new ToolStripItem[]
{ menuEditCut,menuEditCopy,
menuEditPaste});
menuBar.Items.AddRange(new
ToolStripItem[]
{ menuFile,menuEdit});
this.Controls.Add(menuBar);
attachEvents();
}
private void attachEvents()
{menuFileExit.Click += processClick; }
private void processClick
(object o, EventArgs e)
{
if (o.Equals(menuFileExit))
Application.Exit();
}
private void frmMenuStrip_Load
(object sender, EventArgs e)
{
createMenu();
}
ContextMenuStrip

Drag & Drop


into the FORM
ImageScalingSize to
set Width, Height Icon

Click Items (Collection) to


add MenuItem
As the same
MenuStrip
1.Choose button
2.Set ContextMenuStrip
3.Attach event as the
same MenuStrip
ToolStripContainer & ToolStrip
ToolStripContainer
ToolStripContainer
Provides panels on each
side of the form and a
central panel that can hold
one or more controls
Drag & Drop
ToolStrip into
the Form
As the
same
MenuStrip
Add Items Collection for ToolBar
Attach Events
for each Item
Exercise:
(for Student)

Coding ContextMenuStrip
& ToolStrip at Runtime
END

You might also like