Các Bài Tập Microsoft .NET

174 191 0
Các Bài Tập Microsoft .NET

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 Các t p Microsoft NET M CL C Bài Microsoft NET Framework Bài Visual Studio.NET .13 Bài Nh ng khác bi t gi a VB.NET v i VB6 37 Bài Nh ng ch c n ng iT ng m i c a VB.NET (ph n I) 59 Bài Nh ng ch c n ng iT ng m i c a VB.NET (ph n II) 68 Bài Nh ng ch c n ng iT ng m i c a VB.NET (ph n III) .82 Bài Nh ng ch c n ng iT ng m i c a VB.NET (ph n IV) .95 Bài Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n I) 112 Bài Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n II) 124 Bài 10 Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n III) 134 Bài 11 Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n IV) 144 Bài 12 Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n V) 161 Các t p Microsoft NET Bài Microsoft NET Framework Cài đ t Visual Studio.NET Beta Visual Studio.NET n m CDs, g m có files: • EN_VS_NET_BETA2_WINCUP.exe • EN_VS_NET_BETA2_CD1.exe • EN_VS_NET_BETA2_CD2.exe • EN_VS_NET_BETA2_CD3.exe Ch y EN_VS_NET_BETA2_WINCUP.exe đ Unzip vào m t folder m t harddisk (eg: E:\CD\WINCUP) L nl t ch y EN_VS_NET_BETA2_CD1.exe, EN_VS_NET_BETA2_CD2.exe EN_VS_NET_BETA2_CD3.exe đ Unzip chúng vào chung m t folder m t harddisk (eg: E:\CD ) Vi c Unzip files có th r t lâu, b n ph i kiên nh n ch cho đ n k t thúc N u không, cài đ t có th b than phi n thi u files K đó, tr c h t ch y Setup.exe c a Windows Component Update nh d i đây, click No Warning dialog hi n ra: Ti p theo, ch y Setup.exe c a Visual Studio.NET: B n nên có CPU Pentium III, 500MHz tr lên, v i 256 MB RAM nh t 10GB Harddisk V OS b n nên dùng Windows 2000 (Professional Các t p Microsoft NET hay Server) hay Windows XP Lý versions Windows h tr Unicode có Internet Information Server (IIS) h tr ASPX đ ta dùng cho ASP.NET M Samples c a QuickStart Tr c expand Samples c a Quickstart b n c n ph i cài đ t IIS N u ch a làm vi c y b n b CD c a Windows2000 hay WindowsXP vào đ install IIS component QuickStart Samples c a NET Framework ch a gi i thích c n b n nhi u thí d expand Samples doubleclick Webpage Starthere.htm nh hình d i đây: Khi trang Web c a QuickStart hi n ra, click QuickStart, tutorials and samples r i sau theo ch d n t ng b c Trang Framework SDK QuickStart Tutorials cho ta t p c a ASP.NET, Windows Forms How Do I Nh đ nguyên folders c a Unzipped files (E:\CD, E:\CD\WINCUP), đ ng delete chúng, NET s dùng chúng Ngoài ra, n u sau NET b corrupted conflict v i application software khác, b n có th cài đ t NET l i Gi i h n c a Software Tools hi n gi Các t p Microsoft NET Architect c a application software hi n gi có nói chung ba t ng (three tiers): t ng giao di n (Presentation Tier), t ng gi a (Middle Tier) t ng d ki n (Data Tier): Presentation Tier: Trong desktop Client ta dùng VB6 n i v i middle tier qua DCOM Trong browser based Client ta dùng Javascript hay Java applet T browser based Client ta dùng http đ n i v i middle tier qua IIS/ASP (có th dùng COM đây) Middle Tier: Ch a rules đ validate data client business rules khác Ta dùng VB6 đây, nh ng cách tri n khai COM v i nh ng Object Oriented Programming concepts r c r i h n bình th ng Ta ph i thi t k cho components scale well (dùng cho m i c ) Có dùng Microsoft Transaction Server Windows NT hay COM+ Services Windows 2000 L p ráp versions c a components m t thách th c l n Nhi u middle tier nói chuy n v i database qua HTTP, ADO CDO (Collaborative Data Objects), v.v Data Tier: Th ng relational database nh Microsoft SQL Server hay Oracle Ngoài có Exchange hay database x a c a mainframe Do ta th y: • Desktop tools không thích h p cho Distributed System hay Internet Ph i dùng nhi u th codes nh VB6 Code, VBScript, JavaScript, Dynamic HTML, Cascading Style Sheet, VC++, Stored Procedures (Transact-SQL SQLServer hay PL-SQL Oracle) Tùy thu c nhi u vào central database: Ngay c ADO dù ti n d ng c ng đòi h i Client luôn connect v i Server i u không thích Các t p Microsoft NET h p cho Internet applications, b n ch t c a Internet stateless (không gi tr ng thái), m i l n c n làm vi c m i connect l i m t chút • DLL "Hell": Các ActiveX c n ph i đ c registered v i Windows Registry, ch có m t version đ c ch p nh n N u version m i nh t c a m t DLL không compatible v i versions tr c mà applications máy c n có r c r i Gi i h n c a VB6 • Thi u kh n ng inheritance (th a k ) m t s Object Oriented features khác • Kh n ng Error handling gi i h n: On Error Goto , On Error Resume Next Các t p Microsoft NET • Nhi u c n ph i g i Windows API đ làm nh ng vi c VB6 không h tr : vi c không t nhiên nguy hi m • Không có multi-threading: không th đoán tr c response c a code ch y windows c a m t VB6 application Ngay c gi i quy t v n đ multitasking b ng Timers c ng không đáng tin c y • Không d dùng chung v i ngôn ng VC++ • Không ti n cho Web development: WebClass không thành công l m Ít ch u cho ta cài ActiveX máy c ah khác nh NET Framework NET đ c developed t đ u n m 1998, lúc đ u có tên Next Generation Windows Services (NGWS) Nó đ c thi t k hoàn toàn t s không đ dùng cho Internet Vi n t ng c a Microsoft xây d ng m t globally distributed system, dùng XML (ch a nh ng databases tí hon) làm ch t keo đ k t h p ch c n ng c a nh ng computers khác m t t ch c hay kh p th gi i Nh ng computers có th Servers, Desktop, Notebook hay Pocket Computers, đ u có th ch y m t software d a m t platform nh t, đ c l p v i hardware ngôn ng l p trình ó NET Framework Nó s tr thành m t ph n c a MS Windows s đ c port qua platform khác, có th c Unix M c d u Beta, NET Framework r t stable Visual Studio.NET r t bugs, có th dùng cho software development t bây gi Hi n có m t s sách v l p trình NET Wrox Oreilly xu t b n Các ph n c a Microsoft.NET Framework: NET application đ c chia làm hai lo i: cho Internet g i ASP.NET, Các t p Microsoft NET g m có Web Forms Web Services cho desktop g i Windows Forms Windows Forms gi ng nh Forms c a VB6 Nó h tr Unicode hoàn toàn, r t ti n cho ch Vi t th t s Object Oriented Web Forms có nh ng Server Controls làm vi c gi ng nh Controls Windows Forms, nh t có th dùng codes đ x lý Events y h t nh c a Windows Forms i m khác bi t chánh gi a ASP (Active Server Pages) ASP.NET ASP.NET, ph n đ i di n visual components code n m riêng nhau, không l n x n nh ASP Ngoài ASP.NET code hoàn toàn Object Oriented Web Services gi ng nh nh ng Functions mà ta có th g i dùng t URL Internet, thí d nh Credit Card authorisation ADO.NET m t lo i cache database nho nh (g i disconnected database) đ thay th ADO Thay application connects v nh vi n v i database m qua ADO, application NET làm vi c v i portable database ch ch a m t hai tables, copy t database m Khi c n, portable database (ADO.NET) s đ c reconciled v i database m đ update thay đ i Hai tables ADO.NET có th đ c related Master/Details relationship Vì ADO.NET có ch a original data l n data m i nh t nên Rollback ADO.NET r t d dàng nh ký XML đ c y m tr t i đa N m phía sau ADO.NET XML XML có th Table of records ADO.NET hay Tree of nodes DOM (Document Object Model) IO đ c h tr b ng toàn b Stream k c Memory Stream StreamReader/StreamWriter Thêm vào DataFormatting cho Serialisation đ ch a Object xu ng binary file hay text file TCP/IP http hai protocols thông d ng nh t NET, nh ng chúng làm vi c phía sau sân kh u giúp ta g i m t remote procedure (n m computer khác) d dàng nh m t local procedure K thu t y g i Remoting Các t p Microsoft NET Security h tr Cryptography, Permissions Policy Diagnostics cho ta Debug Trace Threading r t ti n đ n gi n đ implement Lightweight Process V n đ Timing NET program r t linh đ ng, hi u qu xác Vi c thi t k Common Language Runtime nh m vào m c tiêu sau đây: • Vi c tri n khai đ n gi n nhanh h n: developers s dành gi quy t đ nh ráp nh ng software components l i v i nhi u h n l p trình th t s • Các công tác thi t y u ("plumbing") nh memory management, process communication v.v đ c lo li u t đ ng • Các công c h tr r t đ y đ (no more API): NET Framework Base classes r t phong phú cho file, network, serialisation, mã hóa, XML, database, v.v • Cài đ t đ n gi n an toàn (no more DLL "hell"): ch c n xcopy files, gi ng nh th i vàng son c a DOS Lý NET application ch y NET framework, m t ta cài NET framework vào máy r i có đ y đ m i DLL c n thi t Có l t ng lai Microsoft cài NET framework chung v i Windows • Dùng cho t (scalability) WindowsCE đ n Desktop, đ n Web Các t p Microsoft NET Metadata Metadata d ki n c t ngh a cho ta bi t v d ki n Thí d XML Schema c a m t XML file metadata c t ngh a v data structure c a data XML file Chính XML Schema c ng m t XML file Các NET application components, g i Assembly, ch a r t nhi u metadata đ c t ngh a v (self describing) Tìm bi t v m t NET application đ có th làm vi c v i g i Reflection H tr ph i h p m i ngôn ng l p trình Common Language Runtime (CLR) trung tâm m c a NET Famework, h m máy đ ch y n ng tính c a NET Trong NET, m i ngôn ng l p trình đ u đ c compiled Microsoft Intermediate Language (IL) gi ng gi ng nh byte code c a Java Nh b t bu c m i ngôn ng đ u ph i dùng lo i data types (g i Common Type System) nên Common Language Runtime có th ki m soát m i interface, g i gi a components cho phép ngôn ng có th h p tác m t cách thông su t T c NET, VB.NET program có th inherit C# program ng c l i m t cách hoàn toàn t nhiên i u ch ng nh ng giúp VC++ hay Java programmers b t đ u 10 Các t p Microsoft NET dùng C# m t cách d dàng mà làm m t d án v i VB.NET programmers n a N u VC++ linh đ ng hi u n ng h n VB6, C# ch ng khác VB.NET B n có th port C# code qua VB.NET code r t d dàng Vì source code VC++ Java g n g i C# h n VB6 v i VB.NET nên đ i có nhi u C# code h n VB.NET Do đó, m c d u hai ngôn ng VB.NET C# đ u ngang c nhau, n u dùng C# b n đ nhi u source code s n nh t lâu ng c l i m có i ta v n mang n t ng r ng VC++ hay Java programmers m i th t s cao th l p trình, có khuynh h ng tr l ng guru VC++/Java cao h n VB programmers Khi ch y NET application, s đ c compiled b ng m t JIT (Just-In- Time) compiler r t hi u n ng machine code đ ch y i m giúp NET application ch y nhanh h n Java interpreted code Java Virtual Machine Just-In-Time c ng có ngh a ch ph n code c n x lý lúc y m i đ c compiled IL code ch y CLR đu c nói managed code .NET code có th ch y chung v i ActiveX, nh ng code ActiveX đ c g i unmanaged code, t c CLR không ch u trách nhi m Ngoài vi c allocation management of memory, CLR gi refrerence đ n objects đ rác (handle garbage collection), t c thâu l i m nh v n memory chúng không c n dùng n a Tr c đây, m i m t DLL đu c loaded vào memory, system s ghi nh n có task dùng đ task cu i ch m d t system unload DLL tr l i ph n memory dùng tr c đ system dùng cho chuy n khác Ch n u allocate memory đ dùng mà không nh dispose s b memory leak (r ), l n l n ta dùng h t memory, b b t bu c ph i reboot Các t p Microsoft NET N u b n click Tile vertical, hai forms child s đ d i đây: 160 c layout ki u Tile nh 161 Các t p Microsoft NET Bài 12 Nh ng ch c n ng m i giao di n c a s c a VB.NET (ph n V) Toolbars Toolbars NET đ c nâng c p b ng cách thêm ch c n ng cho ToolBarButtons collection c a nh ng buttons y dùng th Toolbar control, b n kh i đ ng m t Project m i đ t m t Toolbar vào form b ng cách doubleclick lên Toolbar icon Toolbox M t Toolbar s hi n n m d i tiêu đ c a form K rightclick lên Toolbar y ch n Properties đ edit property Buttons Collection b ng cách click lên ch (Collection) r i click ba d u ch m phía bên ph i đ hi n th ToolbarButton Collection Editor B n Add vào Toolbar ba buttons v i nh ng đ c tính sau: Khi ch y th ch • i property Text c a button th nh t (ToolbarButton1) Close ta mu n đóng ch ng trình user click lên button y By default Style c a ToolbarButton PushButton • i property Style c a button th nhì (ToolbarButton2) Separator ta mu n dùng đ t o kho ng cách gi a button th nh t button th ba • i property Text c a button th ba (ToolbarButton3) Background Colour property Style DropDownButton ta mu n dùng nh m t Combobox ng trình ta s th y hình gi ng nh d i đây: Các t p Microsoft NET 162 Bây gi ta s vi t code đ x lý Event Click c a Toolbar Ch có m t handler, Sub ToolBar1_ButtonClick, đ c dùng cho t t c buttons Ta phân bi t Button d a vào Index c a nó, gi ng gi ng nh m t array of buttons VB6 N u user click button th nh t ta s có ToolBar1.Buttons.IndexOf(e.Button) b ng 0, lúc y ta s Close form Private Sub ToolBar1_ButtonClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick Select Case ToolBar1.Buttons.IndexOf(e.Button) Case ' Close Button Me.Close() Case ' Never happens because the Button is a Separator Case ' MessageBox.Show("You clicked the third button") End Select End Sub N u không mu n dùng ToolBar1.Buttons.IndexOf(e.Button), b n c ng có th so sánh Buttons v i operator Is nh sau: If e.Button Is ToolBarButton1 Then Me.Close() ElseIf e.Button Is ToolBarButton3 Then Các t p Microsoft NET 163 MessageBox.Show("You clicked the third button") End If K cho đ t m t ContextMenu tên ContextMenu1 vào form assign vào property DropDownMenu c a button th ba nh hình d i đây: N u không mu n assign ContextMenu1 vào button th ba lúc thi t k , b n có th th c hi n vi c y b ng code lúc form m i load nh sau: Private Sub frmToolbar_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ToolBarButton3.DropDownMenu = ContextMenu1 End Sub B n edit hai menuItems cho ContextMenu1: m t tên mnuXám v i Text Xám tên mnuTr ng v i Text Tr ng Khi ch y ch ng trình, n u b n click có d u tam giác đen n m bên ph i button th ba, ContextMenu1 s hi n đ b n dùng N u b n click button th ba, ch ng trình c ng generate m t Click Event nh ng hi n gi ta không dùng nó, ch hi n th m t s p nh đ xác đ nh có Event Click y Nh th , ta th y NET ghép m t ContextMenu vào m t ToolbarButton đ bi n thành m t DropDownMenu Có u sau user ch n m t Item ContextMenu/DropDownMenu, Text c a Item không đ c hi n th gi ng nh m t ComboBox N u b n khó tính mu n có chuy n ph i t làm l y nh cho th y code d i đây: Private Sub frmToolbar_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ToolBarButton3.DropDownMenu = ContextMenu1 ToolBarButton3.Text = "Xám" End Sub Private Sub mnuXám_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuXám.Click MessageBox.Show("B n ch n màu Xám") Các t p Microsoft NET 164 ToolBarButton3.Text = "Xám" End Sub Private Sub mnuTr ng_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuTr ng.Click MessageBox.Show("B n ch n màu Tr ng") ToolBarButton3.Text = "Tr ng" End Sub Khi ch y ch ng trình b n s th y nh sau: ListBox Items m t collection of Strings M i dùng đ n, ta s th y NET ListBox r t gi ng ListBox VB6 Ti n ch bây gi ta có th edit string Items c a ListBox m t editor nho nh s hi n ta click vào ch (Collection) c a property Items: Các t p Microsoft NET 165 Các Items đ c ch a m t collection tên Items, ta có th làm vi c v i m i ch c n ng c a m t collection nh Add, Clear, Insert, Remove, RemoveAt, Count v.v Thí d nh ta cho thêm b n Items vào Listbox1 lúc Form_Load nh sau: Private Sub frmListbox_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Add individual items ListBox1.Items.Add("K ng-gu-ru") ListBox1.Items.Add("Công") ' Add more than one items by instantiating an object with items list enclosed in curly brackets {} ListBox1.Items.AddRange(New Object() {" u", "G u Panda"}) End Sub N u ch y ch ng trình, b n thêm nhi u Items vào ListBox mu n tránh update display Listbox nhi u l n, b n có th k p code gi a hai statements BeginUpdate EndUpdate nh sau: ' Shutdown the painting of the ListBox as items are added Các t p Microsoft NET 166 ListBox1.BeginUpdate() ' Loop through and add 50 items to the ListBox Dim x As Integer For x = To 50 ListBox1.Items.Add("Item " & x.ToString()) Next x ' Allow the ListBox to repaint and display the new items ListBox1.EndUpdate() Gi ng nh VB6, property MultiColumn hi n th Items nhi u c t n u đ c set thành True, property SelectionMode n u b ng MultiExtended cho ta select nhi u Items m t lúc Tuy nhiên, Items đ c ch n s có m t m t collection ch không ph i có Selected(i)=True nh VB6 Mu n select m t Item lúc run-time ta dùng code nh sau: ' Select three items (2nd, fourth and sixth) from the ListBox ListBox1.SetSelected(1, True) ' is index of 2nd item ListBox1.SetSelected(3, True) ListBox1.SetSelected(5, True) Trong thí d t i ta có ListBox1 v i danh sách v t S Thú Saigon Button List Items s li t kê danh sách ý cách ta hi n th m t Item v i expression Listbox1.Items(i).ToString Private Sub BtnListItems_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnListItems.Click Dim i As Integer Dim Mess As String ' make up the list of Items separated by CarriageReturn/LineFeed For i = To ListBox1.Items.Count - Mess &= (ListBox1.Items(i).ToString) & vbCrLf Next ' Show the list Các t p Microsoft NET 167 MessageBox.Show(Mess) End Sub Sau set property SelectionMode c a Listbox1 MultiExtended, code d i s li t kê danh sách items đ c ch n v i index c a chúng: Private Sub BtnListSelectedItems_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnListSelectedItems.Click Dim i As Integer Dim Mess As String ' make up the list of Selected Items separated by CarriageReturn/LineFeed ' Collection SelectedIndices contains the index of selecteditems For i = To ListBox1.SelectedItems.Count - Mess &= (ListBox1.SelectedIndices(i).ToString) & ":" & (ListBox1.SelectedItems(i).ToString) & vbCrLf Next ' Show the list MessageBox.Show(Mess, "Selected Items", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Các t p Microsoft NET 168 Items m t Array of Objects ListBox c a NET không h tr ItemData nh VB6 ItemData m t array ch a s t ng ng v i nh ng Items List array c a ListBox VB6 T c m i ListBox Item Vb6 có th đ c ch đ nh tr c m t s đ i di n Khi user select List(i), ta có th l y ItemData(i) c a List Item y Th t Items c a NET Listbox c ng có th m t Array of Objects, không nh t thi t ph i m t collection of Strings nh ta dùng D i code ta đ nh ngh a m t Class tên LBItem, đo n dùng code th Add m t Array of Objects lo i LBItem vào Listbox1: Public Class LBItem Private mList As String Private mItemData As Integer ' List Item of Listbox Public Property List() As String Get Return mList End Get Set ( ByVal Value As String) Các t p Microsoft NET 169 mList = Value End Set End Property ' ItemData of Listbox Public Property ItemData() As Integer Get Return mItemData End Get Set ( ByVal Value As Integer) mItemData = Value End Set End Property ' Function to return a string representing this item for display Overrides Function ToString() As String Return mList End Function End Class Sau Add m t Array of Objects vào ListBox1 ta ph i ch đ nh làm th đ hi n th m t Item Thí d nh dùng property List c a LBItem nh d i đây: ' Indicate that Property List of LBItem will be used to display ListBox1.DisplayMember = "List" N u ta không ch đ nh DisplayMember, t c ListBox1.DisplayMember = "" ListBox1 s dùng Function ToString c a LBItem đ hi n th Ngoài ra, đ tr v m t value gi ng nh ItemData c a List Item ta ch đ nh ValueMember nh d i đây: Private Sub BtnAddOjects_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddOjects.Click ' Clear all items in Listbox1 ListBox1.Items.Clear() Các t p Microsoft NET 170 Dim Objs(5) As LBItem ' Create an array of Objects of LBItem Dim i As Integer For i = To Objs(i) = New LBItem() Objs(i).List = "Line " & i.ToString Objs(i).ItemData = i + 100 Next ' Add the array of objects to Listbox1 ListBox1.DataSource = Objs ' Indicate that Property List of LBItem will be used to display ListBox1.DisplayMember = "List" ' Indicate that Property ItemData of LBItem will be used to return a value ListBox1.ValueMember = "ItemData" End Sub Khi ch y ch ng trình này, sau click nút Add Objects đ clear ListBox1 Add Objects m i, n u b n click hàng th ListBox s th y hình d i đây: Code x lý Event SelectedIndexChanged (t c Event Click tr c a ListBox1 gi ng nh d i đây: c đây) 171 Các t p Microsoft NET Private Sub ListBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Try If ListBox1.SelectedValue "" Then MessageBox.Show(ListBox1.SelectedValue & " of " & ListBox1.SelectedItem.ToString, "Selected value") End If Catch ex As Exception ' Do nothing, ignore this error End Try End Sub Nh th ta implemented (thi hành) cho NET ListBox m t ch c n ng t ng đ ng v i ItemData c a ListBox VB6 .NET ListBox không h CheckedListBox tr Style Checkbox, nh ng ta có th dùng ComboBox Vì ComboBox th a k t ListBox nên t t c nh ng ta bi t v ListBox đ u áp d ng cho ComboBox c bi t bây gi ComboBox có property MaxDropDownItems cho ta quy t đ nh hi n th items danh sách đ c m Kèm theo m t ch ng trình bi u di n ComboBox ta dùng Property ValueMember c a ComboBox đ tr v m t tr s đ i di n Item Data ComboBox1 đ c loaded t m t Access2000 database table b ng code sau đây: Private Sub frmCombo_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ds As New DataSet () ' Instantiate a Dataset ' Instantiate an OleDbDataAdapter for Access2000 database Authors.mdb and return table Authors Dim myData As New OleDbDataAdapter("Select * from Authors", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \Authors.mdb") myData.Fill(ds, "Authors") ' Load table Authors into Dataset 172 Các t p Microsoft NET With ComboBox1 ' Bind Table Authors to ComboBox1 DataSource = ds.Tables("Authors") ' Make Property/Datafield FullName the DisplayMember of ComboBox1 DisplayMember = "FullName" ' Make Property/Datafield AuthorID the ValueMember of ComboBox1 ValueMember = "AuthorID" End With End Sub Chúng ta ch đ nh record datafield FullName làm DisplayMember c a ComboBox1 datafield AuthorID làm ValueMember c a ComboBox1 Ta truy c p data c a c s d li u b ng cách dùng m t DataAdapter lo i OleDbDataAdapter cho m t SQL CommandText: "Select * from Authors" m t connection string, có cho bi t database driver: Microsoft.Jet.OLEDB.4.0 tên c a database \Authors.mdb File Authors.mdb n m chung v i mã ngu n c a ch ng trình parent folder c a folder bin, n i ch a ComboBox.exe K ta dùng DataAdapter đ b table Authors vào dataset ds Cách làm vi c t ng t nh ADO (Active Data Object) VB6 Có m khác Dataset có th ch a nhi u tables (recordsets) ho t đ ng nh m t cached disconnected database b nh K thu t có tên ADO.NET ta s bàn thêm nhi u v t ng lai M i l n user select m t item m i t ComboBox1, ch th AuthorId, ValueMember Label1 ng trình s hi n Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Try 'Display the selected valueMember Label1.Text = ComboBox1.SelectedValue Catch End Try Các t p Microsoft NET 173 End Sub có hai cách đ ta select m t ComboBox item b ng coding Cách th nh t cho bi t AuthorId (ValueMember), user clicks button Select by AuthorId đ th y k t qu : Private Sub BtnSelectbyAuthorId_Click_1( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSelectbyAuthorId.Click 'Use Try to ignore error if operation fails Try ' Select the ComboBox Item whose valueMember equal txtAuthorId.Text ComboBox1.SelectedValue = txtAuthorId.Text Catch End Try End Sub cách th hai cho bi t FullName (DisplayMember), user clicks button Select by Name đ th y k t qu : Private Sub BtnSelectByName_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSelectByName.Click 'Use Try to ignore error if operation fails Try ' Select the ComboBox Item whose DisplayMember equal txtFullName.Text ' FindString returns the index of the found item ComboBox1.SelectedIndex = ComboBox1.FindString(txtFullName.Text) Catch End Try End Sub Khi ch y ch ng trình, b n s th y hình nh CÁCd i Trong hình y, MaxDropDownItems c a ComboBox1 đ c set b ng Các t p Microsoft NET 174 [...]... i đánh tr c ti p vào MainMenu Thêm các menuitems m i b ng cách đánh th ng vào các ch có ch Type Here L u ý các Tabs bên trên Editing Area khi b n m nhi u forms Các bài t p Microsoft NET Edit MainMenu, insert m t Separator 22 Các bài t p Microsoft NET Chú ý danh sách các References đ Edit Shortcut cho m t menu item 23 c li t kê trong Solution Explorer Các bài t p Microsoft NET 24 Button bây gi ch ng... y b thay đ i bên ngoài VS.NET, khi VS.NET khám phá ra nó s load vào trong VS.NET cho b n n u b n đ ng ý Các bài t p Microsoft NET 15 m m t Solution/Project có s n, b n có th click link c a tên project trên trang StartPage, hay dùng Menu command File | Open | Project , hay Menu Command File | Recent Projects Visual Studio.NET IDE 16 Các bài t p Microsoft NET Giao di n c a VS.NET có nh ng đ c đi m gi... nh rang đ làm v n, câu cá Trong bài t i ta s h c v Visual Studio.NET interface và b t đ u vi t th m t program Demo 13 Các bài t p Microsoft NET Bài 2 Visual Studio.NET Visual Studio.NET ch y VisualStudio.NET b n c n ph i Set Time c a Windows l i tr c cu i tháng 7,2001, eg: 1-July-2001 Visual Studio.NET Beta 2 hi n nay có Service Pak 2, nó cho phép ta dùng Visual Studio.NET Beta 2 sau ngày 31-July-2001,... có time-bomb N u VS.NET version c a b n b gi i h n v th i gian nói trên, t trong VS.NET b n có th download Service Pak 2 đ cài đ t b ng cách dùng IDE Menu Command Help | Check for Updates Có hai cách đ b t đ u m t project m i trong VS.NET Ho c Click New Project trong trang Web StartPage nh d i đây: Ho c dùng Menu command File | New | Project gi ng nh trong VB6 IDE: 14 Các bài t p Microsoft NET Khi Add... Add Reference các NET components Nh ng b n c ng có th dùng ActiveX (có s n trong VB6) trong NET application b ng cách Add Reference COM (click Tab COM trên Add Reference Dialog) .NET s gói ActiveX thành m t NET component (click Yes tr 20 Các bài t p Microsoft NET l i câu h i "Would you like to have a wrapper generated for you?") Ngoài ra dùng Add Reference Projects đ refer đ n DLL c a các User developed... n u không có VS.NET ta có th dùng Notepad vi t code nh th và sau khi compile, nó v n ch y y h t nh trong tr dùng VS.NET ng h p ta i m này gi ng nh trong Java, ta có th vi t code b ng Notepad và dùng Command line đ compile và link code file v i các components khác Thí d nh khi ta vi t m t VB.NET program đ n gi n đ ch y trên trong DOS Console, ta có th compile nó nh sau: 30 Các bài t p Microsoft NET vbc... Window đ in ra các messages mà trong VB6 b n dùng Immediate Window Thí d trong VB6 b n vi t: Debug.Print "Count=" & CStr(Count) đ in ra trong Immediate Window, thì trong VB.NET b n có th vi t: Console.WriteLine("Count= {0}", Count) 19 Các bài t p Microsoft NET đ in ra trong Output Window D nhiên b n v n có th ti p t c dùng Immediate Window trong công tác Debug nh tr c đây trong VB6 Ch a h t, VS.NET còn cho... theo góc y c a form thanh D và Right đây ta click hai i và Ph i cho chúng tr nên màu đen đ ch n Anchor Bottom 25 Các bài t p Microsoft NET Trong lúc thi t k ta có th edit các List items c a m t Listbox b ng cách m property Items Collection ra và Edit vào m t trang Walk Through Code Trong NET, các classes đ System.XML, c chia ra thành nhi u nhóm nh System.IO, System.Data, System.Drawing v.v Ngoài ra trong... ch ng nh ng m t, mà 28 Các bài t p Microsoft NET nhi u controls khác đ u có th ng ký (Register) đ đ c Thông báo (Notified) khi Event y xãy ra Control RaiseEvent đ c g i là Publisher (Nhà Xu t B n), các controls mu n handle event đ c g i là Subcribers (nh ng Ng i t Mua dài h n) D nhiên cách handle event c a m i control đ u khác nhau, d u r ng đ c passed cho cùng hai parameters Các EventHandlers này... cho nó n m ngang và vertical Toolbox tab hi n ra bên trái Lúc y, Toolbox trong tr ng thái Auto Hide (hi n ra/rút vào) nh d i đây: Các bài t p Microsoft NET 17 B n có th g i h u h t các Windows hi n th b ng cách dùng Menu commands View, View | Other Windows và Debug | Windows: Các Windows bên ph i có th float, n m ch ng lên nhau thành nh ng Tabs trong m t Window set hay n m c nh nhau vertically tiled N ... Các t p Microsoft NET Edit MainMenu, insert m t Separator 22 Các t p Microsoft NET Chú ý danh sách References đ Edit Shortcut cho m t menu item 23 c li t kê Solution Explorer Các t p Microsoft. .. hình ta dùng nh ng namespaces liên h đ n VB6 nh Microsoft. Visualbasic Thay code: Microsoft. Visualbasic.Left ( InputString,6) ta tuyên b : Imports VB6= Microsoft. Visualbasic Sau ta có th code: VB6.Left... có đ y đ m i DLL c n thi t Có l t ng lai Microsoft cài NET framework chung v i Windows • Dùng cho t (scalability) WindowsCE đ n Desktop, đ n Web Các t p Microsoft NET Metadata Metadata d ki n c

Ngày đăng: 30/10/2015, 17:41

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan