繁体   English   中英

在运行时编辑多行文本框时出现问题 vb.net

[英]Problems editing multiline textbox during runtime vb.net

我是一名大学生,没有太多 vb.net 经验,在个人项目上需要一些帮助。 我正在尝试增加多行文本框控件的高度,以便在运行时插入多行。 按回车键后,我想将 go 插入文本框中的新行。 此外,同时我试图在通过按回车创建的每个文本框行旁边生成一个 combobox 。 这是我到目前为止的代码:

Private Sub ThisTextBox_keypress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles ThisTextBox.KeyPress

    Dim TextboxLine As String() = ThisTextBox.Text.Split(vbNewLine)
    Dim Linecount As Integer = TextboxLine.Count
    If e.KeyChar = Chr(Keys.Enter) Then
       Me.ThisTextBox.Height = TextRenderer.MeasureText(" ", Me.ThisTextBox.Font).Height * _ Linecount 
       For Each Item In TextboxLine
            Dim newCombobox = New ComboBox()
            Me.Controls.Add(newCombobox)
            newCombobox.Items.Insert(0, "Item 1")
            newCombobox.Items.Insert(1, "Item 2")
            newCombobox.Items.Insert(2, "Item 3")
            newCombobox.Items.Insert(3, "Item 4")

            newCombobox.Location = New System.Drawing.Point(108, 69+=27)
            newCombobox.Size = New System.Drawing.Size(92, 21)

        Next
    End If


End Sub

问题是文本框的高度随着我输入的每个字符而增加,当我按下回车键时,控件高度以奇怪的增量增加,这与文本字体 * 行数无关。 此外,我的代码可能与如何在运行时创建 combobox 并将其设置到特定位置相距甚远,但希望您能够看到我想要做什么。 提前致谢。

有趣的问题。

以下代码几乎可以实现您想要的,但是在每个文本行旁边放置一个 combobox 会导致问题,因为没有足够的空间使 combobox 足够高。

Private Sub ThisTextBox_keypress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles ThisTextBox.KeyPress
    Static previousLineCount = 0
    Dim LineCount As Integer = ThisTextBox.Lines.Count
    If LineCount > previousLineCount Then

        Dim lineHeight As Integer = TextRenderer.MeasureText(" ", Me.ThisTextBox.Font).Height
        ThisTextBox.Height = lineHeight * LineCount + 10
        For Each Item In ThisTextBox.Lines
            Dim newCombobox = New ComboBox()
            Me.Controls.Add(newCombobox)
            newCombobox.Items.Insert(0, "Item 1")
            newCombobox.Items.Insert(1, "Item 2")
            newCombobox.Items.Insert(2, "Item 3")
            newCombobox.Items.Insert(3, "Item 4")

            newCombobox.Location = New System.Drawing.Point(ThisTextBox.Left + ThisTextBox.Width, ThisTextBox.Top + (LineCount) * lineHeight - 12)
            newCombobox.Size = New System.Drawing.Size(92, lineHeight)

        Next
    End If
End Sub

我不知道有什么方法可以改变 combobox 控件的高度(这里的其他人可能知道你是否可以),但无论如何它看起来很有趣。

您还可以考虑使用 RichTextBox 控件并更改行距。

希望上面的代码无论如何都能帮助您入门。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM