You are on page 1of 20

TRNG CAO NG CNG THNG TP.

HCM
KHOA CNG NGH THNG TIN

WINDOWS FORMS APPLICATION


Microsoft
Visual C

THNH PH H CH MINH THNG 09/2017


Lp trnh Windows C#

CC COMMON CONTROL C BN
1. Label
Hin th mt chui vn bn tnh (ngi dng khng th thay i ni dung) ln form nhm m t, gii
thch cho cc control khc.

Properties

(Name): Tn label (xem nh tn bin)


AutoSize: Cho php t ng thay i kch thc label va vi chiu di text (khng wrap text)
Text: Ni dung hin th
TextAlign: Canh l text
Font: Cha cc properties v font, size, bold, italic, underline ca text
ForeColor: Mu text
BorderStyle: ng vin ca text (None, Fixed Single, Fixed3D)
Visible: Trng thi hin th (True Hin, False n)

2. LinkLabel
Hin th mt chui vn bn c cha siu lin kt (hyperlink)

Examples

label1
linklable1

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)


{
System.Diagnostics.Process.Start("http://google.com");
label1.Text = "You just click on the link!";
label1.ForeColor = Color.Red;
}

3. Button
Cho php ngi dng (user) click chut vo n v thc hin mt hnh ng no thng qua cc s kin.

Events

Click: Xy ra khi click chut vo button.


Click l s kin mc nh ca button, nn c th thm s kin ny bng cch double click vo button
(trong ch design).
private void button1_Click(object sender, EventArgs e)
{
// code x l s kin
}
Khi click vo button, chng trnh s thc hin phn code bn trong cp du { }

MouseHover: Xy ra khi r chut vo button


Microsoft
Visual C -2
Lp trnh Windows C#

Properties

(Name): Tn button (xem nh tn bin)


Text: Ni dung hin th trn button
TextAlign: Cn l text hin th trn button (TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter,
MiddleRight, BottomLeft, BottomCenter, BottomRight)
FlatStyle: Thay i kiu button (Flat bng phng, Standard, Popup, System)
BackgroundImage: nh nn cho button
Cursor: Hnh dng con tr chut khi r qua button (Default, Hand)
Enabled: Cho php click vo button (True) hoc khng cho php click (False)

Examples

tt
tt

tt

tt
h h

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("Xin cho!"); (*)
}

private void button2_Click(object sender, EventArgs e)


{
this.BackColor = Color.Blue;
}

private void button3_Click(object sender, EventArgs e)


{
this.BackColor = Color.Red;
}

private void button4_Click(object sender, EventArgs e)


{
this.BackColor = Color.Empty;
}

4. TextBox
Cho php ngi dng nhp vo mt chui vn bn (text).
Ngoi ra cn c th dng textbox xut vn bn (ngi dng c th thay i ni dung).

(*) Lp MessageBox hin mt thng bo hay mt hng dn cho user


Microsoft
Visual C -3
Lp trnh Windows C#

Properties

Text: Ni dung hin th trong textbox


Multiline: Cho php text m rng thnh nhiu dng (True) hoc ch hin th trn mt dng (False
mc nh)
PasswordChar: K t hin th cho nhp mt khu
Enabled: Cho php copy text v sa ni dung text (True) hoc khng cho php (False)
Methods

SelecAll(): Chn tt c text trong textbox


Clear(): Xa tt c text trong textbox
Events

TextChanged mc nh: Xy ra khi text trong textbox b thay i


Click: Xy ra khi click vo textbox
Examples

t
t R O t

t
t Ch

private void b_login_Click(object sender, EventArgs e)


{
if (this.tb_username.Text == "phucdeptrai" && this.tb_pass.Text == "phucdeptrai") this.Close();
else
{
MessageBox.Show( T hp hoc mt kh kh , "Thng bo", MessageBoxButtons.OKCancel,
MessageBoxIcon.Error);
this.tb_username.Focus();
}
}

private void tb_username_KeyPress(object sender, KeyPressEventArgs e)


{
if (e.KeyChar == 13) // phm ENTER
tb_pass.Focus();
}

private void tb_pass_KeyPress(object sender, KeyPressEventArgs e)


{
if (e.KeyChar == 13) b_login.Focus();
}
Microsoft
Visual C -4
Lp trnh Windows C#

private void b_remove_Click(object sender, EventArgs e)


{
this.tb_username.ResetText();
this.tb_pass.Text = "";
this.tb_username.Focus();
}

private void b_exit_Click(object sender, EventArgs e)


{
DialogResult dl = MessageBox.Show("Bn mun thot?", "Thng bo", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if (dl == DialogResult.OK) Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)


{
Form f = new Form2();
f.ShowDialog();
}

5. CheckBox
Cho php ngi dng chn mt item no . Trong mt nhm checkbox, c th chn nhiu item
cng lc.

Properties

Text: Ni dung text hin th bn cnh checkbox, nhm m t checkbox

BackColor: Mu nn checkbox
TextAlign: Cn l checkbox (TopLeft, TopCenter, TopRight, MiddleLeft text bn tri check box,
MiddleCenter text gia check box, MiddleRight text bn phi check box mc nh, BottomLeft,
BottomCenter, BottomRight)

Checked: Checkbox c chn (True) hoc khng chn checkbox (False)

Enabled: Cho php check (True) hoc khng cho php check (False)

CheckState: Trng thi ca checkbox: Chn (Checked), khng chn (Unchecked), khng tc dng
(Indeterminate trng thi ny ch c tc dng khi ThreeState = True)
ThreeState: Cho php chn 3 trng thi trn mt checkbox: Chn, khng chn, khng tc dng

Examples

h k
h k
h k

tt tt

Microsoft
Visual C -5
Lp trnh Windows C#

private void SetAllDisplay(CheckBox a, Label b)


{
if (a.CheckState == CheckState.Checked)
{
b.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
b.BackColor = System.Drawing.SystemColors.ActiveCaption;
}
else
{
b.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
b.BackColor = System.Drawing.SystemColors.Control;
}
}

private void cb1_CheckedChanged(object sender, EventArgs e)


{
if (cb1.CheckState == CheckState.Checked)
MessageBox.Show(cb1.Text + c chn!");
else if (cb1.CheckState == CheckState.Unchecked)
MessageBox.Show(cb1.Text + kh c chn");
SetAllDisplay(cb1, label1);
}

private void cb2_CheckedChanged(object sender, EventArgs e)


if (cb1.CheckState == CheckState.Checked)
MessageBox.Show(cb1.Text + c chn!");
else if (cb1.CheckState == CheckState.Unchecked)
MessageBox.Show(cb1.Text + kh c chn");
SetAllDisplay(cb2, label2);
}

private void cb3_CheckedChanged(object sender, EventArgs e)


{
if (cb1.CheckState == CheckState.Checked)
MessageBox.Show(cb1.Text + c chn!");
else if (cb1.CheckState == CheckState.Unchecked)
MessageBox.Show(cb1.Text + kh c chn");
SetAllDisplay(cb3, label3);
}

private void button1_Click(object sender, EventArgs e)


{
cb1.CheckState = CheckState.Checked;
cb2.CheckState = CheckState.Checked;
cb3.CheckState = CheckState.Checked;
}
private void button2_Click(object sender, EventArgs e)
{
cb1.CheckState = CheckState.Unchecked;
cb2.CheckState = CheckState.Unchecked;
cb3.CheckState = CheckState.Unchecked;
}

6. RadioButton
Cho php ngi dng ch c chn mt item duy nht trong cng mt nhm. Ngha l khi check
mt item th cc item cn li s trng thi unchecked.
Microsoft
Visual C -6
Lp trnh Windows C#

Examples

panel1 panel3
tt checkbox1
radiobutton2 checkbox2
radiobutton3 checkbox3
checkbox4

button1

private void Form1_Load(object sender, EventArgs e)


{
radiobutton1.Checked = true; //mc h tt c chn
}

private void button1_Click(object sender, EventArgs e)


{
string s = "Sex: ";
if (radiobutton1.Checked == true) s += radiobutton1.Text;
else if (radiobutton2.Checked == true) s += radiobutton2.Text;
else s += radiobutton3.Text;

s += "\nDo you have pets? ";


if (radiobutton4.Checked == true) s += radiobutton4.Text;
else s += radiobutton5.Text;

s += "\nWhich pets do you have? ";


if (checkbox1.Checked == true) s = s + checkbox1.Text + ", ";
if (checkbox2.Checked == true) s = s + checkbox2.Text + ", ";
if (checkbox3.Checked == true) s = s + checkbox3.Text + ", ";
if (checkbox4.Checked == true) s += checkbox4.Text;

MessageBox.Show(s);
}

7. ListBox
Hin th danh sch cc item theo tng dng v cho php ngi dng chn mt hoc nhiu item.

Nu nh s lng item ln hn s lng m danh sch c th hin th th mt thanh cun (scroll) s


c auto thm vo listbox.

Properties

Items: Danh sch cc item: Items[0], Items[1], Items[2]


Sorted: Sp xp cc item theo th t alphabet (True)
MultiColumn: Cho php chia thnh cc ct theo chiu ngang (True) hoc ch hin th mt ct (False
mc nh)
Microsoft
Visual C -7
Lp trnh Windows C#

SelectionMode: Ch chn (None khng cho php chn item, One ch cho php chn mt item,
MultiSimple Cho php chn nhiu item cng lc, MultiExtended ging MultiSimple, nhng c th dng
phm Shift hoc cc phm mi tn chn)
SelectedItem: Item ang c chn
SelectedItems: Danh sch cc item ang c chn

Methods

ClearSelected(): B chn tt c cc item


SetSelected(int32, boolean): Chn (True) hoc xa chn (False) cc item
FindString(String): Tm mt chui trong listbox

Examples

tb_add

b_add
lb_drinks

b_select

b_remove

b_selectall

b_removeall

lb_selected

private void b_select_Click(object sender, EventArgs e)


{
string selected_item = lb_drinks.SelectedItem.ToString();
MessageBox.Show(selected_item, "Selected items");
if (lb_selected.Items.Count >= 1)
{
for (int i = 0; i < lb_selected.Items.Count; i++)
if (selected_item == lb_selected.Items[i].ToString())
{
MessageBox.Show("Items have been selected!"); return;
}
}
lb_selected.Items.Add(selected_item);
}

private void b_selectall_Click(object sender, EventArgs e)


{
lb_selected.Items.Clear();
for (int i = 0; i < lb_drinks.Items.Count; i++)
lb_selected.Items.Add(lb_drinks.Items[i]); // hoc lb_selected.SetSelected(i, true);
}

private void b_removeall_Click(object sender, EventArgs e)


{
lb_selected.Items.Clear();
}
Microsoft
Visual C -8
Lp trnh Windows C#

private void b_remove_Click(object sender, EventArgs e)


{
try
{
if (lb_selected.Items.Count > 0)
{
string selected_item = lb_selected.SelectedItem.ToString();
MessageBox.Show(selected_item, "Xc nhn", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
lb_selected.Items.Remove(selected_item);
}
else
MessageBox.Show("Danh sch 2 rng!");
}
catch
{
}
}

private void b_add_Click(object sender, EventArgs e)


{
lb_drinks.Items.Add(tb_add.Text);
tb_add.Text = null;
tb_add.Focus();
}
}

8. CheckedListBox
Hin th danh sch cc checkbox v cho php ngi dng chn mt hoc nhiu checkbox.

Examples

b_add

tb_add t

clb_drinks
b_select
t
b_remove
b_removeall

private void b_add_Click(object sender, EventArgs e)


{
clb_drinks.Items.Add(tb_add.Text);
tb_add.Text = null;
tb_add.Focus();
}

private void b_select_Click(object sender, EventArgs e)


{
string selected = "";

Microsoft
Visual C -9
Lp trnh Windows C#

for (int i = 0; i < clb_drinks.Items.Count; i++)


if (clb_drinks.GetItemChecked(i) == true) selected += clb_drinks.Items[i].ToString() + ", ";
string s = selected.Substring(0, selected.Length - 2);
MessageBox.Show("You selected: " + s);
string selected_item = clb_drinks.SelectedItems.ToString();
for (int i = 0; i < clb_drinks.Items.Count - 1; i++)
if (clb_drinks.GetItemChecked(i) == true)
{
// Add item i to clb_selected
string t = clb_drinks.Items[i].ToString();
clb_selected.Items.Add(t);
// Remove item i at clb_drinks
clb_drinks.Items.Remove(i);
}
}

private void b_remove_Click(object sender, EventArgs e)


{
if (clb_selected.Items.Count > 0)
for (int i = 0; i < clb_selected.Items.Count; i++)
if (clb_selected.GetItemChecked(i) == true) clb_selected.Items.Remove(clb_selected.Items[i--].ToString());
else
MessageBox.Show("You have not selected item to remove!");
}

private void b_selectall_Click(object sender, EventArgs e)


{
clb_selected.Items.Clear();
for (int i = 0; i < clb_drinks.Items.Count; i++) clb_selected.Items.Add(clb_drinks.Items[i]);
}
private void b_removeall_Click(object sender, EventArgs e)
{
clb_selected.Items.Clear();
}

private void clb_drinks_ItemCheck(object sender, ItemCheckEventArgs e)


{
string item = clb_drinks.SelectedItem.ToString();
if (e.NewValue == CheckState.Checked) lb_selected.Items.Add(item);
else lb_selected.Items.Remove(item);
}

9. ComboBox
Hin th danh sch cc item dng drop down v cho php ngi dng chn mt item. Combo box
kh ging textbox, nhng c thm arrow bn cnh dng th xung danh sch.

Properties

MaxDropDownItems: S item ti a c hin th trong phn th xung


DropDownStyle: Kiu danh sch th xung (Simple , DropDown, DropDownList khng th nhp
vo combo box)
AutoCompleteMode: Ch auto complete (None khng c g xy ra khi nhp vo combo box. Nu
nhp ng mt item v nhn Enter th item c chn, Suggest khi nhp vo combo box s xut hin
cc gi , Append, SuggestAppend)

Microsoft
Visual C - 10
Lp trnh Windows C#

Examples

cob_name

tb_url

private void cob_name_SelectedIndexChanged(object sender, EventArgs e)


{
if (cob_name.SelectedItem.ToString() == "Zidean") tb_url.Text = "http://www.zidean.com/";
else if (cob_name.SelectedItem.ToString() == "Dohoavn") tb_url.Text = "http://dohoafx.com/";
else if (cob_name.SelectedItem.ToString() == "W3School") tb_url.Text = "https://www.w3schools.com/";
else if (cob_name.SelectedItem.ToString() == "Gliffy") tb_url.Text = "https://www.gliffy.com/";
else if (cob_name.SelectedItem.ToString() == "Deviantart") tb_url.Text = "http://www.deviantart.com/";
else if (cob_name.SelectedItem.ToString() == "Aviary") tb_url.Text = "https://www.aviary.com/";
else if (cob_name.SelectedItem.ToString() == "Logopond") tb_url.Text = "https://logopond.com/";
else if (cob_name.SelectedItem.ToString() == "Find icon") tb_url.Text = "http://findicons.com/";
else if (cob_name.SelectedItem.ToString() == "Sumopaint") tb_url.Text = "http://www.sumopaint.com/app/";
else if (cob_name.SelectedItem.ToString() == "Vit designer") tb_url.Text = "http://vietdesigner.net/";
else if (cob_name.SelectedItem.ToString() == "Google webfonts") tb_url.Text = "https://fonts.google.com/";
else if (cob_name.SelectedItem.ToString() == "iStock") tb_url.Text = "http://www.istockphoto.com/";
else if (cob_name.SelectedItem.ToString() == "Shutter stock") tb_url.Text = "https://www.shutterstock.com/";
else if (cob_name.SelectedItem.ToString() == "Koding") tb_url.Text = "http://www.koding.com/";
else tb_url.Text = "http://rgb.vn/";
}

10. ListView
Hin th danh sch cc item vi cc icon bng bn ch xem.

Thm icon vo list view

S dng ImageList control n, to danh sch hnh nh mun lm icon cho list view.

Properties

Items: Danh sch cc item

Ch h h h h t
kh t ImageList

Thm sub item


(khi h D t

Thm item
Xa item

Microsoft
Visual C - 11
Lp trnh Windows C#

View: Ch xem (LargeIcon, Details, SmallIcon, List, Title)


MultiSelect: Cho php chn nhu item
Columns: Thm collumns vo list view (khi ch xem l Details)
Lv_info.View = View.Details;

ch_fullname.Text = "Full name";


ch_fullname.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center;
ch_fullname.Width = 75;

Thm column

LargeImageList v SmallImageList: ImageList s dng khi hin th cc item (khi ch xem l


LargeIcon v SmallIcon)
Checkboxes: Hin th checkbox bn cnh mi item (True) hoc khng hin th (False)
GirdLine: Hin th ng xung quanh cc item v sub item (khi ch Details)

Examples

tb_name
tb_id tb_sex

lw_info

dtp_dob

cb_eng
cb_vie b_add
rb_smallicon cb_other
rb_largeicon
rb_list rb_details
rb_tile

private void rb_largeicon_CheckedChanged(object sender, EventArgs e)


{
lv_info.View = View.LargeIcon;
}

private void rd_smallicon_CheckedChanged(object sender, EventArgs e)


{
lv_info.View = View.SmallIcon;
}

private void rb_list_CheckedChanged(object sender, EventArgs e)


{
lv_info.View = View.List;
}
Microsoft
Visual C - 12
Lp trnh Windows C#

private void rb_tile_CheckedChanged(object sender, EventArgs e)


{
lv_info.View = View.Tile;
}

private void rb_details_CheckedChanged(object sender, EventArgs e)


{
lv_info.View = View.Details;
}

private void b_add_Click(object sender, EventArgs e)


{
string lag = "";
if (cb_vie.Checked == true) lag = lag + cb_eng.Text.ToString() + ", ";
else if (cb_eng.Checked == true) lag += cb_eng.Text.ToString() + ", ";
else lag = lag + cb_other.Text.ToString();
ListViewItem lv_item = new ListViewItem(new String[] { tb_name.Text, tb_id.Text, tb_sex.Text, dtp_dob.Text, lag }, -1);
lv_info.Items.Add(lv_item);
}

11. TreeView
Hin th h thng phn cp cc node, mi node c th cha cc node khc (node con). Ging nh
cc folder c hin th ca s bn phi ca Windows Explorer.

Properties

Nodes: Danh sch cc node

SelectedNode: Node ang c chn


Methods

ExpandAll(): M rng tt c cc node


CollapsesAll(): Thu gn tt c cc node
GetNodeCount(Boolean): S lng node (True bao gm c subtree, False khng bao gm subtree)
Microsoft
Visual C - 13
Lp trnh Windows C#

Events

AfterCheck: Xy ra sau khi check box ca node c check


AfterCollapse: Xy ra sau khi node c thu nh
AfterSelect: Xy ra sau khi node c chn

Examples

p_print

lb_print

tv_bookmarks

private void tv_bookmarks_AfterSelect(object sender, TreeViewEventArgs e)


{
MessageBox.Show(e.Node.Text);
}

private void PrintRecursive(TreeNode treenode)


{
lb_print.Items.Add(treenode.Text);
foreach (TreeNode tn in treenode.Nodes)
PrintRecursive(tn);
}

private void button1_Click(object sender, EventArgs e)


{
PrintRecursive(tv_bookmarks.SelectedNode);
}

12. NumericUpDown
Cho php chn mt s (nguyn hoc thc) trong mt min gii hn thng qua cc nt ln xung.

Properties

Maximum v Minimum: Gi tr ti a v ti thiu c th chn


Increment: Gi tr tng hoc gim (bc nhy) khi cc nt ln, xung c click
DecimalPlaces: S ch s thp phn
Value: Gi tr gn cho NumericUpDown

Methods

DownButton(): Gim gi tr ca NumericUpDown


UpButton(): Tng gi tr ca NumericUpDown
Events

ValueChanged: Xy ra khi value thay i

Microsoft
Visual C - 14
Lp trnh Windows C#

Examples

nud_hours
nud_minutes
nud_seconds

private void Form1_Load(object sender, EventArgs e)


{
nud_hours.Value = DateTime.Now.Hour;
nud_minutes.Value = DateTime.Now.Minute;
nud_seconds.Value = DateTime.Now.Second;
}

private void nud_hours_ValueChanged(object sender, EventArgs e)


{
MessageBox.Show("You changed nud_hours!");
}

private void nud_minutes_ValueChanged(object sender, EventArgs e)


{
MessageBox.Show("You changed nud_minutes!");
}

private void nud_seconds_ValueChanged(object sender, EventArgs e)


{
MessageBox.Show("You changed nud_seconds!");
}

13. PictureBox

Cho php hin th mt hnh nh. Picturebox h tr nhiu nh dng nh: BMP, GIF, JPEG, EXIF, PNG, TIFF,
ICO... (cc nh dng c GDI+ h tr)

Properties

SizeMode: Cch thc hnh nh hin th (Normal, StretchSize cng nh va vi PictureBox, AutoSize
thit lp size ca PictureBox bng vi size ca nh, CenterImage center nh vo gia PictureBox, Zoom
cng nh nhng gi nguyn t l nh)
ImageLocation: ng dn hoc URL ca nh

Example

tb_image

b_load

pb_image

private void b_load_Click(object sender, EventArgs e)


Microsoft
Visual C - 15
Lp trnh Windows C#

{
pb_image.ImageLocation = tb_image.Text;
pb_image.SizeMode = PictureBoxSizeMode.Zoom;
}

14. RichTextBox
Cho php ngi dng nhp vo mt chui vn bn (text) v chnh sa vi nhiu nh dng. C th
nhp trc tip hoc ti ln t mt text file (.doc, .txt, .rtf)

Properties

AcceptsTab: Cho php s dng phm tab


MaxLength: S k t ti a ngi dng c th nhp hoc paste vo
ZoomFactor: zoom
SelectionFont, SelectionColor, SelectionBackColor, SelectionAlignment: Font ch, mu ch, mu
nn, canh l ca text ang c chn
SelectionLength: S k t ca text ang c chn
CanUndo: Cho php ngi dng c th hon tc (undo) cc hot ng trc Ctrl Z (True) hoc
khng cho php (False)
Methods

LoadFile(String): Load .rtf file hoc standard ASCII text file vo RichTextBox
SaveFile(String): Save ni dung trong RichTextBox vo .rtf file
Select(): T khi t v tr bt u n v tr kt thc
Undo(): Hon tc hot ng chnh sa cui cng
Redo(): Reapply li hot ng cui cng c hon tc
Cut(), Copy(), Paste()
Example

cb_font

cb_size

rbt_text

private void Form1_Load(object sender, EventArgs e)


{
for (int i = 1; i < 100; i++) cb_size.Items.Add(i.ToString());
cb_size.SelectedIndex = 9;

System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();


foreach (FontFamily f in fonts.Families)
cb_font.Items.Add(f.Name.ToString());
cb_font.SelectedItem = "Calibri";
}

Microsoft
Visual C - 16
Lp trnh Windows C#

private void Format()


{
int fsize = 10;
if (cb_size.SelectedIndex != -1) fsize = (int)int.Parse(cb_size.SelectedItem.ToString());
string fname = "Calibri";
if (cb_font.SelectedIndex != -1) fname = cb_font.SelectedItem.ToString();
Font f = new Font(new FontFamily(fname), fsize);
rtb_text.Font = f;
}

15. MaskedTextBox
Cho php ngi dng nhp vo mt chui vn bn (text) bng cch s dng mt mt n (mask)
hng dn nhp.

MaskedTextBox khng h tr Multiline v Undo.

Example

mtb_dob

private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)


{
if (mtb_dob.MaskFull)
try
{
DateTime.ParseExact(mtb_dob.Text, "dd/mm/yyyy", null);
}
catch
{
MessageBox.Show("Value entered is invalid! Ahuhu!");
mtb_dob.ResetText();
}
}

16. DateTimePicker
Cho php ngi dng chn mt ngy, thi gian v hin th vi mt nh dng c th.

Example

dtp_form

dtp_to

Microsoft
Visual C - 17
Lp trnh Windows C#

int Distance(int year, int month, int day)


{
if (month < 3)
{
year--;
month += 12;
}
return 365 * year + year / 4 - year / 100 + year / 400 + (153 * month - 457) / 5 + day - 306;
}

private void b_distance_Click(object sender, EventArgs e)


{
int s = Distance(dtp_form.Value.Year, dtp_form.Value.Month, dtp_form.Value.Day) - Distance(dtp_to.Value.Year,
dtp_to.Value.Month, dtp_to.Value.Day);
MessageBox.Show(s.ToString() + " days");

17. MonthCalendar
Cho php ngi dng chn mt s ngy bng cch s dng lch.

Example

mc_difference

b_difference

int Distance(int year, int month, int day)


{
if (month < 3)
{
year--;
month += 12;
}
return 365 * year + year / 4 - year / 100 + year / 400 + (153 * month - 457) / 5 + day - 306;
}

private void d_difference_Click_1(object sender, EventArgs e)


{
int s1 = Distance(mc_difference.SelectionEnd.Year, mc_difference.SelectionEnd.Month,
mc_difference.SelectionEnd.Day) - Distance(mc_difference.SelectionStart.Year, mc_difference.SelectionStart.Month,
mc_difference.SelectionStart.Day);
string s2 = "Form: " + mc_difference.SelectionStart + "\nTo: " + mc_difference.SelectionEnd + "\n" + s1.ToString() + "
days.";
MessageBox.Show(s2);
}

Microsoft
Visual C - 18
Lp trnh Windows C#

18. ProgressBar
Cho php hin th tin trnh hot ng ca chng trnh.

Properties

Step: Bc nhy
Maximum, Minimum: Gi tr ln nht v nh nht
Value: V tr hin ti

Example

l_load
pg_load

b_load

private void b_load_Click(object sender, EventArgs e)


{
if(!timer1.Enabled)
{
b_load.Text = "Stop";
timer1.Start();
}
else
{
b_load.Text = "Start";
timer1.Stop();
}
}

private void timer1_Tick(object sender, EventArgs e)


{
pg_load.PerformStep();
l_load.Text = "Loading: " + pg_load.Value.ToString() + "%";
}

19. NotifyIcon
To ra mt icon trn System Tray

Properties

Icon: Icon hin ti


BalloonTipIcon: Icon hin th trn cc balloon tip
BalloonTipText: Text hin th trn cc balloon tip
ContextMenuStrip: Menu la chn khi right click vo icon
Text: Text hin th khi con tr chut nm trn mt khu vc NotifyIcon

Example

Microsoft
Visual C - 19
Lp trnh Windows C#

tb_hello
b_hello

private void b_hello_Click(object sender, EventArgs e)


{
ni_hello.ShowBalloonTip(5000, "Hello", tb_hello.Text, ToolTipIcon.Info);
}

Microsoft
Visual C - 20

You might also like