'Renaming layout tab autocad c# .net

the following code is intended to rename the layout tabs in autocad. It renames always the first 2 layout tabs. When I add more tabs before running the program it renames them all but skips layout5. When I run the program again it renumbers them all (including layout5). If after that I add more layouts I get an fatal error: Unhandeld Acces Violation Reading 0xffffffff exception at 1315DFE3h

Can someone point in the right direction or can clue me in what I'm doing wrong? Thanks in advance.

public class LayoutHernummeren
{
    [CommandMethod("LayRenum")]
    public void CmdLayRenum()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor edt = doc.Editor;
        Database db = doc.Database;

        string oldName;
        string newName;

        bool model = true;

        try
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {


                DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;

                foreach (DBDictionaryEntry layoutEntry in layoutDict)
                {

                    Layout layout = trans.GetObject((ObjectId)layoutEntry.Value, OpenMode.ForRead) as Layout;
                    layout.UpgradeOpen();                                                
                    
                    oldName = layout.LayoutName;
                    newName = "blad" + layout.TabOrder.ToString("00");
                  

                    if (oldName != newName)
                    {
                        if (layout.ModelType != model)
                        {
                            LayoutManager.Current.RenameLayout(oldName, newName);
                       
                        }
                    }
                }

                trans.Commit();

            }
        }

        catch(System.Exception ex)
        {
            edt.WriteMessage("\nError >> " + ex.Message);
        }
       
    }
}


Solution 1:[1]

It looks like we need to iterate through all layouts to refresh the TabOrder first. Here's an example.

    [CommandMethod("LayRenum")]
    public void CmdLayRenum()
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        var edt = doc.Editor;
        var db = doc.Database;
        var layoutMgr = LayoutManager.Current;

        try
        {
            using (var trans = db.TransactionManager.StartTransaction())
            {
                var layoutDict = (DBDictionary)trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                var layouts = layoutDict
                    .Cast<System.Collections.DictionaryEntry>()
                    .Select(entry => (Layout)trans.GetObject((ObjectId)entry.Value, OpenMode.ForWrite))
                    .OrderBy(layout => layout.TabOrder)
                    .ToArray();
                for (int i = 1; i < layouts.Length; i++)
                {
                    var layout = layouts[i]; 
                    LayoutManager.Current.RenameLayout(layout.LayoutName, $"blad{i:00}");
                }
                trans.Commit();
            }
        }

        catch (System.Exception ex)
        {
            edt.WriteMessage("\nError >> " + ex.Message);
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1