See the top rated post in this thread. Click here

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

  1. #1
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    1

    Default Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    I want to populate a Combobox with every layer in the drawing but I'd like for them to be in alphabetical order. Is there a quick way to do this? Populating it is easy but it appears to pop them into the combobox in the order that they appear..... in the AutoCAD database? Is there ANY way to alphabetize a list? Any help is appreciated.

  2. #2
    Active Member rcrabb's Avatar
    Join Date
    2007-02
    Posts
    99
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    Quote Originally Posted by Coolmo
    I want to populate a Combobox with every layer in the drawing but I'd like for them to be in alphabetical order. Is there a quick way to do this? Populating it is easy but it appears to pop them into the combobox in the order that they appear..... in the AutoCAD database? Is there ANY way to alphabetize a list? Any help is appreciated.
    try this

    or this
    Code:
    Public Sub Alphabetize(StringArray() As String)
    Dim loopOuter As Integer
    Dim loopInner As Integer
    Dim i As Integer
    For loopOuter = UBound(StringArray) To _
    LBound(StringArray) Step -1
    For loopInner = 0 To loopOuter - 1
    If UCase(StringArray(loopInner)) > _
    UCase(StringArray(loopInner + 1)) Then
    Swap StringArray(loopInner), _
    StringArray(loopInner + 1)
    End If
    Next loopInner
    Next loopOuter
    End Sub
    
    Private Sub Swap(a As String, b As String)
    Dim c As String: c = a: a = b: b = c
    End Sub
    or this?

    Code:
    Private Function QSort(strList() As String, lLbound As Long, lUbound As Long)
    'Classic QSort function - Modified to *not* be case sensitive (added all "lcase"
    functions)
    
    Dim sTemp As String
    Dim sBuffer As String
    Dim lCurLow As Long
    Dim lCurHigh As Long
    Dim lCurMid As Long
    
    lCurLow = lLbound 'Start current low and high at actual low/high
    lCurHigh = lUbound
    
    If lUbound <= lLbound Then Exit Function
    lCurMid = (lLbound + lUbound) \ 2 'Find the approx midpoint of the array
    
    sTemp = LCase(strList(lCurMid))
    'Pick as a starting point (we are making an assumption that
    'the data *might* be in semi-sorted order already!)
    
    Do While (lCurLow <= lCurHigh)
    Do While LCase(strList(lCurLow)) < sTemp
    lCurLow = lCurLow + 1
    If lCurLow = lUbound Then Exit Do
    Loop
    Do While sTemp < LCase(strList(lCurHigh))
    lCurHigh = lCurHigh - 1
    If lCurHigh = lLbound Then Exit Do
    Loop
    
    If (lCurLow <= lCurHigh) Then 'if low is <= high then swap
    sBuffer = strList(lCurLow)
    strList(lCurLow) = strList(lCurHigh)
    strList(lCurHigh) = sBuffer
    lCurLow = lCurLow + 1 'CurLow++
    lCurHigh = lCurHigh - 1 'CurLow--
    End If
    Loop
    
    If lLbound < lCurHigh Then 'Recurse if necessary
    QSort strList(), lLbound, lCurHigh
    End If
    
    If lCurLow < lUbound Then 'Recurse if necessary
    QSort strList(), lCurLow, lUbound
    End If
    
    End Function
    one more ...

    Code:
    '------------------------------------
    'Add all layer names to an array
    'Return the sorted array
    '------------------------------------
    Public Function SortLayers() As String()
    Dim i As Integer
    Dim oLayers As AcadLayers, oLayer As AcadLayer
    
    Set oLayers = ThisDrawing.Layers
    ReDim strLayers(0 To oLayers.Count - 1) As String
    
    i = 0
    For Each oLayer In oLayers
    strLayers(i) = oLayer.Name
    i = i + 1
    Next oLayer
    
    SortLayers = MergeSort (strLayers)
    End Function
    
    '----------------------------------
    'Add layers to a combo box named CboLayers
    '----------------------------------
    Private Sub UserForm_Activate()
    Dim strLayers() As String
    Dim i As Integer
    
    strLayers = SortLayers()
    For i = LBound(strLayers) To UBound(strLayers)
    CboLayers.AddItem strLayers(i)
    Next i
    
    End Sub
    hope one of those helps

  3. #3
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    See how it will be works for you

    Code:
    ComboBox1.Sorted = True
    Hth

    ~'J'~

  4. #4
    Active Member rcrabb's Avatar
    Join Date
    2007-02
    Posts
    99
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    Quote Originally Posted by fixo
    See how it will be works for you

    Code:
    ComboBox1.Sorted = True
    Hth

    ~'J'~
    are you sure that is available in VBA? im under the understanding thats only a VB option

  5. #5
    I could stop if I wanted to
    Join Date
    2002-02
    Location
    Kansas
    Posts
    487
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    I use is this function to load data in to a combox

    the function return the index where the item was add to the list

    Code:
     
    Public Function LoadComBox(item As String, Combox As Object)
    Dim myflag As Boolean
    Dim myComBox As ComboBox
    Dim mycount, i
    Dim myIdex
    myflag = False
    myIdex = -1
    If TypeOf Combox Is ComboBox Then
    Set myComBox = Combox
    If myComBox.ListCount = 0 Then
    myComBox.AddItem item
    myIdex = 0
    Else
    For i = 0 To myComBox.ListCount - 1
    	If UCase(item) < UCase(myComBox.List(i)) Then
    	 myIdex = i
    	 myComBox.AddItem item, i
    	 myflag = True
    	 Exit For
    	End If
    Next
    If Not myflag Then
    myComBox.AddItem item
    myIdex = myComBox.ListCount - 1
    End If
    End If
     
    Else
    End If
    LoadComBox = myIdex
    End Function
    to test the code add to a form
    a CommandButton name CommandButton1
    and a ComboBox name ComboBox1
    pase this code in the form code window

    click the command button to add text to the comboxBox

    Code:
    Private Sub CommandButton1_Click()
    Dim x
    x = jwaLoadComBox.LoadComBox(InputBox(xxx), ComboBox1)
    MsgBox CStr(x)
    End Sub
    to put a list layers in combobox1
    Code:
    Private Sub CommandButton1_Click()
    Dim x
    For Each x In ThisDrawing.Layers
     jwaLoadComBox.LoadComBox x.Name, ComboBox1
    Next
    End Sub
    how You have a function to sort all your comboboxs
    Last edited by jwanstaett; 2007-03-27 at 04:01 PM.

  6. #6
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    Quote Originally Posted by rcrabb
    are you sure that is available in VBA? im under the understanding thats only a VB option
    Oh, sorry
    My bad...

    ~'J'~

  7. #7
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    I'm getting an error on:

    SortLayers = MergeSort (strLayers)

    What is MergeSort?
    Is that the function for sorting the array? My VBA doesn't allow it. I literally copied your code an ran it but it keeps stopping on that one. Ideas?

  8. #8
    AUGI Addict MikeJarosz's Avatar
    Join Date
    2015-10
    Location
    New York NY
    Posts
    1,497
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    Quote Originally Posted by Coolmo
    What is MergeSort?
    VBA does not include a sort method. VB does.

    I didn't look too closely, but the options presented above look like bubble sort, quick sort and merge sort. There is also what is called a Shell sort.

    Sorting is a potentially enormous topic of discussion. People get Phd's in this stuff. I won't go into the details here, you can google these sort techniques and find out more about them. For sorting a simple list of layers, a bubble sort (sometimes called a swap sort) is good enough. I use it in many of my routines. There are many VBA routines out there for bubble sorts, as it is the easiest to understand.

    Ignore the snobs that say it is the slowest or least elegant technique. You're not sorting the entire General Motors payroll by social security number. That's what the other algorithms are designed for.

  9. #9
    Active Member
    Join Date
    2002-05
    Posts
    72
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    You can always save the layers into an array then query the array using ORDER BY. I don't remember the exact details, but used this method a few years back to solve a similar problem.

  10. #10
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    0

    Default Re: Populate a Combobox with all the Layers from a DWG, list them in alphabetical order

    bubble sort huh? I'll check it out. Thanks

Page 1 of 2 12 LastLast

Similar Threads

  1. Layer Tool Not in Alphabetical Order
    By CadDog in forum AutoCAD General
    Replies: 2
    Last Post: 2011-03-04, 11:42 PM
  2. Change the “Reference other view” drop-down combobox sort order
    By revit.wishlist1942 in forum Revit Architecture - Wish List
    Replies: 3
    Last Post: 2008-01-25, 03:05 PM
  3. Replies: 2
    Last Post: 2007-01-09, 06:39 AM
  4. Layers not in alphabetical order
    By pgastelum77763 in forum AutoCAD General
    Replies: 4
    Last Post: 2006-08-01, 08:03 PM
  5. Populate List box using For Loop...
    By wpeacock in forum VBA/COM Interop
    Replies: 3
    Last Post: 2005-01-12, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •