在textbox vb.net中的特定文本或单词后添加文本

时间:2015-09-02 10:37:58

标签: vb.net

我有一个组合框和文本框。

组合框将有供用户选择的选项。

Textbox1有文字: - Troubleshooting Steps:在文本框的属性中设置。

如果用户从组合框列表中选择:Rebooted PC,则按一个按钮,它将在Troubleshooting Steps:之后添加文本。

示例:Troubleshooting Steps: Rebooted PC

我希望能够做的是一个接一个地添加多个选择。

示例:

Troubleshooting Steps: Rebooted PC- Problem returned after reboot- Replaced part

我做了一些谷歌搜索,发现这个代码已经改变了它的一部分。

但我遇到的问题是将最后一个文本选为Troubleshooting Steps:,将所选的第一个文本推到最后。

发生什么事:

Troubleshooting Steps: (3rd)Replaced part- (2nd)Problem returned after reboot- (1st)Rebooted PC

代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim text As String = TextBox1.Text

    Dim index As Integer = text.IndexOf("Troubleshooting Steps:")

    Dim countChars As Integer
    countChars = "Troubleshooting Steps:".Length

    If index >= 0 Then
        text = text.Insert(index + countChars, ComboBox1.Text)
        TextBox1.Text = text
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

这里有更新的,有一个好的!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'you can set the TextBox text either here or in the properties panel
    TextBox1.Text = "Troubleshooting Steps: "
    TextBox2.Text = "Follow up: "
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    If ComboBox1.SelectedIndex < 0 Then
     'MsgBox("Your message if nothing is selected in ComboBox1 when the button is pressed")
    Else
        TextBox1.Text = TextBox1.Text & " - " & ComboBox1.SelectedItem.ToString
    End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If ComboBox2.SelectedIndex < 0 Then
    'MsgBox("Your message if nothing is selected in ComboBox2 when the button is pressed")
    Else
        TextBox2.Text = TextBox1.Text & " - " & ComboBox2.SelectedItem.ToString
    End If
End Sub