Vbdotnet子串

时间:2014-10-08 21:20:45

标签: vb.net

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(TextBox1.Text)      
       Dim a As String
       a = reader.ReadLine     
       RichTextBox1.Text = RichTextBox1.Text + a
       Label5.Text = RichTextBox1.Text.Substring(5, a.Substring(5, a.Length))
       reader.Close()
    End Sub

您好。我正在尝试读取文本文件并将数字放在不同的变量中。文本文件就像这样

IMAGE

每次运行代码时都会出现错误。我该怎么办?

1 个答案:

答案 0 :(得分:1)

你试图用这条线做什么?

    Label5.Text = RichTextBox1.Text.Substring(5, a.Substring(5, a.Length))

这一部分:a.Substring(5, a.Length)返回一个字符串,但Substring的第二个参数需要一个整数,因此它会导致错误。

Substring如下所示:Substring(startIndex As Integer, length As Integer)。您正在尝试将字符串传递给第二个参数。

简单地从该行中删除第二个参数似乎可以按照您的意愿执行:

    Label5.Text = RichTextBox1.Text.Substring(5)

如果这些类型的问题经常影响到您,我强烈建议您在Visual Studio中启用Option Strict。 Here are some instructions on how to do so.