weixin_52087280
刘力乾
采纳率100%
2021-03-01 23:17

求各位大佬解释一下vb.net这几串代码 没学过vb真的看不懂

5

 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

  • 点赞
  • 收藏
  • 复制链接分享

2条回答

  • soar3033 soar3033 1月前
     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
    点赞 评论 复制链接分享
  • technologist_04 CSDN技术专家团—麒麟奇 1月前

    按钮点击事件处理,打开文件对话框,选择文件后,读出文件内容显示到两个文本框中

    点赞 评论 复制链接分享