I am a college student without much vb.net experience and need some help on a personal project. 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. Also, simultaneously I am trying to generate a combobox next to each textbox line created by hitting enter. 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. 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.
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.
You might also look into using a RichTextBox control and changing the line spacing.
Hopefully the code above helps to get you started anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.