Visual Basic .NET Language
Total Page:16
File Type:pdf, Size:1020Kb
Visual Basic .NET Language #vb.net 1 1: Visual Basic .NET 2 2 2 Examples 2 2 Hello World 3 4 4 2: ByValByRef 12 Examples 12 ByVal 12 ByRef 12 3: FTP 14 14 Examples 14 FTP 14 FTP 14 FTP 14 FTP 14 4: GDI + 15 Examples 15 15 15 16 16 5: LINQ 18 18 Examples 18 18 18 Select 18 18 IEnumerable 19 Distinct 19 6: OOP 20 Examples 20 20 20 20 NotInheritable 20 MustInherit 20 21 21 21 NotOverridable 21 MustOverride 22 MyBase 22 MyClass 23 24 24 25 7: VB.NET 26 26 Examples 26 26 Employee Class 28 8: Visual Basic 14.0 32 32 Examples 32 32 NameOf 32 33 33 34 34 #Region 35 35 35 9: WindowsGoogle 37 Examples 37 WindowsGoogle Map 37 10: WinForms SpellCheckBox 47 47 Examples 47 ElementHost WPF TextBox 47 11: WPF XAML 50 50 Examples 50 ViewModelStringViewTextBox 50 12: 52 Examples 52 IDisposable 52 52 13: BackgroundWorker 54 Examples 54 Background worker 54 14: Statement 55 55 Examples 55 Disposable 55 15: Windows 56 Examples 56 Form 56 56 16: 58 58 Examples 58 58 2 58 17: 59 Examples 59 .gz 59 18: 60 Examples 60 60 60 60 60 19: VB.NetaxWindowsMediaPlayer 61 61 Examples 61 axWindowsMediaPlayer 61 63 20: 65 Examples 65 Async / Await 65 LINQTAP 65 21: 67 Examples 67 Thread Class 67 22: 69 69 Examples 69 69 69 69 - 69 23: 71 Examples 71 Console.ReadLine 71 Console.WriteLine 71 Console.Write 71 Console.Read 71 Console.ReadKey 71 72 24: 74 Examples 74 ... 74 For Each ... Next 75 while 75 ...... 75 76 78 25: 79 79 Examples 79 79 79 79 79 80 80 80 80 81 26: 82 82 Examples 82 82 82 83 83 27: 85 Examples 85 85 DataTable 86 86 28: / 88 Examples 88 zip 88 zip 88 zip 88 88 29: 90 90 Examples 90 90 90 StreamWriter 90 30: 91 Examples 91 91 91 31: 92 Examples 92 92 92 Flags 92 HasFlag 93 93 GetNames 94 GetValues 94 ToString 94 EnumFlagsAttribute 95 For-each 96 96 96 32: 99 Examples 99 IF ... THEN ... ELSE 99 99 33: 100 Examples 100 100 100 “” 100 100 100 101 34: 102 102 Examples 102 102 102 103 103 104 104 35: BackgroundWorker 105 Examples 105 BackgroundWorker 105 BackgroundWorkerGUI 106 36: NullReferenceException 107 107 Examples 107 107 107 37: AndAlso - OrElse 108 108 108 108 Examples 108 108 OrElse 108 NullReferenceException 109 109 109 38: 111 Examples 111 Control.Invoke 111 Async / Await 111 39: 113 113 Examples 113 113 - 115 116 40: 120 Examples 120 120 120 120 121 121 122 123 41: 126 126 Examples 126 126 126 127 42: 128 Examples 128 128 43: 129 129 Examples 129 129 129 130 131 131 131 132 44: 133 Examples 133 n 133 45: 134 134 134 Examples 134 134 134 46: 136 Examples 136 136 / 136 136 47: 138 138 Examples 138 138 138 48: 139 Examples 139 ......... 139 139 140 140 140 49: 142 142 142 Examples 142 142 Random 142 50: 144 144 Examples 144 144 144 51: 146 146 146 Examples 146 146 148 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: visual-basic--net-language It is an unofficial and free Visual Basic .NET Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Visual Basic .NET Language. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/zh-TW/home 1 1: Visual Basic .NET Visual Basic .NETMicrosoftVisual Basic。 Visual Basic [.NET]PythonC ++。 VB .NETEnd If Next End Sub。 VB.NET Visual Studio .NET Framework 7 2002 1.0 2002213 7.1 2003 1.1 2003-04-24 8 2005 2.0 / 3.0 2005-10-18 9 2008 3.5 2007-11-19 10.0 2010 4 2010-04-12 11.0 2012 4.5 2012-08-15 12.0 2013 4.5.1 / 4.5.2 〇 14.0 2015 4.6.0~4.6.2 2015-07-20 15.0 2017 4.7 201737 Examples Microsoft Visual StudioCommunity。Console ApplicationVisual Basic'Hello World' Module Module1 Sub Main() Console.WriteLine("Hello World") End Sub End Module F5 “ ”“”“” 。 'Hello World'。 https://riptutorial.com/zh-TW/home 2 Hello World 11 button1Button1_Click event Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub End Class textbox1 。 .Text。 Property Textbox.Text, gets or sets the current text in the TextBox 。Textbox1.Text Textbox1.Text=。Textbox1.TextHello World 。Hello WorldTextbox1.Text TextBox1.Text = "Hello World" button1clicked event Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox1.Text = "Hello World" End Sub End Class https://riptutorial.com/zh-TW/home 3 VB“Region” #Region "Events" Protected Sub txtPrice_TextChanged(...) Handles txtPrice.TextChanged 'Do the ops here... End Sub Protected Sub txtTotal_TextChanged(...) Handles txtTotal.TextChanged 'Do the ops here... End Sub 'Some other events.... #End Region 1000+。。 VS 2005,2008,2010,20152017。 。 1. https://www.visualstudio.com/downloads/Visual Studio。 https://riptutorial.com/zh-TW/home 4 2. https://riptutorial.com/zh-TW/home 5 3. Visual Basic“Windows”。。 4. “” https://riptutorial.com/zh-TW/home 6 5. “”。“”。“”“”“x”。 6. https://riptutorial.com/zh-TW/home 7 。。 https://riptutorial.com/zh-TW/home 8 https://riptutorial.com/zh-TW/home7. Properties。Text。 Font。 9 8. 。。 9. VB.Net。。。 10. 。 Dim new。。。 https://riptutorial.com/zh-TW/home 10 VB.Net Visual Basic .NET https://riptutorial.com/zh-TW/vb-net/topic/352/visual-basic--net https://riptutorial.com/zh-TW/home 11 2: ByValByRef Examples ByVal ByValByVal。 。 Class SomeClass Public Property Member As Integer End Class Module Program Sub Main() Dim someInstance As New SomeClass With {.Member = 42} Foo (someInstance) ' here someInstance is not Nothing (still the same object) ' but someInstance.Member is -42 (internal state can still be changed) Dim number As Integer = 42 Foo(number) ' here number is still 42 End Sub Sub Foo(ByVal arg As SomeClass) arg.Member = -arg.Member ' change argument content arg = Nothing ' change (re-assign) argument End Sub Sub Foo(arg As Integer) ' No ByVal or ByRef keyword, ByVal is assumed arg = -arg End Sub End Module ByRef ByRef。 Class SomeClass Public Property Member As Integer End Class Module Program Sub Main() Dim someInstance As New SomeClass With {.Member = 42} Foo (someInstance) ' here someInstance is not Nothing ' but someInstance.Member is -42 Bar(someInstance) https://riptutorial.com/zh-TW/home 12 ' here someInstance is Nothing End Sub Sub Foo(ByVal arg As SomeClass) arg.Member = -arg.Member ' change argument content arg = Nothing ' change (re-assign) argument End Sub Sub Bar(ByRef param As Integer) arg.Member = -arg.Member ' change argument content arg = Nothing ' change (re-assign) argument End Sub End Module ByValByRef https://riptutorial.com/zh-TW/vb-net/topic/4653/byvalbyref https://riptutorial.com/zh-TW/home 13 3: FTP • My.Computer.Network.DownloadFileserverFile As StringlocalFile As String • My.Computer.Network.DownloadFileserverFile As StringlocalFile As Stringuser As String password As String • My.Computer.Network.UploadFilelocalFile As StringserverFile As String • My.Computer.Network.UploadFilelocalFile As StringserverFile As Stringuser As String password As String Examples FTP My.Computer.Network.DownloadFile("ftp://server.my/myfile.txt", "donwloaded_file.txt") server.mymyfile.txtdonwloaded_file.txt。。 FTP My.Computer.Network.DownloadFile("ftp://srv.my/myfile.txt", "donwload.txt", "Peter", "1234") srv.mymyfile.txtdonwload.txt。。Peter1234。 FTP My.Computer.Network.UploadFile("example.txt", "ftp://server.my/server_example.txt") example.txtserver.my。server_example.txt 。 FTP My.Computer.Network.UploadFile("doc.txt", "ftp://server.my/on_server.txt", "Peter", "1234") doc.txt server.myserver.my。server_example.txt 。Peter1234。 FTP https://riptutorial.com/zh-TW/vb-net/topic/4078/ftp https://riptutorial.com/zh-TW/home 14 4: GDI + Examples 1. ... 'this will work on any object's paint event, not just the form Private Sub Form1_Paint(sender as Object, e as PaintEventArgs) Handles Me.Paint Dim gra as Graphics gra = e.Graphics End Sub 2. Dim btn as New Button Dim g As Graphics = btn.CreateGraphics 3. 'The existing image can be from a filename, stream or Drawing.Graphic Dim image = New Bitmap("C:\TempBit.bmp") Dim gr As Graphics = Graphics.FromImage(image) Pen 1. 2. Dim pens As New Pen(Color.Purple) pens.DashStyle = DashStyle.Dash 'pen will draw with a dashed line pens.EndCap = LineCap.ArrowAnchor 'the line will end in an arrow pens.StartCap = LineCap.Round 'The line draw will start rounded '*Notice* - the Start and End Caps will not show if you draw a closed shape Private Sub GraphicForm_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint Dim pen As New Pen(Color.Blue, 15) 'Use a blue pen with a width of 15 Dim point1 As New Point(5, 15) 'starting point of the line Dim point2 As New Point(30, 100) 'ending point of the line e.Graphics.DrawLine(pen, point1, point2) e.Graphics.DrawRectangle(pen, 60, 90, 200, 300) 'draw an outline of the rectangle 1 Dim pen2 as New Pen(Color.Orange) 'Use an orange pen with width of 1 Dim origRect As New Rectangle(90, 30, 50, 60) 'Define bounds of arc e.Graphics.DrawArc(pen2, origRect, 20, 180) 'Draw arc in the rectangle bounds End Sub https://riptutorial.com/zh-TW/home 15 Graphics.FillShapes。 1.