vb的listview添加滚动条
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb的listview添加滚动条相关的知识,希望对你有一定的参考价值。
VB中用listview的report格式的时候由于字段比较多,有14个。但是只能显示8个,剩下的都在最右边。下面也没有滚动条。。
求教高手怎么给listview添加横向和纵向的滚动条啊??
listview有这个属性吗?没找到啊
我设置了滚动条以平面显示怎么还是不行啊??
晕,“准VB专家”我要的是listview的滚动条,不是list的~~~
tanghailin26 朋友。我不是要隐藏列头,是要显示滚动条啊~~~
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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 参考技术A 在listview点击右键--属性,将里面的的隐藏列头勾选上就有了 参考技术B ‘LV为ListView控件
LV.View = lvwReport 显示方式
With LV
.ListItems.Clear
'.FullRowSelect = True
.ColumnHeaders.Clear '''列表的表头
.ColumnHeaders.Add , , "AAAAAAAA", 2000
.ColumnHeaders.Add , , "BBBBBBBB", 1000
.ColumnHeaders.Add , , "CCCCCCCC", 500
.ColumnHeaders.Add , , "DDDDDDDD", 2000
End With
没问题,下面就有滚动条。本回答被提问者采纳 参考技术C 一般是自动添加滚动条,不过将属性flatscrollbar设置为true的时候好像不会自动出现滚动条
listview 如何总让最后一项选中?????
listview中时刻在添加项,如何时刻选中最后一项?为了让滚动条一直向下滚动,要不然后添加的项需要手动向下滚动才可以看见。。
每次绑定后 将listview最后的一项设置为选中就可以了。listview的长度-1就是最后一项 参考技术A listview.selectindex=listview.items.length-1;
以上是关于vb的listview添加滚动条的主要内容,如果未能解决你的问题,请参考以下文章
VB 中如何给listview控件定义或设置垂直滚动条(水平滚动条不要)