如何从文本文件VB2013生成工具提示

时间:2016-04-16 02:53:41

标签: vb.net visual-studio

我有这个带有段落的txt文件,每个段落都以^符号开头,这样我就可以识别每一行。在我的代码中,我希望在加载时显示随机段落的表单中生成工具提示。

我设法得到一个字符串值到工具提示和它的位置,我已设法“加载”整个文本文件在另一个字符串值,我现在要做的就是找到并选择一个基于“^”符号,删除符号并在工具提示中显示。因为我计划将来编辑txt文件,所以这成为一个自动化过程我想到的功能是给每个以^开头的行,生成一个随机数,然后显示该函数。我已经做到了这一点:

Public Class Question
Dim quote As String
Dim qn As Double
Private Sub Question_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
    ToolTip1.ShowAlways = True

    Dim bquote As String
    bquote = My.Computer.FileSystem.ReadAllText("C:\texts.txt")

    Dim thesearch As String
    thesearch = "^"

    ' for test quote = "i've made it"
    ToolTip1.SetToolTip(Me, quote)
End Sub

请帮忙

1 个答案:

答案 0 :(得分:0)

适用于此特定问题的代码是:

Public Class Question
Dim quote As String
Private Sub Question_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
    ToolTip1.ShowAlways = True
    Dim inital_text_load As String
    inital_text_load = My.Computer.FileSystem.ReadAllText("C:\texts.txt")

    Dim quote_array As String()

    quote_array = inital_text_load.Split("^".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

    Dim rand As New Random()
    Dim index As Integer = rand.Next(0, quote_array.Length - 1)

    quote = quote_array(index)

    ToolTip1.SetToolTip(Me, quote)

End Sub
End Class