在列表框中换行文本 - ms access 2010

Wrap text in a list box - ms access 2010

我在 MS Access 中有一个列表框,可以从查询中检索结果。第四列包含的文本很长,我想知道是否可以添加额外的代码来将文本包装在列中。

下面是一些代码,用于在列表框中加载我的查询结果集

Private Sub Form_Load()

Dim NbrCol As Integer
Dim db As DAO.Database

Set db = CurrentDb()
NbrCol = db.QueryDefs("QryRestructuringToDo").Fields.Count
Restructuring_List.RowSource = ""

Restructuring_List.ColumnCount = NbrCol
Restructuring_List.RowSourceType = "Table/Query"
Restructuring_List.RowSource = "QryRestructuringToDo"
Restructuring_List.ColumnHeads = True
Restructuring_List.ColumnWidths = "1.9cm;4cm;3.25cm;15cm;2cm"

Set db = Nothing

End Sub

我认为没有任何方法可以强制文本在列表框控件中 wrap

这是一个可能的解决方法:设置滚动条 属性 以允许用户将其余文本滚动到视图中。

.ScrollBars = 3 'fmScrollBarsBoth

这是另一种可能的解决方法:劫持表单的 ControlTipText 属性,当用户在列表框中创建 selection 时。一旦你 select 列表中的项目看起来像这样:

Private Sub ListBox1_Change()
Dim i As Long
Dim str As String
For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        str = ListBox1.List(i)
        Exit For
    End If
Next
ListBox1.ControlTipText = str
End Sub

这是另一种方法,它需要一些微调,因为一旦显示 ControlTipText 它似乎没有响应,移动鼠标不会 reset 它。

出于某种原因,屏幕截图不包含鼠标光标,但它看起来是这样的:

Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)


Dim rHeight As Long  'approximate the rowheight?
Dim hoveredRow As Double 'approximate the position of the cursor
Dim str As String

'Debug.Print "X: " & X & ", " & "Y: " & Y

'your mileage may vary, based on font size... this seems to work for font size 8.
rHeight = ListBox1.Font.Size * 1.25

'NOTE modular division
hoveredRow = (Y \ rHeight)
'Debug.Print "Row: " & hoveredRow

'get the text of this item in the listbox
If hoveredRow <= ListBox1.ListCount - 1 Then
    str = ListBox1.List(hoveredRow)
End If

'Display the controlTipText
ListBox1.ControlTipText = str

End Sub