VB中如何在ListBox的记录宽度大于ListBox本身宽度时自动增加横向滚动条,谢谢
谢谢大家的回复,我学VB不是很久,了解不是很多,所以希望大家能够有个针对性很强的答案,答案好的分数我再加100都没问题,我的程序要实现从数据库中读入列表数据,然后判断列表...
谢谢大家的回复,我学VB不是很久,了解不是很多,所以希望大家能够有个针对性很强的答案,答案好的分数我再加100都没问题,我的程序要实现从数据库中读入列表数据,然后判断列表中有没有项目长度超过了List本身长度,如果超过即增加横向滚动,第一个回复显然不够自动,第二个朋友能否解释一下您的程序是意思让文本框的变化确定索引号有什么意义,这里不是很明白,谢谢!第三个朋友的如果在标签中显示了,那List加上滚动也没用啊,若再将数据录入List那就没效率了,再次谢谢大家了
展开
1个回答
2013-09-24
展开全部
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
For i = 1 To 20
List1.AddItem String(100, "0") & i
Next
SendMessage List1.hwnd, &H194, 999, ByVal 0 '999是滚动条的宽度范围,单位为象素
End Sub
=============================================================
更优的代码
'计算出列表框内最长的项目有多少象素的宽度,这样可以使列表框的滚动条调整到一个合适的范围,因为列表框的内容很可能是会变动的,如果生硬的加一个很大的滚动条,同样会使你的程序显得很不专业,所以庖欢问歉�诺拇�耄?
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_SETHORIZONTALEXTENT = &H194
Const DT_CALCRECT = &H400
Public Function ListTextWidth(ByRef lstThis As ListBox) As Long
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long
With lstThis.Parent.Font
.Name = lstThis.Font.Name
.Size = lstThis.Font.Size
.Bold = lstThis.Font.Bold
.Italic = lstThis.Font.Italic
End With
lHDC = lstThis.Parent.hdc
'便历所有的列表项以找到最长的项
For i = 0 To lstThis.ListCount - 1
DrawText lHDC, lstThis.List(i), -1, tR, DT_CALCRECT
lW = tR.Right - tR.Left + 8
If (lW > lWidth) Then lWidth = lW
Next i
'返回最长列表项的长度(像素)
ListTextWidth = lWidth
End Function
'调用代码
Private Sub Command1_Click() '点击Command1会使列表框按当前内容中最长项目的宽度来设置滚动条的范围
'列表框内容有变化时,可以调用这行代码,随时改变滚动条的范围
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, ListTextWidth(List1), 0
'写在这里是为了让你看清楚列表框滚动条的变化
End Sub
'测试数据
Private Sub Form_Load()
Command1.Caption = "点我"
For i = 1 To 100
'添加很长的项目给列表框
List1.AddItem String(i, "0")
Next
End Sub
Private Sub Form_Load()
For i = 1 To 20
List1.AddItem String(100, "0") & i
Next
SendMessage List1.hwnd, &H194, 999, ByVal 0 '999是滚动条的宽度范围,单位为象素
End Sub
=============================================================
更优的代码
'计算出列表框内最长的项目有多少象素的宽度,这样可以使列表框的滚动条调整到一个合适的范围,因为列表框的内容很可能是会变动的,如果生硬的加一个很大的滚动条,同样会使你的程序显得很不专业,所以庖欢问歉�诺拇�耄?
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_SETHORIZONTALEXTENT = &H194
Const DT_CALCRECT = &H400
Public Function ListTextWidth(ByRef lstThis As ListBox) As Long
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long
With lstThis.Parent.Font
.Name = lstThis.Font.Name
.Size = lstThis.Font.Size
.Bold = lstThis.Font.Bold
.Italic = lstThis.Font.Italic
End With
lHDC = lstThis.Parent.hdc
'便历所有的列表项以找到最长的项
For i = 0 To lstThis.ListCount - 1
DrawText lHDC, lstThis.List(i), -1, tR, DT_CALCRECT
lW = tR.Right - tR.Left + 8
If (lW > lWidth) Then lWidth = lW
Next i
'返回最长列表项的长度(像素)
ListTextWidth = lWidth
End Function
'调用代码
Private Sub Command1_Click() '点击Command1会使列表框按当前内容中最长项目的宽度来设置滚动条的范围
'列表框内容有变化时,可以调用这行代码,随时改变滚动条的范围
SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, ListTextWidth(List1), 0
'写在这里是为了让你看清楚列表框滚动条的变化
End Sub
'测试数据
Private Sub Form_Load()
Command1.Caption = "点我"
For i = 1 To 100
'添加很长的项目给列表框
List1.AddItem String(i, "0")
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询