求各比特大佬解釋一下vb.net這幾串代碼 沒學過vb真的看不懂

CSDN問答 2022-01-08 04:12:51 阅读数:799

各比 比特 特大 大佬 一下

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myStr As String

        Dim mystream As System.IO.Stream

        OpenFileDialog1.Filter = "All files (*.*)|*.*"

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

 

            mystream = OpenFileDialog1.OpenFile()

            If Not (mystream Is Nothing) Then

                myStr = IO.File.ReadAllText(OpenFileDialog1.FileName)

                mystream.Close()

 

                TextBox1.Text = "經度" & vbCrLf   

                TextBox2.Text = "緯度" & vbCrLf

                Dim arrStr() As String

                arrStr = Split(myStr, vbCrLf)

 

                For i As Integer = 0 To UBound(arrStr)

                    If arrStr(i) <> "" Then

                        Dim tmp() As String, tmp1() As String

                        tmp = Split(arrStr(i), ";")

                        tmp1 = Split(tmp(1), ",")

                        TextBox1.Text = TextBox1.Text & tmp1(2) & vbCrLf

                        TextBox2.Text = TextBox2.Text & tmp1(3) & vbCrLf

                    End If

                Next

            End If

        End If




采納答案:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myStr As String //聲明一個string變量 Dim mystream As System.IO.Stream //聲明一個stream變量 OpenFileDialog1.Filter = "All files (*.*)|*.*" //配置openfiledialog OpenFileDialog1.FilterIndex = 2 //配置openfiledialog OpenFileDialog1.RestoreDirectory = True //配置openfiledialog If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then //彈出openfiledialog 如果按ok鍵則執行下面的 mystream = OpenFileDialog1.OpenFile() //將Opendialog返回的文件賦值給stream流變量 If Not (mystream Is Nothing) Then //如果變量非空則執行 myStr = IO.File.ReadAllText(OpenFileDialog1.FileName) //讀取文件中的所有內容 mystream.Close() //讀取完畢後關閉文件流 TextBox1.Text = "經度" & vbCrLf //把textbox1的內容改為“經度”+換行符 TextBox2.Text = "緯度" & vbCrLf //把textbox2的內容改為“緯度”+換行符 Dim arrStr() As String //聲明一個字符串數組 arrStr = Split(myStr, vbCrLf) //將讀取到的文件內容用換行符切片並存到之前聲明的數組 For i As Integer = 0 To UBound(arrStr) //按數組的長度進行遍曆(但這個地方看起來應該是UBound(attStr)-1才對,不然應該會數組越界) If arrStr(i) <> "" Then //如果數組元素的值非空則 Dim tmp() As String, tmp1() As String //聲明兩個字符串數組 tmp = Split(arrStr(i), ";") //繼續對數組元素用分號進行切片並賦值到tmp tmp1 = Split(tmp(1), ",") //繼續對tmp用逗號進行切片並賦值到tmp1 TextBox1.Text = TextBox1.Text & tmp1(2) & vbCrLf //textbox1的內容附加上tmp1中的第三個元素並附加換行符 TextBox2.Text = TextBox2.Text & tmp1(3) & vbCrLf //textbox2的內容附加上tmp1中的第三個元素並附加換行符 End If Next End If End If


其他答案2:

按鈕點擊事件處理,打開文件對話框,選擇文件後,讀出文件內容顯示到兩個文本框中

版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201080412507801.html