Bài giảng Lập trình C# 2010: Chương 2.2 trình bày các nội dung tiếp theo của chương 2 về DateTimePicker & MonthCalendar, các thao tác cơ bản, nút lệnh và tính năng của nó, cách thức thực hiện cùng một số nội dung khác.
Trang 3Name 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
Trang 4d The one- or two-digit day
dd The two-digit day Single-digit day values are preceded by a 0
ddd The three-character day-of-week abbreviation
dddd The full day-of-week name
h The one- or two-digit hour in 12-hour format
hh The two-digit hour in 12-hour format Single digit values are preceded
by a 0
H The one- or two-digit hour in 24-hour format
Trang 5HH The two-digit hour in 24-hour format Single digit values are preceded by a 0
m The one- or two-digit minute
mm The two-digit minute Single digit values are preceded by a 0
M The one- or two-digit month number
mm The two-digit minute Single digit values are preceded by a 0
M The one- or two-digit month number
MM The two-digit month number Single digit values are preceded by a 0
MMM The three-character month abbreviation
Trang 6MMMM The full month name
s The one- or two-digit seconds
ss The two-digit seconds Single digit values are preceded by a 0
t The one-letter A.M./P.M abbreviation (A.M is displayed as "A")
tt The two-letter A.M./P.M abbreviation (A.M is displayed as "AM")
y The one-digit year (2001 is displayed as "1")
yy The last two digits of the year (2001 is displayed as "01")
yyyy The full year (2001 is displayed as "2001")
Trang 7private void
frm ListBox_Load( object sender, EventArgs e)
{ listBox1.Item s.Clear();
for ( int i = 0; i < 10; i+ + ) listBox1.Item s.Add( "Item " + i);
}
ListBox
Trang 8A nd W e can use A ddR ange m ethod to add data:
private void frm ListBox_Load(object sender, EventArgs e)
{
string[] strArr = new string[] { "Tèo","Tí","Bin","Bo"};
listBox1.Item s.AddRange(strArr);
}
Trang 9{ private string m _strID ;
private string m _strN am e;
public C S tudent( string strID ,
string strN am e) { this m _strID = strID ;
this m _strN am e = strN am e;
}
public string ID
{ get { return this m _strID ; }
set { this m _strID = value ; }
}
public string N am e
{ get { return this m _strN am e; }
set { this m _strN am e = value ; }
}
}
Trang 10ArrayList arr = new ArrayList ();
for ( int i= 0;i< 10;i+ + )
{arr.Add( new CStudent ( "ID _" + i, "N am e " + i));
}
listBox1.D ataSource = arr;
listBox1.ValueM em ber = "ID " ;
listBox1.D isplayM em ber = "N am e" ;
A lso W e can use D ataSource to display data
Trang 11To get object from Listbox, W e could use coding below :
if (listBox1.SelectedItem != null )
{
CStudent svTeo = ( CStudent )
listBox1.SelectedItem ; lblM essage.Text = aStudent.N am e
+ " W as selected" ; }
Trang 13U se Item s to add data
private void frm CheckListBox_Load
(object sender, EventArgs e)
{ chklbLeft.Item s.Add("Tèo"); chklbLeft.Item s.Add("Tí");
chklbLeft.Item s.Add("Bin");
chklbLeft.Item s.Add("Bo");
}
O r w e could use A ddR ange
chklbLeft.Items.AddRange(new string[] { "Tèo","Tí","Bin","Bo"});
Trang 14H ow to process Item s C hecked ???
Trang 15H ow to process Item s C hecked ???
Trang 16H ow to process Item s C hecked ???
C ase 3:
for (int i = 0; i < chklbLeft.Item s.Count; i+ + ){
if (chklbLeft.G etItem C hecked(i))
{
//Process Item checked here
}
}
Trang 17G o back C hecklistB ox Exam ple:
private void btnAdd_Click
(object sender, EventArgs e)
foreach ( string s in chklbRight.Item s)
{chklbLeft.Item s.Rem ove(s);}
}
Trang 18private void btnAddAll_Click
( object sender, EventArgs e)
{ chklbRight.Item s.AddRange
(chklbLeft.Item s); chklbLeft.Item s.Clear();
}
Trang 19private void btnRem ove_Click
(object sender, EventArgs e)
{
foreach (string s in chklbRight.CheckedItem s)
chklbLeft.Item s.Add(s);
foreach(string s in chklbLeft.Item s)
chklbRight.Item s.Rem ove(s);
}
Trang 20private void btnRem oveAll_Click
( object sender, EventArgs e)
Trang 21Menu (2 ways to use)
Property
Trang 22I 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 !
Trang 23Coding to create Menu at Runtime
Trang 24private M enuItem m enuFile, m enuEdit, m enuFileN ew , m enuFileO pen,
m enuFileExit, m enuEditCut, m enuEditCopy, m enuEditPaste;
private void createM enu()
{
m ainM enuBar = new M ainM enu();
this.M enu = m ainM enuBar;
m enuFile= new M enuItem ("File");
m enuFileN ew = new M enuItem ("N ew ");
m enuFileO pen = new M enuItem ("O pen");
m enuFileExit = new M enuItem ("Exit");
m enuFile.M enuItem s.Add(m enuFileN ew );
m enuFile.M enuItem s.Add(m enuFileO pen);
Trang 25m enuFile.M enuItem s.Add("-");
m enuFile.M enuItem s.Add(m enuFileExit);
m ainM enuBar.M enuItem s.Add(m enuFile);
m enuEdit = new M enuItem ("Edit");
m enuEditCut = new M enuItem ("Cut");
m enuEditCopy = new M enuItem ("Copy");
m enuEditPaste = new M enuItem ("Paste");
m enuEdit.M enuItem s.AddRange(new M enuItem []
{ m enuEditCut,m enuEditCopy,m enuEditPaste});
m ainM enuBar.M enuItem s.Add(m enuEdit);
attachEvents();}
Trang 26private void attachEvents()
Trang 27(object sender, EventArgs e)
private void frm M enuB asic_Load
(object sender, EventArgs e)
{createM enu();
}
Trang 28MenuStrip
Trang 29Click on button to add MenuItem
Trang 30Text to Display
Click here to add Sub MenuItem
MenuItem’s Name
Trang 31Image Icon
Text to Display
Click here to add Sub MenuItem
MenuItem’s Name
Trang 32We could add MenuItem direct on the Windows Form
Enter Name in the “Type Here”
Trang 33Add Event for each
MenuItem
Trang 34Image List
Drag & Drop
Trang 35Gets the ImageList
ImageCollection for this image list See Next Slide
Gets or sets the size of the
images in the image list
image list
Trang 36Click Add button to insert
Image into Collection
Trang 37Select image & click Open
Trang 38At
Runtime
Trang 39private M enuStrip m enuBar;
private ToolStripM enuItem m enuFile, m enuEdit,
m enuFileN ew , m enuFileO pen, m enuFileExit,
m enuEditCut, m enuEditCopy, m enuEditPaste;
Trang 40{ m enuBar = new M enuStrip();
m enuBar.Font = new Font ( "arial" , 36, FontStyle.Bold,
G raphicsU nit Pixel);
this M ainM enuStrip = m enuBar;
m enuFile = new ToolStripM enuItem ( "File" );
m enuFileN ew = new ToolStripM enuItem ( "N ew " );
m enuFileN ew Im age = im ageList1.Im ages[0];
Trang 41ToolStripSeparator sp = new ToolStripSeparator ();
m enuFileExit = new ToolStripM enuItem ( "Exit" );
m enuFileExit.Im age = im ageList1.Im ages[1];
m enuFile.D ropD ow nItem s.Add(
m enuFileN ew );
m enuFile.D ropD ow nItem s.Add(
m enuFileO pen);
Trang 42m enuFile.D ropD ow nItem s.Add(sp);
m enuFile.D ropD ow nItem s.Add(
m enuFileExit);
m enuEdit = new ToolStripM enuItem ( "Edit" );
m enuEditCut = new ToolStripM enuItem ( "Cut" );
m enuEditCopy = new ToolStripM enuItem ( "Copy" );
m enuEditPaste = new ToolStripM enuItem ( "Paste" );
Trang 44private void frm M enuStrip_Load
( object sender, EventArgs e)
{
createM enu();
}
Trang 45Drag & Drop into the
FORM
Trang 46Click Items (Collection) to add MenuItem
ImageScalingSize to set Width, Height Icon
Trang 47As the same
MenuStrip
Trang 481.Choose button
2.Set ContextMenuStrip
3.Attach event as the same MenuStrip
Trang 49ToolStripContainer & ToolStrip
Trang 50ToolStripContainer
Trang 51ToolStripContainer Provides panels on each side of the form and a central panel that can
hold one or more controls
Trang 52Drag & Drop
ToolStrip into
the Form
Trang 53As the same
MenuStrip
Trang 54Add Items Collection for ToolBar
Trang 55Attach Events for each
Item
Trang 56(for Student)
Coding ContextMenuStrip & ToolStrip at
Runtime
Trang 57END