以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  请教:如何根据列名称或列内容自动获得最大列宽?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=6189)

--  作者:yangming
--  发布时间:2010/3/15 13:21:00
--  请教:如何根据列名称或列内容自动获得最大列宽?
有时列名只有两个字,但是列中内容的字却很多,比如:部门列,有可能会有七八个或是十几个字,而合同结束时间,列名长,而内容却只有八位,那么,如何让系统自动根据情况选择列宽呢?如列名小于内容的宽度,就选内容的宽度,而如果内容小于列名,就选列名的宽度?
主要是用在窗口的表中
[此贴子已经被作者于2010-3-15 13:22:07编辑过]

--  作者:mr725
--  发布时间:2010/3/15 13:35:00
--  

MaxLength

如果是字符列,返回该列允许输入的最大长度,否则返回-1。

例如:

Dim Len As Integer
Len =
DataTables("订单").DataCols("产品").
MaxLength
Output.Show(Len)


--  作者:yangming
--  发布时间:2010/3/15 13:39:00
--  
如何获得列宽呢?列宽和字符长度如何对应?
--  作者:mr725
--  发布时间:2010/3/15 13:50:00
--  
Dim Len As Integer
with currenttable
    For Each c As Col In CurrentTable.Cols
        len = datatables(currenttable.name).datacols(c.name).maxlength
        Output.Show(c.Index & "  " & c.name & "    " & len )
    next
end with

这个就是列设置时的允许最大的字符数啊。列宽用Width取得吧。
--  作者:mr725
--  发布时间:2010/3/15 14:21:00
--  
这样都有了::::

Dim Len As Integer
Dim n As Integer
Dim s as string
with currenttable
    For Each c As Col In CurrentTable.Cols
        len = datatables(currenttable.name).datacols(c.name).maxlength
        s = currenttable.Cols(n).Width
        Output.Show(c.Index & "  " & c.name & "    " & len & "  => " & s)  
                    \'\'\'   列序号              列名              列设置的字符长    列宽度)
        n = n + 1
    next
end with
[此贴子已经被作者于2010-3-15 14:21:24编辑过]

--  作者:mr725
--  发布时间:2010/3/15 14:44:00
--  
这个更全啦:  如果都是字符型的列:
Dim Len As Integer
Dim n As Integer
Dim n0 As Integer
Dim s as string
with currenttable
    For Each c As Col In CurrentTable.Cols
        len = datatables(currenttable.name).datacols(c.name).maxlength
        s = currenttable.Cols(n).Width
        for i as integer = 0 to currenttable.count -1
            if currenttable.rows(i)(c.name).length > n0
                n0 = currenttable.rows(i)(c.name).length
            end if
        next
        Output.Show("【列位置: 】" & c.Index & " 【列名:】  " & _
        c.name & " 【列设置的字符长度:】  " & len & "  【列宽:】 " & _
        s & " 【本列已输入的最大字符长:】 " & n0)
        n = n + 1
    next
end with

[此贴子已经被作者于2010-3-15 14:51:40编辑过]

--  作者:yangming
--  发布时间:2010/3/15 15:29:00
--  
还是无法比较,因为maxlength只对字符型有效
谢谢老师!
[此贴子已经被作者于2010-3-15 15:29:33编辑过]

--  作者:yangming
--  发布时间:2010/3/15 15:53:00
--  
我只能是先这样做了:

Dim n,len As Integer
Dim s As Integer
For Each cl As DataCol In DataTables("RS_统计查询_Table2").DataCols
len = datatables("RS_统计查询_Table2").datacols(cl.name).maxlength
s = Tables("RS_统计查询_Table2").Cols(n).PrintWidth
If cl.name.Length < Math.Abs(len)
Tables("RS_统计查询_Table2").Cols(n).PrintWidth = Math.Abs(len)
Else
Tables("RS_统计查询_Table2").Cols(n).PrintWidth = cl.name.Length *5

End If
n = n + 1
Next


--  作者:mr725
--  发布时间:2010/3/15 16:30:00
--  
这样不管是什么列类型:

Dim Len,n As Integer
Dim s as string
with currenttable
    For Each c As Col In CurrentTable.Cols
            len = datatables(currenttable.name).datacols(c.name).maxlength
            s = currenttable.Cols(n).Width
            Dim s1 as string = 0
            dim nn as string
            for i as integer = 0 to currenttable.count -1
                nn = "" & currenttable.rows(i)(c.name) & ""
                if s1 < nn.length
                    s1 = nn.length
                end if
            next
            Output.Show("【列位置: 】" & c.Index & " 【列名:】  " & _
            c.name & " 【列设置的字符长度:】  " & len & "  【列宽:】 " & _
            s & " 【本列已输入的最大字符长:】 " & s1)            
        n = n + 1
    next
end with

--  作者:czy
--  发布时间:2010/3/15 17:29:00
--  
要不用模拟按键试试系统的最佳列宽