Chapter2-Windows Forms and Windows Forms Controls-Part 2
Chapter2-Windows Forms and Windows Forms Controls-Part 2
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.
Click on button to
add MenuItem
Click Add button to add new MenuItem
Text to Display
MenuItem’s Name
Image Icon
Text to Display
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
Gets the
ImageList.
ImageCollection
for this image list.
See Next Slide
Coding ContextMenuStrip
& ToolStrip at Runtime
END