【发布时间】:2011-09-28 06:50:29
【问题描述】:
我的问题是 ComboBox,第一个项目的文本比组合长 - 我没有从开头看到文本,我看到了文本的结尾。我想从一开始就看到文字。我尝试将“SelectionStart”属性设置为 0,但它与我的问题无关 - 它只设置从文本中选择的位置。
更长的解释:
我的表单上有几个组合框,我必须调整它们的下拉列表宽度以适合最长的项目。因此,当我打开下拉列表时,它的宽度与最长的项目一样长。但是,有一个问题 - 我有一个特殊的功能,它会遍历组合框列表并将它们的下拉宽度设为最长的项目:
Public Sub MakeDropDownListWider()
Dim conKontrola As ComboBox
conKontrola = Me
'make the dropdown wider so the entire selection can be seen
If conKontrola.Items.Count > 0 Then
Dim pixlength As Graphics = Graphics.FromHwnd(New IntPtr)
Dim lengthHolding As Int32
Dim stringWidth As Int32
Dim g As Graphics = conKontrola.CreateGraphics
For Each myItem As Object In conKontrola.Items
If myItem.GetType().ToString() = "System.Data.DataRowView" Then
lengthHolding = pixlength.MeasureString(myItem.Row.Item(conKontrola.DisplayMember).ToString, conKontrola.Font).ToSize.Width
Else
lengthHolding = g.MeasureString(myItem, conKontrola.Font).Width + 15
End If
If lengthHolding > stringWidth Then
stringWidth = lengthHolding
End If
Next
Dim allowedWidth As Int32 = 0
If Me.Parent.Width > 0 Then
allowedWidth = Me.Parent.Width - conKontrola.Location.X - 10
End If
If allowedWidth > 0 And (stringWidth + 15 > allowedWidth) Then
conKontrola.DropDownWidth = allowedWidth
Else
conKontrola.DropDownWidth = stringWidth + 15 'add 15 for the scrollbar
End If
End If
End Sub
当我在表单的每个组合框上运行此功能时,我的所有组合框都会在表单显示后被选中。 (我在表单显示事件上调用此方法)。我不想选择组合框,所以我使用了组合框的 SelectionStart 属性,如下所示:
myComboBox1.SelectionStart = myComboBox1.Text.Length
这样,似乎没有一个组合框被选中。但是,出现了另一个问题:我在组合框中只看到了选择的结尾。如果第一项很短,那么一切都很酷。但是,如果第一个项目比组合框长,我只能看到项目的结尾 - 但是,我必须从头开始。
所以,f.e.:我的第一个项目是:“C# 是 Anders Hejlsberg 设计的一种非常好的编程语言”,而我的组合比正文短,我只会看到“由 Anders Hejlsberg 设计”。 - 我想看看“C# 是一个非常好的编程”。
我无法将“SelectionStart”属性移至 0,因为我的所有组合框都再次被选中。即使我这样做了,我仍然看到第一个项目的结尾,而不是开始 - 唯一的区别是选择了该项目。
知道如何从头开始查看第一项的文本吗?
【问题讨论】:
标签: .net vb.net visual-studio-2010