本帖最后由 dtooboss 于 2010-4-16 14:59 编辑
对,从数据库获取数据, 但上面的代码贴错了(上面的是我改过的),你看看源码吧,在15#
还有,26# ...
superpangya 发表于 2010-4-16 13:57
你的表单宽度是像素,而字串长度是字串个数,所以需要个数乘以字体大小才是像素宽度。我帮你改了下这个函数,字符大小我取了 9 。Func du($tabname)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open("Select " & $T & " From " & $tabmain & " where name = '" & $tabname & "'")
$temp = StringStripWS($RS.Fields(2).value, 2) ;删去字符串中的所有"空白符" 1 char,2 char,3 int,4 char
$arr = StringSplit($temp, ",") ;以指定分隔符把字符串拆分成若干子串
$w = $arr[0]
GUICtrlDelete($ListView1)
Dim $k, $s = '', $c
For $c = 0 To $w - 1
$y = StringSplit($arr[$c + 1], " ")
$k = $k & $y[1] & ","
$s = $s & $y[1] & "|"
Next
$ListView1 = GUICtrlCreateListView("id|" & $s & "", 155, 15, 635, 300)
$s = ""
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open("Select " & $T & " From " & $tabname)
Dim $n
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
For $i = 0 To $w
$numlen = StringLen(StringStripWS($RS.Fields($i).value, 2));得到目前的字串数
If $numlen * 9 > _GUICtrlListView_GetColumnWidth($ListView1, $i) Then ; 如果字串数乘以字体大小的宽度大于目前的宽度则
_GUICtrlListView_SetColumnWidth($ListView1, $i, $numlen * 9) ; 设置目前的宽度为适合字串的宽度
EndIf;结束判断
$n = $n & StringStripWS($RS.Fields($i).value, 2) & "|"
Next
GUICtrlCreateListViewItem(StringTrimRight($n, 1), $ListView1)
$RS.movenext
$n = ""
WEnd
$RS.close
$addfld.Close
EndFunc ;==>du
|