按名称对列表框项排序



我设置了一个VBScript hta让我的用户选择默认打印机。我现在正试着让它尽可能清晰。

我们的打印机列表如下:

4th floor
1st floor
Block 2
3rd floor
etc.

有人知道我可以对代码进行更改,以按名称对这些输出进行排序吗?多谢

<html> 
<head>
    <title>Choose your default printer</title> 
<style type="text/css"> 
    body { 
        font-family:Verdana; 
        font-size: 12px; 
        color: #49403B; 
        background: #FFFFFF; 
        text-align: center; 
        } 
</style> 
<SCRIPT Language="VBScript">
    Sub Window_Onload
        window.resizeTo 500,550
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
        Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
        Set re = New RegExp
        re.Pattern = "^\\.*?\"
        For Each objPrinter in colPrinters
            strPrinter = objPrinter.Name
            Set objOption = Document.createElement("OPTION")
            objOption.Text = re.Replace(strprinter, "")
            objOption.Value = strPrinter
            If objPrinter.Default Then objOption.Selected = True
            AvailablePrinters.Add(objOption)
        Next
    End Sub
    Sub SetDefault
        strPrinter = AvailablePrinters.Value
        Set WshNetwork = CreateObject("Wscript.Network")
        WshNetwork.SetDefaultPrinter strPrinter
        Msgbox strprinter & " has been set as your default printer."
    End Sub
</SCRIPT>
Click a printer name to set it as your default<p>
<select size="20" name="AvailablePrinters" onChange="SetDefault"></select>
<p>
Close this window when done

我将使用ArrayListDictionary,如下所示:

Set printerNames = CreateObject("System.Collections.ArrayList")
Set printers     = CreateObject("Scripting.Dictionary")
For Each objPrinter in colPrinters
    name = re.Replace(objPrinter.Name, "")
    printerNames.Add name
    printers.Add name, objPrinter
Next
printerNames.Sort
For Each name in printerNames
    Set objOption = Document.createElement("OPTION")
    objOption.Text = name
    objOption.Value = printers(name).Name
    If printers(name).Default Then objOption.Selected = True
    AvailablePrinters.Add(objOption)
Next

您可以将打印机名称添加到数组中,然后使用简单的冒泡排序。唯一棘手的部分是您还获得了默认打印机。所以你必须在迭代Win32_Printer集合时保存它。

' Create an array to hold the printer names...
ReDim a(colPrinters.count - 1)
' Add each printer name to the array. Also, save the name of the default printer.
Dim i, strDefault
For Each objPrinter In colPrinters
    a(i) = objPrinter.Name
    If objPrinter.Default Then strDefault = objPrinter.Name
    i = i + 1
Next
' Sort the array...
Sort a
For i = 0 To UBound(a)
    ' Create your <option>...
    If a(i) = strDefault Then objOption.Selected = True
Next
' Basic bubble sort...
Sub Sort(a)
    Dim i, j, temp
    For i = UBound(a) - 1 To 0 Step -1
        For j = 0 To i
            If a(j) > a(j + 1) Then
                temp = a(j + 1)
                a(j + 1) = a(j)
                a(j) = temp
            End If
        Next
    Next
End Sub

这是一个代码片段,从标准输入读取,添加到一个断开的记录集,排序记录集,读取和写入标准输出。

With rs
    .Fields.Append "SortKey", 201, 260 
    .Fields.Append "Txt", 201, 5000 
    .Open
    Do Until Inp.AtEndOfStream
        Lne = Inp.readline
        SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3))
        .AddNew
        .Fields("SortKey").value = SortKey
        .Fields("Txt").value = Lne
        .UpDate
    Loop
    If Arg(2) = "a" then SortColumn = "SortKey ASC"
    If Arg(2) = "d" then SortColumn = "SortKey DESC"
    .Sort = SortColumn
    Do While not .EOF
        Outp.writeline .Fields("Txt").Value
        .MoveNext
    Loop
End With

或者你可以使用。net排序数组/集合。

Set sortthing = CreateObject("System.Collections.SortedList")

相关内容

  • 没有找到相关文章

最新更新