在列表框中显示在100附近的Vb6 1000中 [英] In List Box In Vb6 1000 showing next to 100
问题描述
在列表框1000中显示100 ...我可以解决吗?
继续OriginalGriff的解决方案和OP后续评论...
如果将字段id的类型设置为adBigInt并确保记录集在该列上排序(rs.Sort = id
)然后它应该工作。确保你的ListBox Sorted属性设置为False(仅限设计时间)。
以下是一个愚蠢的小例子,我敲了一下来证明这个方法......
Dim rs As New ADODB.Recordset
rs.Fields.AppendCol1,adBigInt
rs.Fields .AppendCol2,adVarChar,30
rs.Open
rs.AddNew
rs.Fields(Col1)= 100
rs.Fields(Col2)= text 100
rs.AddNew
rs.Fields(Col1)= 101
rs.Fields(Col2)=text 101
rs.AddNew
rs.Fields(Col1)= 1
rs.Fields(Col2)=text 1
rs.AddNew
rs.Fields(Col1)= 2
rs.Fields(Col2)=text 2
rs.AddNew
rs.Fields(Col1)= 1000
rs.Fields(Col2)=text 1000
rs.AddNew
rs.Fields(Col1)= 200
rs.Fields(Col2 )=text 200
rs.Sort =Col1
rs.MoveFirst
Do while not rs.EOF
Me.List1.AddItem CStr(rs(Col1) ))++ rs(Col2)
rs.MoveNext
Loop
结果输出
1 text 1
2 text 2
100 text 100
101 text 101
200 text 200
1000 text 1000
OP正在使用我不熟悉的工具但似乎更改数据库中列的数据类型不是一个选项(请注意其他读者 - 始终为您正在存储的数据选择适当的数据类型)。所以相反你可以尝试这样的东西(NB - 在我目前的位置我没有VB6所以下面的代码是完全未经测试的)
Dim rsSort As New ADODB.Recordset
设置rsSort = rs.Clone(adLockUnspecified)
rsSort.Fields.AppendSorter,adBigInt
rsSort.Open
rsSort.MoveFirst
Do while not rsSort.EOF
rsSort.Fields(Sorter)= val(rs.Fields(id)
循环
rsSort.Sort =Sorter然后使用rsSort填充ListBox。请记住在设计时确保List1.Sorted = False并整理新的记录集。如果你尝试这种方法并遇到问题,请尝试google forDisconnectedClone
或者在填充ListBox时将id格式化为左边的零填充,例如
List1.AddItem Format
(Val(rs.Fields(id)),String(5,0))&& rs.Fields(name)这是最简洁的演示(无论如何)意味着名字将排在第一位e显示)。如果你使用这个方法,那么记得设置List1.Sorted = True
简单:你正在使用字符串,所以排序顺序是基于字符的:第一个区别用字符控制排序顺序。
所以,它是:
1
10
11
12
...
19
2
20
21
...100出现同样的问题和$
如果没有你的代码,我无法建议修复,但你需要查看加载ListBox的位置,并使用数值而不是字符串。
in list box 1000 showing next to 100..how can i solve that ??
Further to OriginalGriff's solution and the OPs subsequent comments ...
If you set the type for field "id" to adBigInt and make sure that the recordset is sorted on that column (rs.Sort="id"
) then it should work. Make sure your ListBox Sorted property is set to False (design time only).
The following is a silly little example that I knocked up to prove this method ...
Dim rs As New ADODB.Recordset rs.Fields.Append "Col1", adBigInt rs.Fields.Append "Col2", adVarChar, 30 rs.Open rs.AddNew rs.Fields("Col1") = 100 rs.Fields("Col2") = "text 100" rs.AddNew rs.Fields("Col1") = 101 rs.Fields("Col2") = "text 101" rs.AddNew rs.Fields("Col1") = 1 rs.Fields("Col2") = "text 1" rs.AddNew rs.Fields("Col1") = 2 rs.Fields("Col2") = "text 2" rs.AddNew rs.Fields("Col1") = 1000 rs.Fields("Col2") = "text 1000" rs.AddNew rs.Fields("Col1") = 200 rs.Fields("Col2") = "text 200" rs.Sort = "Col1" rs.MoveFirst Do While Not rs.EOF Me.List1.AddItem CStr(rs("Col1")) + " " + rs("Col2") rs.MoveNext Loop
results in output
1 text 1 2 text 2 100 text 100 101 text 101 200 text 200 1000 text 1000
[EDIT] OP is using a tool I'm not familiar with but it would seem that changing the data type of the column on the database is not an option (note to other readers - always choose the appropriate data type for the data you are storing). So instead you could try something like this (NB - At my current location I do not have VB6 so the following code is completely untested)
Dim rsSort As New ADODB.Recordset Set rsSort = rs.Clone(adLockUnspecified) rsSort.Fields.Append "Sorter", adBigInt rsSort.Open rsSort.MoveFirst Do While Not rsSort.EOF rsSort.Fields("Sorter") = val(rs.Fields("id") Loop rsSort.Sort="Sorter"and then use rsSort to populate the ListBox. Remember to ensure that List1.Sorted = False at Design Time and to tidy up new recordset. If you try this method and you get problems try google for "DisconnectedClone"
Alternatively format the id to pad left with zeroes as the ListBox is being populated e.g.
List1.AddItem Format
(Val(rs.Fields("id")), String(5, "0")) & " " & rs.Fields("name")which is far neater presentation anyway (means the names will be lined up on the display). If you use this method then remember to set List1.Sorted=True
Simple: you are using strings, so the sort order is character based: the first difference in characters controls the sort order.
So, it goes:
1 10 11 12 ... 19 2 20 21 ...And the same problem occurs with 100 and 1000
Without your code I can't suggest a fix, but you need to look at where you load the ListBox, and use numeric values instead of string.
这篇关于在列表框中显示在100附近的Vb6 1000中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!