<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Change Current Layout in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7464879#M29230</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;So, if I was calling it from a class other than my main class, would that be a possible reason for it throwing an exception?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;As&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/529262"&gt;@kerry_w_brown&lt;/a&gt;&amp;nbsp;says, it depends on what your main class may be doing in preparation for using the method, but it should be designed such that it could be called from any class, with the understanding that a document lock would be needed if the caller is running in the application context (e.g., from a click handler of a button on a PaletteSet or a modeless dialog, or a command method that has the CommandFlags.Session flag set).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You also aren't showing the calling context. Where is the method being called from? Is it being called from the handler of a CommandMethod or, a button-click handler on a form or window, or from the handler of some AutoCAD event (the latter of which is the most dangerous calling context).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Without knowing something about the calling context, no one can tell you why your code is failing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other problem is that you're not doing what you can to find out exactly what the cause of the exception is, which is to do something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;try
{
    // Code here that could throw an exception
}
catch(System.Exception ex)
{
    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Oct 2017 20:34:08 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-10-16T20:34:08Z</dc:date>
    <item>
      <title>Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461260#M29221</link>
      <description>&lt;P&gt;I have read many forums on this and I still&amp;nbsp;can't figure out why my code crashes AutoCAD. Some things I can do so simply in LISP are crazy in C#...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; public static void SetCurrentLayoutTab(string tab)
        {
            using (DocumentLock lk = acDoc.LockDocument())
            {
                // Start a transaction in the new database
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    try
                    {
                        LayoutManager.Current.CurrentLayout = tab;
                        acTrans.Commit();
                    }
                    catch
                    {

                        acTrans.Abort();

                    }
                }
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Oct 2017 13:44:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461260#M29221</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-10-15T13:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461349#M29222</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public static void SetCurrentLayoutTab(string tab)
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    using (doc.LockDocument())
        LayoutManager.Current.CurrentLayout = tab;
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Locking the document is only mandatory if the method is called in application context (CommandFlags.Session, modeless dialog, ...).&lt;/P&gt;
&lt;P&gt;In document context, you can simply call:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;LayoutManager.Current.CurrentLayout = tab;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 15:13:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461349#M29222</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-10-15T15:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461354#M29223</link>
      <description>&lt;P&gt;No need to start a transaction to use LayoutManager to change the current layout, and I don't seem to have any problem activating a layout by setting the CurrentLayout property, so regarding&amp;nbsp;the comment in your code (&lt;EM&gt;Start a transaction in the new database) &lt;/EM&gt;&amp;nbsp;what &lt;EM&gt;new database&lt;/EM&gt; would that be?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code references the variables: acDoc and acCurDb, that aren't declared in the method. Are they static fields of your class? If so, I'll guess that's probably why your code is crashing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Locking the document shouldn't have any role in a failure in this case, so you would only have to do that if your code is running in the application context, rather than from the handler of a registered command that doesn't use the CommandFlags.Session flag.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You should also display a message in the catch{} block displaying the exception information so you know what happened.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;I have read many forums on this and I still&amp;nbsp;can't figure out why my code crashes AutoCAD. Some things I can do so simply in LISP are crazy in C#...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; public static void SetCurrentLayoutTab(string tab)
        {
            using (DocumentLock lk = &lt;FONT color="#FF0000"&gt;acDoc&lt;/FONT&gt;.LockDocument())
            {
                &lt;FONT color="#FF0000"&gt;// Start a transaction in the new database&lt;/FONT&gt;
                using (Transaction acTrans = &lt;FONT color="#FF0000"&gt;acCurDb&lt;/FONT&gt;.TransactionManager.StartTransaction())
                {
                    try
                    {
                        LayoutManager.Current.CurrentLayout = tab;
                        acTrans.Commit();
                    }
                    catch
                    {

                        acTrans.Abort();

                    }
                }
            }
        }&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 15:41:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461354#M29223</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-10-15T15:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461401#M29224</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd have thought that if &lt;EM&gt;&lt;FONT color="#FF6600"&gt;acDoc&lt;/FONT&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;FONT color="#FF6600"&gt;acCurDb&lt;/FONT&gt;&lt;/EM&gt; were not declared and assigned a value the compiler would throw a wobbly, rather than allow compilation and cause a runtime error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;added:&lt;/P&gt;
&lt;P&gt;That being said, there a lot of questions on the forums that could be resolved more readily if a little bit more information was included with the original query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 16:17:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461401#M29224</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2017-10-15T16:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461695#M29225</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/529262"&gt;@kerry_w_brown&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd have thought that if &lt;EM&gt;&lt;FONT color="#FF6600"&gt;acDoc&lt;/FONT&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;FONT color="#FF6600"&gt;acCurDb&lt;/FONT&gt;&lt;/EM&gt; were not declared and assigned a value the compiler would throw a wobbly, rather than allow compilation and cause a runtime error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;added:&lt;/P&gt;&lt;P&gt;That being said, there a lot of questions on the forums that could be resolved more readily if a little bit more information was included with the original query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I presumed that acDoc and acCurDb must be static members of the type&amp;nbsp;that declares the method (or a containing type), as that's the only way it would compile, but wanted the pose the question to the OP anyway. The compiler wouldn't complain if the variables/static members aren't assigned a value, and that would trigger a runtime error, which is quite possibly what's going on, or the acDoc and/or acCurDb aren't the active document/database, since that's what the LayoutManager's methods operate on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 21:20:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461695#M29225</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-10-15T21:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461712#M29226</link>
      <description>&lt;P&gt;I do have them as static members, but it hasn't caused this issue until now. Granted, I am not changing anything in CAD up until now. I understand that more information would have been useful, but I have a lot of code and this is just one small portion of it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What about this is causing the crash?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        private static Editor ed = acDoc.Editor;

        //Returns the current autocad reference
        public static Document acDoc
        {
            get
            {
                return Application.DocumentManager.MdiActiveDocument;
            }
        }


        //returns the databse of the active autocad instance
        public static Database acCurDb
        {
            get
            {
                return acDoc.Database;
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Oct 2017 21:36:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7461712#M29226</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-10-15T21:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7462028#M29227</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;I do have them as static members, but it hasn't caused this issue until now. Granted, I am not changing anything in CAD up until now. I understand that more information would have been useful, but I have a lot of code and this is just one small portion of it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What about this is causing the crash?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        private static Editor ed = acDoc.Editor;

        //Returns the current autocad reference
        public static Document acDoc
        {
            get
            {
                return Application.DocumentManager.MdiActiveDocument;
            }
        }


        //returns the databse of the active autocad instance
        public static Database acCurDb
        {
            get
            {
                return acDoc.Database;
            }
        }&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You can put a try{} catch{} block around the entire method body, and in the catch{} block, see what exception is being thrown (display it on the text window using exception.ToString()).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The one issue with the code above is that the Editor shouldn't be cached. You should just get it from the acDoc property when you need it, because if the active document changes, the value of the static field you assigned the Editor to doesn't change. Every Document has its own Editor that must be used when the Document is active.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can change the static field to a property like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public static Editor ed
{
   get
   {
       return acDoc.Editor;
   }
}&lt;/PRE&gt;&lt;P&gt;If there is much more to your code, it may not be possible to tell you what is causing the crash without seeing more of the code.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 03:56:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7462028#M29227</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-10-16T03:56:50Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7462788#M29228</link>
      <description>&lt;P&gt;So, if I was calling it from a class other than my main class, would that be a possible reason for it throwing an exception?&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 10:54:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7462788#M29228</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-10-16T10:54:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7464170#M29229</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;FONT color="#999999"&gt;zdraboin wrote:&lt;/FONT&gt;&lt;BR /&gt;
&lt;P&gt;&lt;FONT color="#999999"&gt;So, if I was calling it from a class other than my main class, would that be a possible reason for it throwing an exception?&lt;/FONT&gt;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It depends on the value and scope of the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say "if I was ... "&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that mean you are ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mentioned several times in this thread, Implement error handling and perhaps be more forthcoming about your implementation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have you tried to debug/trace the code. ?&lt;/P&gt;
&lt;P&gt;Have you made use of the Locals pane in Visual Studio when stepping through code ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 17:27:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7464170#M29229</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2017-10-16T17:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: Change Current Layout</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7464879#M29230</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;So, if I was calling it from a class other than my main class, would that be a possible reason for it throwing an exception?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;As&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/529262"&gt;@kerry_w_brown&lt;/a&gt;&amp;nbsp;says, it depends on what your main class may be doing in preparation for using the method, but it should be designed such that it could be called from any class, with the understanding that a document lock would be needed if the caller is running in the application context (e.g., from a click handler of a button on a PaletteSet or a modeless dialog, or a command method that has the CommandFlags.Session flag set).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You also aren't showing the calling context. Where is the method being called from? Is it being called from the handler of a CommandMethod or, a button-click handler on a form or window, or from the handler of some AutoCAD event (the latter of which is the most dangerous calling context).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Without knowing something about the calling context, no one can tell you why your code is failing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other problem is that you're not doing what you can to find out exactly what the cause of the exception is, which is to do something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;try
{
    // Code here that could throw an exception
}
catch(System.Exception ex)
{
    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 20:34:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-current-layout/m-p/7464879#M29230</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-10-16T20:34:08Z</dc:date>
    </item>
  </channel>
</rss>

