简体   繁体   English

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

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

I am a college student without much vb.net experience and need some help on a personal project.我是一名大学生,没有太多 vb.net 经验,在个人项目上需要一些帮助。 I am trying to increase the height of a multiline textbox control so I can insert multiple lines during runtime.我正在尝试增加多行文本框控件的高度,以便在运行时插入多行。 I want to go to a new line in my textbox after I hit enter.按回车键后,我想将 go 插入文本框中的新行。 Also, simultaneously I am trying to generate a combobox next to each textbox line created by hitting enter.此外,同时我试图在通过按回车创建的每个文本框行旁边生成一个 combobox 。 Here is the code I have so far:这是我到目前为止的代码:

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

The problem is the textbox increases its height with each character I type into it, and when I hit enter, the control height increases in weird increments that cannot be related to the text font * number of lines.问题是文本框的高度随着我输入的每个字符而增加,当我按下回车键时,控件高度以奇怪的增量增加,这与文本字体 * 行数无关。 Also, my code may be way off on how to create a combobox during runtime and set it to a specific location, but hopefully you are able to see what I am trying to do.此外,我的代码可能与如何在运行时创建 combobox 并将其设置到特定位置相距甚远,但希望您能够看到我想要做什么。 Thanks in advance.提前致谢。

Interesting question.有趣的问题。

The following code almost achieves what you want, however placing a combobox next to each text line causes problems because there isn't enough space to make the combobox tall enough.以下代码几乎可以实现您想要的,但是在每个文本行旁边放置一个 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

I'm not aware of a way to change the height of a combobox control (someone else here might know if you can) but it's going to look funny anyway.我不知道有什么方法可以改变 combobox 控件的高度(这里的其他人可能知道你是否可以),但无论如何它看起来很有趣。

You might also look into using a RichTextBox control and changing the line spacing.您还可以考虑使用 RichTextBox 控件并更改行距。

Hopefully the code above helps to get you started anyway.希望上面的代码无论如何都能帮助您入门。

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

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