将文本同时放在其他位置的2个文本框中

时间:2014-04-10 05:34:12

标签: vb.net

嘿,我想在2个不同的文本框中插入文本,也在内部标记

所以我有一个空的文本框,当你点击我的按钮时它会这样做:

    Dim myNum As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    myNum = InputBox("Please input a Color code. Dont forget the # for the code Example: #000000")
    Dim insertText = "[color=" + myNum + "]" + "[/color]"
    Dim insertText2 = "<font color=" + DubbelQ + myNum + DubbelQ + "></font>"
    Dim insertPos As Integer = TextBox1.SelectionStart

    TextBox2.Text = TextBox2.Text.Insert(insertPos, insertText2)
    TextBox2.SelectionStart = insertPos + insertText2.Length
    TextBox1.Text = TextBox1.Text.Insert(insertPos, insertText)
    TextBox1.SelectionStart = insertPos + insertText.Length
End Sub

但是当我想在标签中添加另一种颜色时,它不起作用[

1 个答案:

答案 0 :(得分:1)

那是因为您正在使用第二个文本框中第一个文本框的SelectionStart。 如果希望它能正常工作,您需要找到一种计算thextbox2正确插入点的方法。

根据标签的不同长度,您需要在第二个文本框中使用insertPos + 6.

试试这段代码:

Dim DubbelQ As String = """"
Dim myNum As String
Dim NumberOfColorTags As Integer = 0  
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    myNum = InputBox("Please input a Color code. Dont forget the # for the code Example: #000000")
    Dim insertText = " [color=" + myNum + "]" + "[/color]"
    Dim insertText2 = "<font color=" + DubbelQ + myNum + DubbelQ + "></font>"
    Dim insertPos As Integer = TextBox1.SelectionStart
    Dim insertPos2 As Integer = insertPos += (6 * NumberOfColorTags)

    TextBox2.Text = TextBox2.Text.Insert(insertPos2, insertText2)
    TextBox2.SelectionStart = insertPos2 + insertText2.Length
    TextBox1.Text = TextBox1.Text.Insert(insertPos, insertText)
    TextBox1.SelectionStart = insertPos + insertText.Length
    NumberOfColorTags++
End Sub

Dim DubbelQ As String = """" Dim myNum As String Dim NumberOfColorTags As Integer = 0 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click myNum = InputBox("Please input a Color code. Dont forget the # for the code Example: #000000") Dim insertText = " [color=" + myNum + "]" + "[/color]" Dim insertText2 = "<font color=" + DubbelQ + myNum + DubbelQ + "></font>" Dim insertPos As Integer = TextBox1.SelectionStart Dim insertPos2 As Integer = insertPos += (6 * NumberOfColorTags) TextBox2.Text = TextBox2.Text.Insert(insertPos2, insertText2) TextBox2.SelectionStart = insertPos2 + insertText2.Length TextBox1.Text = TextBox1.Text.Insert(insertPos, insertText) TextBox1.SelectionStart = insertPos + insertText.Length NumberOfColorTags++ End Sub