【问题标题】:Adjusting the auto-complete dropdown width on a textbox调整文本框上的自动完成下拉宽度
【发布时间】:2010-09-14 19:02:14
【问题描述】:

我在使用自定义 AutoCompleteSource 设置的 .NET 2 winforms 应用程序中使用文本框。无论如何通过代码我可以增加包含自动完成建议的列表的宽度?

理想情况下,我希望在不增加文本框宽度的情况下这样做,因为我的 UI 空间不足。

【问题讨论】:

标签: .net winforms .net-2.0 textbox autocomplete


【解决方案1】:

据我所知,您可以自动调整文本框的大小,使其仅在需要时才变宽,而不是始终与最长的文本一样宽。

来自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

结束类

【讨论】:

    【解决方案2】:

    嗯,真的没有直接的方法。您可能不得不求助于子类化(在 Windows API 意义上)TextBox 来做到这一点,即使那样,也有很多猜测要做。

    【讨论】:

      【解决方案3】:

      据我所知,TextBox 类封装了 Windows 附带的完整 AutoComplete API。唉,这一事实不能“移植”到 .NET 框架的其他部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-14
        • 2017-08-06
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 2016-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多