You are on page 1of 42

Chng 2 Cc iu kin c bn Ni dung

Gii thiu Form Label TextBox Button Cc thuc tnh chung Cc s kin chung iu khin s kin bn phm iu khin s kin chut Cc iu khin c bn khc

Gii thiu Mt s iu kin c bn


Form: i tng ca s ca chng trnh cha cc i tng khc. Label: i tng dng hin th vn bn v hnh nh (ngi dng khng sa c). TextBox: i tng dng hin th v nhp d liu t bn phm. Button: L nt n cho php Click n thc hin mt chc nng. CheckBox: i tng cho php chn hoc khng chn. ListBox: i tng cho php xem v chn d liu t cc dng. ComboBox: i tng cho php chn d liu t cc dng. GroupBox: i tng cha cc i tng khc. Panel: i tng cha cc i tng khc.

Form
Dng to giao din cho chng trnh Thm mt Form mi
Chn Project ->Add Windows Form Chn Windows Form -> g tn Form -> Add

Trang 1

Cc iu khin ca Form
L cc thnh phn ho nh Label, TextBox,... Mi iu khin to ra cc i tng cng lp Cc i tng c cc thuc tnh, cc s kin v cc phng thc ring. Properties: Cc thuc tnh m t i tng. Methods: Cc phng thc thc hin cc chc nng ca i tng. Events: Cc s kin sinh ra bi s chuyn ng ca bn phm v con chut, chi tit do ngi lp trnh vit.

Cc thuc tnh thng dng


AcceptButton: Nt c click khi n phm Enter CancelButton: Nt c click khi n phm Esc BackgroundImage: nh nn ca Form Font: Font hin th ca Form v Font ngm nh ca cc i tng ca Form. FormBorderStyle: Kiu ng vin ca Form None: Form khng c ng vin Fix...: C nh kch thc khi chy Form Sizeable: C th thay i kch thc Form ForeColor: Mu ch ca Form v mu ch ca cc i tng ca Form. Text: Dng vn bn hin th trn tiu Form MaximizeBox: C/khng nt phng to MinimizeBox: C/khng nt thu nh StartPosition: V tr bt u khi chy Form CenterScreen: Nm gia mn hnh WindowState: Xc nh trng thi ban u Form Close: ng Form v gii phng cc ti nguyn. Form ng ko th m li. Hide: n Form v khng gii phng ti nguyn ca Form. Show: Hin th mt Form n. Load: Xy ra khi chy Form (ngm nh khi nhy p chut trong ch thit k). FormClosing: Xy ra khi ng Form.

V d t cc thuc tnh ca Form


To mt Form mi G dng tiu ca Form t Form ch FixDialog Ct nt phng to Ct nt thu nh Cho Form nm gia mn hnh Chn mt nh lm nn ca Form Chy Form

Trang 2

Label
i tng hin th vn bn kt hp hnh nh, Ko sa c vn bn hin th Cc thuc tnh thng dng AutoSize: T thay i kch thc ca i tng Fonts: Font ch ca i tng Label ForeColor: Mu ch ca i tng Image: nh ca i tng Text: Vn bn xut hin trn i tng. TextAlign: L ca vn bn.

TextBox
i tng dng nhp d liu t bn phm

Cc thuc tnh thng dng


Enabled: C/khng cho php thao tc i tng Multiline: C/khng cho php nhp d liu nhiu dng (mc nh l Ko) PasswordChar: Nhp k t lm mt khu ReadOnly: C/khng cho php sa d liu ca i tng (mc nh l c) Text: Vn bn nhp (hin th) ca i tng.

Cc s kin thng dng


TextChanged: Xy ra khi nhp hoc xo cc k t (ngm nh khi nhy p chut trong ch thit k) KeyDown: Xy ra khi n mt phm bt k trn i tng. KeyUp: Xy ra khi th mt phm n trn i tng. Ch : D liu nhp vo TextBox l vn bn do nu thc hin cc php ton s hc, logic th cn chuyn sang kiu s.

Button
i tng nt n cho php thc hin mt chc nng C th hin th hnh nh kt hp vi vn bn

Cc thuc tnh thng dng


Text: Vn bn hin th trn i tng Image: Hnh nh hin th trn i tng

Cc s kin thng dng


Click: Xy ra khi nhn con tr chut hoc g Enter trn i tng (ngm nh khi nhy p chut trong ch thit k).

V d
Gii phng trnh bc nht
Nhp h s a, h s b Chn nt <Gii> Kim tra d liu nhp? Nu khng phi l s? Chuyn thnh s a, b ? Nu a =0 Nu b = 0 -> V s nghim Nu b != 0 -> V nghim Nu a !=0 -> x = -b/a

Trang 3

Chuyn d liu t xu k t sang s


Chuyn khng kim tra d liu
S dng hm Convert: <num> = Convert.ToDouble(str) double a = Convert.ToDouble(123.45);

Chuyn c kim tra d liu


S dng hm TryParse ca kiu d liu: bool IsNumber = double.TryParse(str, out num) IsNumber = true nu i c IsNumber = false nu khng i c

Chuyn d liu t s sang xu


S dng hm Convert <str> = Convert.ToString(num) string st = Convert.ToString(123); S dng hm ToString() ca i tng <obj>.Tostring() int a =2; string s = a.ToString();

Hm Message
Dng hin th mt thng bo Dng xc nhn mt hnh ng

//hien thi thong bao dang ham 7 co 4 tham so MessageBox.Show("Gi tr nhp vo khng phi l s !", "Thng bo...", MessageBoxButtons.OK, MessageBoxIcon.Information); //Xac nhan hanh dong dang ham 7 hoac dang ham 9 DialogResult myDialog; myDialog = MessageBox.Show("Chc chn xo d liu khng ?", "Thng bo...", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (myDialog == DialogResult.No) return;

Cc thuc tnh chung


Cc thuc tnh thng dng
BackColor: Mu nn ca i tng. BackgroundImage: nh nn ca i tng Cursor: Kiu con tr chut khi a con tr chut vo i tng Enabled: C/khng cho php thao tc vi i tng Font: Font ch ca i tng ForeColor: Mu ch ca i tng
Trang 4

TabIndex: Th t n phm Tab chuyn con tr n i tng, Chn biu tng Text: Dng vn bn hin th trn i tng TextAlign: L ca dng vn bn hin th trn i tng Visible: n/hin i tng Anchor: Neo i tng so vi cc cnh ca i tng cha Dock: C nh i tng trong i tng cha Location: V tr ca i tng so vi i tng cha

V d v thuc tnh Anchor v Dock

Focus: Chuyn con tr n i tng Hide: n i tng Show: Hin th i tng n

Cc s kin chung
S kin ca Form
KeyDown KeyUp KeyPress MouseDown MouseUp

Cc s kin thng dng


KeyDown: Xy ra khi mt phm c n trn i tng. KeyUp: Xy ra khi mt phm c th trn i tng KeyEventArg: Tham s cho s kin KeyDown v KeyUp. KeyPress: Xy ra khi n v th mt phm trn i tng. KeyPressEventArg: Tham s cho s kin KeyPress KeyPressEventArg: L tham s ca s kin KeyPress KeyChar: Tr v k t ca phm c n Alt: C/khng phm Alt c n

Trang 5

Control: C/khng phm Control c n. KeyEventArgs: L tham s ca cc s kin KeyDown v KeyUp. Shift: C hay khng phm Shift c n. KeyCode: Tr v phm c n. KeyValue: Tr v m ca phm c n

V d
Xy dng Form cho php g phm Enter hoc cc phm mi tn di chuyn con tr gia cc TextBox
private void textBox2_KeyDown(object sender, { if ((e.KeyValue == 13) || (e.KeyValue == 40)) textBox3.Focus(); if (e.KeyValue == 38) textBox1.Focus(); } KeyEventArgs e)

Cc s kin chut
Cc s kin thng dng
MouseEnter: Xy ra khi a con tr chut vo vng ca i tng. MouseLeave: Xy ra khi a con tr chut ra khi vng ca i tng. MouseDown: Xy ra khi n nt chut trong khi con tr chut ang nm trong vng ca i tng. MouseUp: Xy ra khi th nt chut trong khi con tr chut ang nm trong vng ca i tng. MouseMove: Xy ra khi di chuyn con tr chut trong vng ca i tng. MouseEventArg: L tham s ca cc s kin MouseUp, MouseDown, v MouseMove Button: Nt chut n (left, right, middle or none). Clicks: S ln nt chut c n. X: To tng i x ca con tr chut. Y: To tng i y ca con tr chut.

Bi tp: Vit chng trnh hin th to Vit CT nhp vo 3 cnh ca 1 tam gic, tnh din tch v chu vi.

GroupBox
i tng dng cha cc i tng khc Mi i tng c tiu Thuc tnh thng dng
Text: Vn bn hin th trn tieu GroupBox

Trang 6

Panel
i tng dng cha cc i tng khc Mi i tng khng tiu Thuc tnh thng dng
BorderStyle: ng vin ca i tng (ngm nh l None)

CheckBox
i tng cho php chn/khng chn gi tr, cho php chn ng thi nhiu i tng Cc thuc tnh thng dng
Checked: C/khng i tng c chn Text: Vn bn hin th trn i tng

Cc s kin thng dng


CheckedChanged: Xy ra khi chn/khng chn i tng (ngm nh khi nhy p chut trong ch thit k)

RadioButton
Cho php chn/ko chn gi tr, cho php chn 1 i tng mt thi im.
chn nhiu i tng phi t cc iu khin trong GroupBox hoc Panel

Cc thuc tnh thng dng


Checked: C/ko i tng c chn Text: Vn bn hin th trn i tng

Cc s kin thng dng


Click: Xy ra khi i tng c click. CheckedChanged: Xy ra khi chn/ khng chn i tng (ngm nh khi nhy p chut trong ch thit k).

PictureBox
i tng dng hin th hnh nh (Bitmap, GIF, JPEG, Metafile, Icon) Cc thuc tnh thng dng
Image: nh hin th trong PictureBox. SizeMode: Ch hin th nh

Trang 7

Normal:t nh gc trn bn tri ca i tng CenterImage: t nh gia i tng StretchImage: Thay i kch thc nh ng vi kch thc i tng in PictureBox. AutoSize: Thay i kch thc i tng PictureBox ng vi kch thc nh.

Trang 8

Chng 3 Cc iu kin nng cao Ni dung


1 2 3 4 5 6 7 8 9 1 2
3 4

ListBox CheckedListBox ComboBox TabControl Menu Toolbar MDI Windows TreeView ListView Cho php xem v chn cc dng d liu Cc thuc tnh thng dng
Items: Mng cc dng trong ListBox. Items[0] = Cat Items[1] = Mouse MultiColumn: C/khng chia ListBox thnh nhiu ct. SelectedIndex: Tr v dng hin thi c chn Nu chn nhiu dng th tr v 1 gi tr tu ca cc dng c chn. Nu khng chn th tr v gi tr -1. SelectedIndices: Tr v mt mng cc ch s ca cc dng c chn. SelectedItem: Tr v gi tr dng c chn. SelectedItems: Tr v mt mng gi tr cc dng c chn. Sorted: C/Khng sp xp d liu trong ListBox. Ngm nh l False. SelectionMode: Xc nh s lng dng c chn ca ListBox. one: Mt dng Multi: Nhiu dng GetSelected(index): Tr v True nu dng Index c chn, ngc li tr Add: Thm mt dng vo ListBox listBox1.Items.Add(Cat); listtBox1.Items.Add(Mouse); RemoveAt(row):Xo dng v tr row listBox1.Items.RemoveAt(row); Clear: Xo tt c cc dng listBox1.Items.Clear();

ListBox

5
6 1 2 3 4 5 6 7 8 9 10 1

vfalse.
2 3 4 5 6 7 8

S kin thng dng

Trang 9

SelectedIndexChanged: Xy ra khi chn mt dng. Ngm nh khi nhy p thit k.

ch
3

V d
Xy dng Form private voidbtnAdd_Click(object sender, EventArgs e) { listBox1.Items.Add(txtInput.Text); txtInput.Clear(); } private voidbtnRemove_Click(object sender, EventArgs e) { int row=listBox1.SelectedIndex; if (row != -1) listBox1.Items.RemoveAt(row); } private voidbtnClear_Click(object sender, EventArgs e) { listBox1.Items.Clear(); }
1

Bi tp
1 Xy dng Form cho php di chuyn cc dng gia 2 ListBox

1 2 3 4

CheckedListBoxe
5 1

L s m rng ca ListBox bng cch thm CheckBox pha bn tri mi dng ->C th chn cc dng

Cc thuc tnh thng dng


CheckedItems: Mng cc gi tr ca dng c nh du Check. 2 CheckedIndices: Mng cc ch s dng c nh du Check.

Phng thc thng dng


GetItemChecked(index): Tr v true nu dng c chn.
3

Trang 10

S kin thng dng


4

ItemCheck: Xy ra khi dng c checked hoc unchecked.

V d
1 Xy dng Form khi nh du check th dng c a sang ListBox bn phi, khi b du Check th xo dng trong ListBox bn phi 2 private void myCheckedListBox_Load(objectsender,EventArgs e) { checkedListBox1.Items.Add(C++ HTP); } private void checkedListBox1_ItemCheck(objectsender, ItemCheckEventArgse) { stringitem = checkedListBox1.SelectedItem.ToString(); if( e.NewValue == CheckState.Checked) { listBox1.Items.Add(item); } else { listBox1.Items.Remove(item); } }

Bi tp
1 Xy dng form cho php nh du v chuyn cc dng gia 2 CheckedListBox S kin cho nt Add ca CheckedListBox private void btnAdd_Click(objectsender, EventArgse) { for(inti = 0; i < checkedListBox1.Items.Count -1; i++) { if(checkedListBox1.GetItemChecked(i) == true) { // Adds Item i to checkedListBox2 stringitem = checkedListBox1.Items[i].ToString(); checkedListBox2.Items.Add(item); // Removes Item i at checkedListBox1 checkedListBox1.Items.RemoveAt(i); } } }

Trang 11

ComboBox
L s kt hp ca TextBox v ListBox

Cc thuc tnh thng dng


1 2 3 4

DropDownStyle: Xc nh kiu ca ComboBox. Simple: Chn hocg gi tr DropDown(ngm nh): Chn hocg gi tr DropDownList: Ch cho php chn gi tr.

1 2 3 4 5 1 2 3 4 5 6 7

Items: Mng cc dng trong ComboBox SelectedIndex: Ch s dng c chn. Nu khng chn c gi tr -1. SelectedItem: Gi tr dng c chn. Sorted: C/Khng sp xp d liu trong ComboBox. Ngm nh lfalse.

S kin thng dng


SelectedIndexChanged: Xy ra khi chn 1 dng.

Cc phng thc thng dng


Add: Thm mt dng vo ComboBox comboBox1.Items.Add(Cat); comboBox1.Items.Add(Mouse); RemoveAt(row): Xo dng v tr row comboBox1.Items.RemoveAt(row); Clear: Xo tt c cc dng trong ComboBox comboBox1.Items.Clear();

V d
1 Xy dng Form ly Font ca h thng private voidComboBox_Load(objectsender, EventArgse) { FontFamily[] ff = FontFamily.Families; for(inti = 0; i <ff.Length; i++) cboFont.Items.Add(ff[i].Name); for(int i = 8; i <= 72; i++)

Trang 12

cboSize.Items.Add(i); } private void cboFont_SelectedIndexChanged(objectsender,EventArgse) { if(cboFont.Text.Trim() == "") return; lbDisplay.Font = new Font(cboFont.Text,lbDisplay.Font.Size); } private void cboSize_SelectedIndexChanged(objectsender,EventArgse) { if(cboSize.Text.Trim() == "") return; lbDisplay.Font =newFont(lbDisplay.Font.Name, (float)Convert.ToDouble(cboSize.Text)); }

Menu
1 2 3 4 5 6 1 1
2 3 4

5
6 7

8
9 1 2 3 4 5

Dng nhm cc lnh cng nhau Menu c th cha Menu ngang Menu dc Menu con Cc biu tng Cc phm nng Cc ng phn cch . Xy dng menu Ko biu tng MenuStrip vo Form G cc dng cho menu t tn cho cc dng ca menu Chn ca s Properties v t thuc tnh Name Tn menu t bng tin t mnu (v d: mnuFile, mnuEdit) Chn hnh nh cho cc dng ca menu Nhy chut phi v chn Set Image Chn Local Resource -> Import ->chn hnh nh t phm nng cho cc dng ca menu Chn ca s Properties v t thuc tnh ShortCutKey Cc thuc tnh thng dng Name: Tn menu c dng trong m lnh. Checked: C/Khng dng menu xut hin checked. Ngm nh l false. ShortCutKey: t phm nng cho menu ShowShortcut: C/Khng phm nng hin th trn dng menu. Ngm nh l true. Text:Xut hin trn dng menu. S kin thng dng
Trang 13

Click: Xy ra khi mt dng ca menu c click chut hoc n phm nng. Ngm nh khi nhy p chut trong ch thit k. Vit lnh cho dng ca menu gi mt Form 2 <Tn lp> <Tn form> = new <Tn lp>; 3 <Tn Form>.Show(); 4 <Ten Form>.ShowDialog();
1

V d: Thit k menu

Menu ng cnh
1 2 3
4

Menu hin th khi nhy chut phi trn i tng ca Form hoc trn Form. Thit k menu ng cnh Ko iu khin MenuContext vo form Thit k cc dng menu ng cnh ging nh thit k menu Hin th menu ng cnh Chn i tng hoc Form t thuc tnh ContextMenuStripca i tng c chn l tn ca menu ng cnh.

ToolBar
ToolBar dng cha cc biu tng ca cc chc nng thng c s dng trong chng trnh. Xy dng ToolBar Ko iu khin ToolStripvo Form Nhy chut vo biu tng phi v chn i tng to ToolBar Button: Nt n DropDownButton: Nt s xung
Trang 14

1
2 3

4
5 6

7
8 9

Separator: ng phn cch Xy dng ToolBar t tn cho cc nt ca ToolBar Chn ca s Properties v t thuc tnh Name Tn TooBar t bng tin t tb (v d: tbNew, tbOpen) Chn hnh nh cho i tng ca ToolBar Nhy chut phi v chn Set Image Chn Local Resource -> Import -> chn hnh nh Vit m lnh Gi t menu: <tn menu>.PerformClick() Gi trc tip i tng

MDI Windows
Mt ng dng MDI cho php ngi dng thao tc vi nhiu ca s mt thi im. 1 SDI v MDI Forms

V d MDI Parent v MDI Child

Thuc tnh MDI Parent ca Form IsMdiContainer: C/Khng mt Form l form MDI Parent. Ngm nh l False.

Trang 15

2 3 4 5 1 2 3 4 5

ActiveMdiChild: Tr v Form Child ang c kch hot. Thuc tnh MDI Child IsMdiChild: C/Khng Form l mt MDI child (thuc tnh read-only). MdiParent: Chi ra mt MDI parent ca Form <Tn form>.MdiParent = this Phng thc thng dng LayoutMdi: Xc nh kiu hin th ca Form con trong MDI Form. ArrangeIcons: Sp xp cc biu tng di MDI Cascade: Sp xp cc ca s chng nhau TileHorizontal: Sp xp ca s theo chiu ngang TileVertical: Sp xp ca s theo chiu dc Cc kiu sp xp

Bi tp
Xy dng mt h son tho vn bn theo dng MDI Form.

Trang 16

TreeView
Hin th thng tin theo cc nt (node) Cc nt cha c cc nt con Nt u tin gi l nt gc n du [+] m nt n du [-] thu gn nt Mi nt c hnh nh km theo Cc thuc tnh thng dng 1 CheckBoxes: C/khng xut hin cc checkbox trn cc node. Mc nh l False. 2 Checked: C/khng mt Node c check (thuc tnhCheckBoxesphi c t lTrue) 3 ImageList: Ch ra danh sch nh hin th trn cc node. 4 ImageList l mt mng cc i tng nh. 5 To danh sch nh ImageList bng cch ko iu kin vo Form, nhy chut 6 phi v chn Choose Image thm cc nh vo ImageList. 1 Nodes: Mng cc TreeNodes trong TreeView. 2 Nodes.Add: B sung mt node vo cy. 3 Nodes.Clear: Xo ton b cc node trn cy. 4 Nodes.Remove: Xo mt node trn cy v cc node con ca n. 5 SelectedNode: Node hin thi c chn 6 FullPath: Ch ra ng dn n node bt u t node gc.

Trang 17

SelectedImageIndex: Ch ra ch s nh c hin th trn node khi node c

chn. ImageIndex: Ch ra ch s nh c hin th trn node khi node khng c chn (deselected). 3 Text: Text hin th ca Node. 4 FirstNode: Node con u tin ca node. 5 LastNode: Nodecon cui cng ca node. 6 PrevNode: Node con trc node con hin thi. 7 NextNode: Nodecon tip theo node hin thi. Cc phng thc thng dng 1 Collapse: Thu nh cc node con canode. 2 Expand: M rng cc node con ca node. 3 ExpandAll: M rng tt c cc node con. 4 GetNodeCount: Tr v s lng node con. Cc s kin thng dng 5 AfterSelect: Xy ra khi mt node c chn (ngm nh khi nhy p chut ch thit k). 6 BeforeExpand: Xy ra khi m rng mt node
2

1 2 3 4 5

6
1 7

V d t tn cc i tng treeView1 txtInput comboBox1 btnAddRoot btnAddChild btnDelete Khai bo trong lp: private TreeNodecurrentNode; Thit lp thuc tnh ImageCollection cho i tng imageList1 Cc s kin: private voidAddTreeView_Load(object sender, EventArgs e) { treeView1.ImageList = imageList1; comboBox1.Items.Add("Image1"); comboBox1.Items.Add("Image2"); comboBox1.SelectedIndex = 0; } private voidbtnAddRoot_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() == "") return; TreeNode childNode = new TreeNode(); childNode.Text = txtInput.Text;
Trang 18

childNode.ImageIndex = comboBox1.SelectedIndex; treeView1.Nodes.Add(childNode); } private voidtreeView1_AfterSelect(object sender,TreeViewEventArgs e) { currentNode = e.Node; } private voidbtnAddChild_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() == "") return; TreeNode childNode = new TreeNode(); childNode.Text = txtInput.Text; childNode.ImageIndex = comboBox1.SelectedIndex; currentNode.Nodes.Add(childNode); currentNode.ExpandAll(); } private voidbtnDelete_Click(object sender, EventArgs e) { currentNode.Remove(); }

ListView
1 2
3

Dng hin th d liu theo cc dng v cc ct C th chn mt hoc nhiu dng C th hin th cc biu tng theo cc dng V d ListView hin th danh sch th mc TP v cc tp

1 2 3 4 5 6 7 8
9 10 11

Cc thuc tnh thng dng CheckBoxes: C/ko cc checkbox trn cc dng d liu (ngm nh l False) Columns: Cc ct hin th trong ch Details. FullRowSelect: ch nh rng mi SubItems c c hightlighted cng vi Item c chn hay khng.

khi
12

Trang 19

13 1 2 3 4 5 1 2 3 4

GridLines: Hin th li (ch hin th trong ch Details). Items: Mng cc dng (ListViewItems) trong ListView. LargeImageList: Danh sch nh (ImageList) hin th trn ListView. SmallImageList: Danh sch nh (ImageList)hin th trn ListView. MultiSelect: C/Khng cho php chn nhiu dng (ngm nh l True). SelectedItems: Mng cc dng c chn. View: Kiu hin th ca ListView Icons: Hin th danh sch theo cc biu tng List: Hin th danh sch theo mtct Details: Hin th ListView theo danh sch nhiu ct

1 2 3 Cc phng thc thng dng 4 Add: Thm mt dng vo ListView 5 Clear: Xo tt c cc dng ca ListView 6 Remove: Xo mt dng trong ListView 7 RemoveAt(index): Xo mt dng v tr index S kin thng dng 8 ItemSelectionChanged: Xy ra khi chn mt dng.

V d
1 Thit k form nhp, sa, xo d liu

Trang 20

t tn cc i tng 1 txtId, txtFirstName, txtLastName, txtAddress, btnNew, btnEdit, btnDelete, btnSave, btnCancel. t thuc tnh cho listView1 2 Columns: Thm 4 ct 3 FullRowSelect: true 4 GridLines: true 5 MultiSelect: false 6 View: Details Vit Code cho cc s kin 6 private boolmodeNew; 7 private introw;//Ham dat dieu khien trang thai cac textbox va cac nut 8 private void SetControls(booledit) 9 { 10 txtId.Enabled = false; 11 txtFirstName.Enabled = edit; 12 txtLastName.Enabled = edit; 13 txtAddress.Enabled = edit; 14 btnNew.Enabled = !edit; 15 btnEdit.Enabled = !edit; 16 btnDelete.Enabled = !edit; 17 btnSave.Enabled = edit; 18 btnCancel.Enabled = edit; 19 } 20 private void frmListView_Load(objectsender, EventArgse) 21 { 22 SetControls(false); 23 } 24 private void btnNew_Click(objectsender, EventArgse) 25 { 26 modeNew = true; 27 SetControls(true); 28 row = listView1.Items.Count; 29 txtId.Text = Convert.ToString(row + 1); 30 txtFirstName.Clear(); 31 txtLastName.Clear(); 32 txtAddress.Clear(); 33 txtFirstName.Focus();

Trang 21

34 35 36
37 38 39

40 41 42
43 44 45 46 47 48 49 50 51 52 53 54 55 56

57 58 59
60

61 62 63
64 65 66

67
68 69

} private voidbtnEdit_Click(objectsender, EventArgse) { modeNew = false; SetControls(true); txtFirstName.Focus(); } private voidbtnSave_Click(objectsender, EventArgse) { if(modeNew) { listView1.Items.Add(txtId.Text); listView1.Items[row].SubItems.Add(txtFirstName.Text); listView1.Items[row].SubItems.Add(txtLastName.Text); listView1.Items[row].SubItems.Add(txtAddress.Text); } else { listView1.Items[row].SubItems[1].Text = txtFirstName.Text; listView1.Items[row].SubItems[2].Text = txtLastName.Text; listView1.Items[row].SubItems[3].Text = txtAddress.Text; } SetControls(false); } private voidbtnCancel_Click(objectsender, EventArgse) { SetControls(false); } private voidbtnDelete_Click(objectsender, EventArgse) { try{ listView1.Items.RemoveAt(row); }catch(Exception){} } private void listView1_ItemSelectionChanged( objectsender,ListViewItemSelectionChangedEventArgse { row = e.ItemIndex; txtId.Text = listView1.Items[row].SubItems[0].Text; txtFirstName.Text = listView1.Items[row].SubItems[1].Text; txtLastName.Text= listView1.Items[row].SubItems[2].Text; txtAddress.Text= listView1.Items[row].SubItems[3].Text;
Trang 22

) 70
71 72 73 74 75

76 77 78
79

80

} private voidbtnClose_Click(objectsender, EventArgse) { this.Close(); }

Bi tp tng hp
Xy dng chng trnh Explore hin th thng tin v tp

M ngun gi : private void frmExplore_Load(objectsender, EventArgse) {//Lay o dia cua may DriveInfo[] drives = DriveInfo.GetDrives(); for(inti = 0; i < drives.Length; i++) { TreeNodemyNode = newTreeNode(); myNode.Text = drives[i].Name; myNode.ImageIndex = 0; myNode.SelectedImageIndex = 0; treeView1.Nodes.Add(myNode); LoadTreeView(myNode.Text, myNode); } } public void LoadTreeView(stringdirValue, TreeNodeparentNode) { string[] dirArray = Directory.GetDirectories(dirValue); if(dirArray.Length != 0) { foreach(stringdirectory in dirArray) {
Trang 23

DirectoryInfocurDirectory = newDirectoryInfo(directory); TreeNode myNode = new TreeNode(curDirectory.Name); parentNode.Nodes.Add(myNode); // recursively populate every subdirectory LoadTreeView(directory, myNode); } } } public voidLoadFilesInDirectory(stringcurDirectory) { listView1.Items.Clear(); DirectoryInfonewDirectory = newDirectoryInfo(curDirectory); // put files and directories into arrays DirectoryInfo[] dirArray = newDirectory.GetDirectories(); FileInfo[] fileArray = newDirectory.GetFiles(); foreach(DirectoryInfodir in dirArray) { ListViewItem newDirectoryItem = listView1.Items.Add(dir.Name); newDirectoryItem.SubItems.Add(""); newDirectoryItem.SubItems.Add("Folder"); newDirectoryItem.SubItems.Add(dir.LastWriteTime.ToString()); newDirectoryItem.ImageIndex = 0; } foreach(FileInfofile in fileArray) { ListViewItem newFileItem = listView1.Items.Add(file.Name); newFileItem.SubItems.Add(file.Length.ToString()); newFileItem.SubItems.Add("File"); newFileItem.SubItems.Add(file.LastWriteTime.ToString()); newFileItem.ImageIndex = 1; } } private void treeView1_AfterSelect(objectsender, TreeViewEventArgse) { stringcurrentDirectory = e.Node.FullPath; LoadFilesInDirectory(currentDirectory); } private voidmnuList_Click(object sender, EventArgse) { listView1.View = View.List; } private voidmnuDetails_Click(objectsender, EventArgse) {
Trang 24

listView1.View = View.Details; } private voidtbUp_Click(objectsender, EventArgse) { stringcurrentDirectory;currentDirectory = treeView1.SelectedNode.Parent.FullPath; LoadFilesInDirectory(currentDirectory); }

TabControl
To ra cc ca s Tab Mi ca s Tab gi l mt TabPage TabPages c th cha cc iu khin Thm v xo cc TabPage

Bi Tp 1

Chng X l li Ni dung
1 2

4.
t vn X l li
Trang 25

3 Lnh try ... catch 4 V d

Gii thiu
Mt li ngoi l (exception) l li khng mong i xy ra khi chng trnh thc hin. Li ngoi l xy ra do 1 Cc li do lp trnh khng tt 2 Cc lnh gi th vin 3 Ti nguyn khng khi thc hin .NET Framework xy dng lp Exception cho php s dng x l cc li ngoi l

Lp Exception
1 2
3 4 5 6

Lp Exception l lp c s cho php cc lp khc c th k tha OleDbException SqlException Mt s thuc tnh Message: L do xy ra li StackTrace: Ni xy ra li V tr dng lnh Th tc

Lnh try ... catch


Dng x l li ngoi l try{ 1. Cc lnhc th xy ra li } catch (Exception ){ 2. Hin th li } finally{ 3.(Tu chn) m lnh lun c thc hin.
}

V d:
1 V d v php chia

Trang 26

M code: private void btnDivide_Click(object sender, EventArgs e) { textBox3.Clear(); try{ int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int c = a / b; textBox3.Text = c.ToString(); } catch(Exception ex){ MessageBox.Show(ex.Message); } }

Trang 27

Chng 5. ADO.NET
Ni dung
1 Cc khi nim 2 Cc i tng ca ADO.NET 3 Xy dng lp truy nhp d liu

I. Cc khi nim
ADO.NET l cng ngh truy nhp d liu c cu trc Cung cp giao din hng i tng hp nht (Uniform object oriented) cho cc d liu khc nhau 1 - C s d liu quan h 2 - XML 3 - Cc d liu khc c thit k cho cc ng dng phn tn v Web ADO.NET= ActiveX Data Objects Cc i tng ADO.NETcha trong khng gian tnSystem.Data. Cc i tng ADO.NET chia thnh 2 loi 1 - Connected: Cc i tng truyn thng trc tip vi c s d liu. 2 - Disconnected: Cc i tng khng truyn thng trc tip vi c s d liu.

1 2 3 4 5 6 1 2 3

Cc i tng DataSet: Mt tp DataTable trong b nh DataTable: Mt bng d liu trong b nh DataRow: Mt bn ghi trong DataTable DataColumn: Mt ct d liu trong DataTable DataRelation: t quan h ca 2 DataTable DataViewManager: To Views ca DataSet C th nh x mt bng vt l vi DataTable Mt DataTable l mt mng 2 chiu gm cc dng v cc ct
Trang 28

DataTable

4
5 6 7 8

Mt s thuc tnh - Columns: Cc ct d liu ca DataTable + Count: S ct trong DataTable - Rows: Cc dng d liu ca DataTable + Count: S dng trong DataTable C th to mt DataTable trong b nh DataTablemyTable= new DataTable(); myTable.Columns.Add(MaKhoa, typeof(string)); myTable.Columns.Add(TenKhoa, typeof(string)); ADO.NET t chc thnh m hnh i tng System.Data System.Data.OleDb System.Data.Common System.Data.SqlClient System.Data.SqlTypes

1 2 3

System.Data:Cc lp ca ADO.NET System.Data.OleDb:Cc lp lm vic vi d liu OLEDB System.Data.SqlClient:Cc lp lm vic vi c s d liu SQL Server

II. Cc i tng
ADO.NET Data Providers 1 L cc lp truy nhp d liu ngun: MicrosoftSQL Server2000, SQL Server 7, Oracle, Microsoft Access 2 Thit lp kt ni gia DataSetsv d liu ngun 3 C 2 th vin ADO.NET Data Providers 4 System.Data.OleDb: Dng truy nhp c s d liu OLE 5 System.Data.SqlClient: Truy nhp SQL Server SqlClient OleDb SqlCommand OleDbCommand SqlConnection OleDbConnection SqlDataReader OleDbDataReader SqlDataAdapter OleDbDataAdapter i tng Connection 1 Biu din kt ni ti c s d liu //Ket noi toi co so du lieu MS Access string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=<DataName>"; OleDbConnection myConnection= newOleDbConnection(conStr); myConnection.Open();
Trang 29

//Ket noi toi co so du lieu SQL Server String conStr = DataSource = <Computer Name>;+Persist Security Info = true; +Initial Catalog = <DataName>; +User Id =name;Password=psw; +Connect Timeout = <seconds>; SqlConnection myConnection= newSqlConnection(conStr); myConnection.Open(); i tng DataAdapter 1 Dng ly d liu t d liu ngun vo DataSet 2 Dng cp nht d liu t DataSet vo d liu ngun 3 OleDbDataAdapter lm vic vi CSDL MS Access 4 SqlDataAdapterlm vic vi d liu SQL Server
1

V d phng thc Fill ly d liu vo DataTable: sString conStr =Data Source = may01;" +Initial Catalog = QLSV; +Persist Security Info = true; +User Id =sa; Password=sa; Connect Timeout =50 ; //Ket noi toi co so du lieu SqlConnection myConnection = new SqlConnection(conStr); myConnection.Open(); string sqlStr= SELECT * FROM tblKhoaDaoTao; SqlDataAdapter myDataAdapter= newSqlDataAdapter(sqlStr,myConnection); DataSet myDataSet= newDataSet(); myDataAdapter.Fill(myDataSet,tblKhoaDaoTao); DataTable myTable=myDataSet.Tables[tblKhoaDaoTao]; 1 V d xy dng form hin th d liu

Trang 30

M code: private stringconStr = "Data Source = (local);" +"Initial Catalog = QLSinhVien;" +"persist security info = true;" +"User Id=sa; Password=sa; Connect Timeout =50"; private SqlConnection myConnection; private SqlDataAdapter myDataAdapter; privateDataSet myDataSet; privateDataTable myTable; private intpos = 0; private void frmDataTable_Load(objectsender, EventArgse) { myConnection = new SqlConnection(conStr); myConnection.Open(); string SqlStr = "SELECT * FROM tblKhoaDaoTao"; myDataAdapter = newSqlDataAdapter(SqlStr, conStr); myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet, "tblKhoaDaoTao"); myTable = myDataSet.Tables["tblKhoaDaoTao"]; btnFirst.PerformClick(); } private void btnFirst_Click(objectsender, EventArgse) { if(myTable.Rows.Count == 0) return; pos = 0; txtMaKhoa.Text = myTable.Rows[pos]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[pos]["TenKhoa"].ToString(); } private void btnPrevious_Click(objectsender, EventArgse) { if(myTable.Rows.Count == 0) return; pos--; if (pos < 0) pos = 0; txtMaKhoa.Text = myTable.Rows[pos]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[pos]["TenKhoa"].ToString(); } private void btnNext_Click(objectsender, EventArgse) { if(myTable.Rows.Count == 0) return; pos++; if (pos > myTable.Rows.Count -1) pos = myTable.Rows.Count -1; txtMaKhoa.Text = myTable.Rows[pos]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[pos]["TenKhoa"].ToString(); } private void btnLast_Click(objectsender, EventArgse)
Trang 31

{ if(myTable.Rows.Count == 0) return; pos = myTable.Rows.Count -1; txtMaKhoa.Text = myTable.Rows[pos]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[pos]["TenKhoa"].ToString(); } i tng DataGridView Dng hin th d liu t 1 DataTable Cch thc hin Thm i tng DataGridView vo Form Nhy chut phi v chn Add column hoc Editcolumns Ln lt chn Add thm cc ct Mi ct cn khai bo cc thuc tnh Name: Tn ct dng trong m lnh Header text: Tiu hin th ca ct DataPropertyName: Tn ct d liu ca DataTable. DataSource: Tn DataTable cn hin th ln li AutoGenerateColumns: T ng ly cc ct nu bng true, ngc li ly ng s ct khai bo. dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = myTable; AllowUserToAddRows: Cho/khng thm dng trn li AllowUserToDeleteRows: Cho/khng xo dng trn li S kin thng dng RowEnter: Xy ra khi con tr a vo mt dng e.RowIndex: Dng hin thi e.ColumnIndex: Ct hin thi V d: Hin th d liu trong bng tblKhoaDaoTao ln li, khi chuyn con tr trn li d liu hin th ln TextBox.

Trang 32

private string conStr = "Data Source = (local);" +"Initial Catalog = QLSinhVien;" +"persist security info = true;" +"User Id=sa; Password=sa; Connect Timeout =50"; private SqlDataAdapter myDataAdapter; private DataSet myDataSet; private DataTable myTable; private void frmDataGridView_Load(object sender, EventArgs e) { string SqlStr = "SELECT * FROM tblKhoaDaoTao"; myDataAdapter = new SqlDataAdapter(SqlStr, conStr); myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet,"tblKhoaDaoTao"); myTable = myDataSet.Tables["tblKhoaDaoTao"]; //Chuyen len luoi dataGridView1.DataSource = myTable; dataGridView1.AutoGenerateColumns = false; } private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { try { int row = e.RowIndex; txtMaKhoa.Text = myTable.Rows[row]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[row]["TenKhoa"].ToString(); } catch(Exception e){} } i tng SqlCommand
Dng thc hin cu lnh SQL: Insert; Update; Delete

Khai bo bin

Trang 33

1
2 3

private stringconStr =Data Source = ...;; private SqlConnection myConnection; private SqlCommand myCommand;

M kt ni 4 myConnection = newSqlConnection(conStr); 5 myConnection.Open(); Thc hin cu lnh SQL 6 myCommand = newSqlCommand(sqlStr,myConnection); 7 myCommand.ExecuteNonQuery();
Ch : sqlStr l cu lnh SQL

Thit k Form cho php nhp, xo bng d liu tblKhoaDaoTao

private string conStr = "Data Source = (local);" +"Initial Catalog = QLSinhVien;" +"persist security info = true;" +"User Id=sa; Password=sa; Connect Timeout =50"; private SqlDataAdapter myDataAdapter; private SqlCommand myCommand; private SqlConnection myConnection; private DataSet myDataSet; private DataTable myTable; public frmSqlCommand() { InitializeComponent(); } private void SetControls(bool edit) { txtMaKhoa.Enabled = edit; txtTenKhoa.Enabled = edit; btnAdd.Enabled = !edit;

Trang 34

btnSave.Enabled = edit; } private void Display() { string SqlStr = "SELECT * FROM tblKhoaDaoTao"; myDataAdapter = new SqlDataAdapter(SqlStr, myConnection); myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet, "tblKhoaDaoTao"); myTable = myDataSet.Tables["tblKhoaDaoTao"];//Chuyen len luoi dataGridView1.DataSource = myTable; dataGridView1.AutoGenerateColumns = false; } private void frmSqlCommand_Load(object sender, EventArgs e) {//Mo ket noi myConnection = new SqlConnection(conStr); myConnection.Open(); Display(); SetControls(false); } private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { try{ int row = e.RowIndex; txtMaKhoa.Text = myTable.Rows[row]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[row]["TenKhoa"].ToString(); } catch(Exception){} } private void btnAdd_Click(object sender, EventArgs e) { txtMaKhoa.Clear(); txtTenKhoa.Clear(); SetControls(true); txtMaKhoa.Focus(); } private void btnSave_Click(object sender, EventArgs e) { string sSql = "Insert Into tblKhoaDaoTao (MaKhoa, TenKhoa)"+"Values (N'"+ txtMaKhoa.Text + "',N'" + txtTenKhoa.Text + "')"; myCommand = new SqlCommand(sSql, myConnection); myCommand.ExecuteNonQuery(); Display(); SetControls(false);
Trang 35

} private void btnDelete_Click(object sender, EventArgs e) { string sSql = "Delete From tblKhoaDaoTao " +"Where MaKhoa = N'" + txtMaKhoa.Text + "'"; myCommand = new SqlCommand(sSql, myConnection); myCommand.ExecuteNonQuery(); Display(); } i tng SqlCommandBuilder
T ng thc hin Update, Insert, Delete

Khai bo cc bin 1 private string conStr = Data Source = ;; 2 private SqlConnection myConnection; 3 private SqlDataAdapter myDataAdapter; 4 private SqlCommandBuilder myCommandBuilder; 5 private DataSet myDataSet; 6 private DataTable myTable; 7 private string sqlStr; To kt ni ti c s d liu 1 myConnection = new SqlConnection(conStr); 2 //To mt SqlDataAdapter 3 myDataAdapter = new SqlDataAdapter(sqlStr, myConnection); 4 //To mt SqlCommandBuilder 5 myCommandBuilder = newSqlCommandBuilder(myDataAdapter); 6 //To mt DataTable

Trang 36

7 8 9

myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet,....); myTable = myDataSet.Tables[....];

Xo mt dng 1 myTable.Rows[pos].Delete(); //pos l dng cn xo 2 myDataAdapter.Update(myTable); 3 //Thm mt dng 4 DataRow newRow = myTable.NewRow(); 5 newRow["MAKHOA"] = txtMakhoa.Text; 6 newRow["TENKHOA"] = txtTen.Text; 7 myTable.Rows.Add(newRow); 8 myDataAdapter.Update(myTable); Sa mt dng 1 DataRow editRow =myTable.Rows[pos]; 2 editRow["MAKHOA"] = txtMakhoa.Text; 3 editRow["TENKHOA"] = txtTenkhoa.Text; 4 myDataAdapter.Update(myTable); 5 //Loi b sa i dng 6 myTable.RejectChanges(); //pos l dng cn sa

Thit k Form cho php nhp, xo bng d liu tblKhoaDaoTao

private string conStr = "Data Source = (local);" +"Initial Catalog = QLSinhVien;" +"persist security info = true;" +"User Id=sa; Password=sa; Connect Timeout =50"; private SqlDataAdapter myDataAdapter; private SqlCommandBuilder myCommandBuilder; private SqlConnection myConnection; private DataSet myDataSet; private DataTable myTable;

Trang 37

private void SetControls(bool edit) { txtMaKhoa.Enabled = edit; txtTenKhoa.Enabled = edit; btnAdd.Enabled = !edit; btnSave.Enabled = edit; } private void Display() { string SqlStr = "SELECT * FROM tblKhoaDaoTao"; myDataAdapter = new SqlDataAdapter(SqlStr, myConnection); myCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet, "tblKhoaDaoTao"); myTable = myDataSet.Tables["tblKhoaDaoTao"]; dataGridView1.DataSource = myTable; dataGridView1.AutoGenerateColumns = false; } private void frmCommandBuilder_Load(object sender, EventArgs e) { myConnection = new SqlConnection(conStr); myConnection.Open(); Display(); SetControls(false); } private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { try { int row = e.RowIndex; txtMaKhoa.Text = myTable.Rows[row]["MaKhoa"].ToString(); txtTenKhoa.Text = myTable.Rows[row]["TenKhoa"].ToString(); } catch (Exception){} } private void btnAdd_Click(object sender, EventArgs e) { txtMaKhoa.Clear(); txtTenKhoa.Clear(); SetControls(true); txtMaKhoa.Focus(); } private void btnSave_Click(object sender, EventArgs e)
Trang 38

{ DataRow newRow = myTable.NewRow(); newRow["MaKhoa"] = txtMaKhoa.Text; newRow["TenKhoa"] = txtTenKhoa.Text; myTable.Rows.Add(newRow); myDataAdapter.Update(myTable); SetControls(false); } private void btnDelete_Click(object sender, EventArgs e) { int pos = dataGridView1.CurrentRow.Index; myTable.Rows[pos].Delete(); } private void btnClose_Click(object sender, EventArgs e){this.Close();} Bi tp: Thit k form

i tng ListBox v ComboBox


ListBoxes: Cho php ngi dng xem v chn cc dng d liu t danh sch ComboBox: S kt hp ca TextBoxv LixtBox DataSource: Ngun d liu, l mt DataTable DisplayMember: Ct hin th trong ListBox ValueMember: Ct gi tr tr v khi chn ListBox SelectedIndex: Dng hin thi c chn SelectedValue: Gi tr c chn trn ListBox

V d hin th d liu trong tblKhoaDaoTao if(myTable.Rows.Count > 0) {


Trang 39

comboBox1.DataSource = myTable; comboBox1.DisplayMember = TenKhoa"; comboBox1.ValueMember = MaKhoa"; comboBox1.SelectedIndex = 0; } Gi tr tr v khi chn l: comboBox1.SelectValue

III. Lp truy nhp c s d liu


Mi form khng ph thuc vo c s d liu Ch kt ni d liu mt ln khi chy ng dng Mi form s dng cc i tng lp Ly d liu vo DataTable Cp nht d liu t DataTabe vo c s d liu Thc hin cc cu lnh SQL thao tc vi d liu M ngun: private static SqlConnection myConnection; private SqlDataAdapter myDataAdapter; public DataServices(){} ~DataServices(){} public bool OpenDB(string myComputer,string myDatabase,string myUser,string myPass) { string strConnection = "Data Source='" + myComputer + "';" +"Initial Catalog='" + myDatabase + "';"+"Persist Security Info=True;" +"User ID='" + myUser + "';Password='" +myPass + "';" +"Connect Timeout =120"; if ((myConnection != null) && (myConnection.State == ConnectionState.Open)) return true; try { myConnection = new SqlConnection(strConnection); myConnection.Open(); } catch (SqlException ex) { DisplayError(ex); myConnection = null; return false; } return true; } public void CloseDB( ) { if (myConnection != null) { if (myConnection.State == ConnectionState.Open) { try { myConnection.Close(); } catch (Exception){} myConnection = null;

Trang 40

} } } private void DisplayError(SqlException ex){ string message; switch (ex.Number) { case 17: message = "Server khng ng !"; break; case 4060: message = "C s d liu khng ng !"; break; case 18456: message = "Khng ng tn truy nhp v mt khu !"; break; case 547: message = "Vi phm kho ngoi !"; break; case 2627: case 2601: message = "Trng gi tr trng kho"; break; case 8152: message = "D liu nhp qu di"; break; default: message = ex.Message; break; } MessageBox.Show(message,"Error " + ex.Number.ToString()); } public DataTable RunQuery(string QueryString, string TableName){ DataSet myDataSet = new DataSet(); myDataAdapter = new SqlDataAdapter(); try{ myDataAdapter.SelectCommand = new SqlCommand(QueryString, myConnection); SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter); if (TableName.Trim().Length > 0) myDataAdapter.Fill(myDataSet, TableName); else myDataAdapter.Fill(myDataSet); } catch (SqlException ex) {
Trang 41

DisplayError(ex); return null; } return myDataSet.Tables[TableName]; } public void Update(DataTable myDataTable) { try{ myDataAdapter.Update(myDataTable); } catch (SqlException ex){ DisplayError(ex); } } public void ExecuteNonQuery(string cmdText) { SqlCommand myCommand = new SqlCommand(cmdText, myConnection); try{ myCommand.ExecuteNonQuery();} catch (SqlException ex){ DisplayError(ex); } }
Truy xut n cc iu khin ca form khc trong c# PHP Code:

private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.textBox1.Text = this.textBox1.Text; frm.Show(); } Foreach(DataRow row in table.Rows) listbox1.Items.Add(row["tn ct"].ToString())//hoc Index ca ct mun Add vo Listbox foreach(xxxxx) listBox1.Items.Add (ds.Tables[0].Rows[i]["MaSV"].ToString()) ListboxItem[] items = new ListboxItem[ds.Tables[0].Rows.Count]; int nItems = ds.Tables[0].Rows.Count; for (int i=0;i<nItems;i++) { items[i] = new ListboxItem(ds.Tables[0].Rows[i]["MSV"].Value); } this.ListBox.AddRange(items);

Trang 42

You might also like