【发布时间】:2016-04-28 08:48:26
【问题描述】:
我正在尝试将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
我的问题是:如何更改给定索引处列表框项的字体?
【问题讨论】:
-
尝试使用 Datagridview。格式化行和单元格更容易
-
是的,但是可以通过
ListBox以相对较低的难度完成。
标签: vb.net fonts listbox listboxitems