我正在使用一个在.NET 2 winforms应用程序中设置了自定义AutoCompleteSource的文本框。是否有一种通过代码增加包含自动完成建议的列表宽度的方法?理想情况下,我希望在不增加文本框宽度的情况下实现这一点,因为UI空间紧张。
我不知道有没有这样的功能,但是你可以自动调整文本框的大小,使其只在需要时才变宽,而不总是与最长的文本一样宽。
示例来自http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3311429&SiteID=1
Public Class Form1
Private WithEvents T As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
T = New TextBox
T.SetBounds(20, 20, 100, 30)
T.Font = New Font("Arial", 12, FontStyle.Regular)
T.Multiline = True
T.Text = "Type Here"
T.SelectAll()
Controls.Add(T)
End Sub
Private Sub T_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles T.TextChanged
Dim Width As Integer = TextRenderer.MeasureText(T.Text, T.Font).Width + 10
Dim Height As Integer = TextRenderer.MeasureText(T.Text, T.Font).Height + 10
T.Width = Width
T.Height = Height
End Sub
结束类