我正在尝试将listbox
中的所有当前可用项目设置为字体粗体。如果稍后添加了项目,它们将具有默认字体。因此,用户可以看到最初列出的项目和新项目。
从表单构造函数中,我像这样填充我的列表框,这些是添加的初始值。
Private Sub FillLinkedBox(ByVal oReferenceList As String())
' Fill the linked listbox with the current linked items
Debug.Print(oReferenceList.ToString)
lbLinkedParameters.Items.AddRange(oReferenceList)
' Set the font of all the existing items in the listbox
For i As Integer = 0 To lbLinkedParameters.Items.Count - 1
' Set the font to bold for these items.
Next
End Sub
用户完成后我还想在处理时测试项目字体,这样我就可以确定什么是初始值,什么不是(但我也可以将列表保留在内存中并检查是否列出了项目)字体检查不是一个好的选择。
如果发现这个post描述了应该执行此操作的内容,但它基于一个事件;我不明白它是如何运作的。
Dim buttonPressed As Boolean
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If ListBox1.SelectedIndices.Contains(e.Index) And buttonPressed Then
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Green, e.Bounds.X, e.Bounds.Y)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End If
If e.Index = ListBox1.Items.Count - 1 Then
buttonPressed = False
End If
e.DrawFocusRectangle()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
buttonPressed = True
ListBox1.Refresh()
End Sub
我的问题是:如何在给定索引处更改列表框项的字体?
答案 0 :(得分:1)
it's based on an event; and I don't understand how it works.
如果将ListControl(List / Combo)设置为OwnerDraw
,则每次Windows需要绘制项目时都会触发DrawItem
事件;你可以提供代码来改变颜色,字体或做其他事情。
After the user is finished I would also like to test the items font when processing
问题是字体是基于项目的状态。正如您所描述的那样,该项目是原始项目还是已添加项目。这个状态不能从索引中确定,也不能稍后从视觉元素中重新确定该状态,例如 绘制的方式(也不应该)。
所以手头的任务实际上是两个相关的事情:
这将使用您可以添加到任何类的接口。这使得该解决方案对这一局部化案例不太具体。
Public Interface IListItem
Property Selected As Boolean
Function GetDisplayText() As String ' not really needed
End Interface
代码将使用Selected
来跟踪和绘制添加的项目。如果您愿意,可以更改名称,但您还必须更改绘图代码。
Public Class SimpleThing
Implements IListItem
' your stuff
Public Property Name As String
Public Property Value As Int32
Public Property Foo As Int32
Public Property Bar As Single
Public Property Selected As Boolean Implements IListItem.Selected
Public Sub New(n As String, v As Int32,
Optional b As Boolean = True)
Name = n
Value = v
Selected = b
End Sub
Public Function GetDisplayText() As String Implements IListItem.GetDisplayText
Return Name
End Function
Public Overrides Function ToString() As String
Return Name
End Function
End Class
GetDisplayText
是在ListControl中显示项目与其他情况时允许不同的文本输出。这不是一个艰难的要求。
SimpleThing
可以存储您需要的所有相关信息,ListBox
/ ComboBox
绘图仅依赖于IListItem
元素。您可以使用List作为DataSource
:
ListThings = New List(Of SimpleThing)
...
ListThings.Add(New SimpleThing("Able", 42, True))
ListThings.Add(New SimpleThing("Baker", 7, True))
...
ListThings.Add(New SimpleThing("Ziggy", 6, True))
lbEnabler.DataSource = ListThings
lbEnabler.DisplayMember = "Name"
lbEnabler.ValueMember = "Value"
UI控件设置为OwnerDrawFixed
并使用此代码绘制项目:
Private Sub lst_DrawItem(sender As Object,
e As DrawItemEventArgs) Handles lbEnabler.DrawItem
Dim lb As ListBox = CType(sender, ListBox)
If e.Index < 0 Then
TextRenderer.DrawText(e.Graphics, "", lb.Font, e.Bounds, lb.ForeColor)
Return
End If
Dim bg As Color = If(e.State.HasFlag(DrawItemState.Selected),
SystemColors.Highlight, SystemColors.Window)
Dim fg As Color = If(e.State.HasFlag(DrawItemState.Selected),
SystemColors.HighlightText, SystemColors.WindowText)
Dim iItem As IListItem
If TypeOf lb.Items(e.Index) Is IListItem Then
iItem = CType(lb.Items(e.Index), IListItem)
Else
TextRenderer.DrawText(e.Graphics, lb.Items(e.Index).ToString, lb.Font, e.Bounds,
fg, bg, TextFormatFlags.Left Or
TextFormatFlags.VerticalCenter)
Return
End If
e.DrawBackground()
If iItem.Selected Then ' change to IsOrginal or IsBold
Using f As New Font(lb.Font.FontFamily, lb.Font.Size, FontStyle.Bold)
TextRenderer.DrawText(e.Graphics, iItem.GetDisplayText(), f, e.Bounds,
fg, bg, TextFormatFlags.Left Or
TextFormatFlags.VerticalCenter)
End Using
Else
' default
TextRenderer.DrawText(e.Graphics, iItem.GetDisplayText(), lb.Font, e.Bounds,
fg, bg, TextFormatFlags.Left Or
TextFormatFlags.VerticalCenter)
End If
End Sub
结果:
就个人而言,我不认为这是一个非常好的演讲。它是非标准的, Bold 代表的并不是立竿见影的。但是,您可以在DrawItem
事件中执行任何操作,例如添加图形指示符:
绿色加号版本清楚地表明添加了哪些版本。获取哪些项目也只是轮询项目集合:
Dim added = myListBox.Items.
Cast(Of SimpleThing).
Where(Function(f) f.Selected).
ToArray()
答案 1 :(得分:0)
创建字体变量并自定义ListBox
中的任何项目,其中e.Index
是您要修改的项目编号
Dim useFont as Font = e.Font
Select Case e.Index
Case 0
'Set Font to Bold
useFont = New Font(e.Font, FontStyle.Bold)
End Select
' Draw the current item text based on the current
' Font and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
useFont, e.Brush, e.Bounds, StringFormat.GenericDefault)