首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在textbox vb.net中的特定文本或单词后添加文本

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

Stack Overflow用户
提问于 2015-09-02 18:37:58
回答 1查看 930关注 0票数 0

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

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

在textbox的属性中设置了Textbox1:- Troubleshooting Steps:

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

示例:Troubleshooting Steps: Rebooted PC

我想要做的是一个接一个地添加多个选择。

示例:

代码语言:javascript
复制
Troubleshooting Steps: Rebooted PC- Problem returned after reboot- Replaced part

我做了一些谷歌搜索,找到了这段代码,我修改了它的一部分。

但我的问题是,它会将最后选择的文本添加到Troubleshooting Steps:,将第一个选择的文本推送到末尾。

发生了什么?示例:

代码语言:javascript
复制
Troubleshooting Steps: (3rd)Replaced part- (2nd)Problem returned after reboot- (1st)Rebooted PC

代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-09-02 20:18:43

这是最新的版本,祝你有一个好的版本!

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32350928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档