Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Finally! A Way To Determine If Your (VS) Control Is Being Used In Design Mode - Sort Of... (Or, At Least, To Tell It That It Is!) [modified]

Finally! A Way To Determine If Your (VS) Control Is Being Used In Design Mode - Sort Of... (Or, At Least, To Tell It That It Is!) [modified]

Scheduled Pinned Locked Moved Clever Code
visual-studiodesignalgorithmshelp
10 Posts 6 Posters 29 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grcook00
    wrote on last edited by
    #1

    I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

    Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

    <CategoryAttribute("Design"), DefaultValueAttribute(True), _
    DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
    DesignOnlyAttribute(True)> _
    Public Property DesignModeIsActive() As Boolean
    Get
    DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
    End Get
    Set(ByVal value As Boolean)
    If value <> Me.DesignModeIsActive Then
    If MsgBox("The current process (" & _ProcessName & ") has" & _
    IIf(value, " not", "").ToString & " been selected a

    G J C M L 5 Replies Last reply
    0
    • G grcook00

      I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

      Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

      <CategoryAttribute("Design"), DefaultValueAttribute(True), _
      DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
      DesignOnlyAttribute(True)> _
      Public Property DesignModeIsActive() As Boolean
      Get
      DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
      End Get
      Set(ByVal value As Boolean)
      If value <> Me.DesignModeIsActive Then
      If MsgBox("The current process (" & _ProcessName & ") has" & _
      IIf(value, " not", "").ToString & " been selected a

      G Offline
      G Offline
      gumi_r msn com
      wrote on last edited by
      #2

      I'm not sure I get it. When would you want to use this and for what and not use Control.DesignMode?

      modified on Saturday, January 22, 2011 4:52 PM

      G 1 Reply Last reply
      0
      • G gumi_r msn com

        I'm not sure I get it. When would you want to use this and for what and not use Control.DesignMode?

        modified on Saturday, January 22, 2011 4:52 PM

        G Offline
        G Offline
        grcook00
        wrote on last edited by
        #3

        I have a control that has different visual appearances and functionality depending on whether or not it is on the design surface or in an executable. The Component.DesignMode property is less than useless; even when it works, it only shows whether my control is currently being designed, not whether the overall solution is. I have had a need for this information in the past but never have been able to get it automatically; here I can use this code in any component/application that I create where this status is required information. I admit that it is really a workaround, and that it isn't automatic either. It is based on some workaround code that I found in various forums (ie: to check if the System.Diagnostics.Process.GetCurrentProcess().ProcessName = "devenv"); however, I found that in VB Express 2010 the current process is, in fact, "vbexpress". I suspect that in other languages/versions/platforms it may be something else; this method enables the programmer in any given situation to tell his or her control when it is in a designer. It also persists that information at the control level (or, rather, at the project level that contains the control - allowing the programmer to store other components that need this info in the same dll, so all can share the persisted list of designer process names). Once implemented, simply use the UI PropertyGrid to set the DesignModeIsActive() boolean property to True (to store the current process as a designer), or to False (if it becomes necessary to remove the process from the list for some reason). Then, within the component's code, reference the property's value as needed to determine the design status. I hope that this clarifies my intent, and that it can help others who have found themselves with a similar need. I know that I will be using it often in my own code.

        Life is full of challenges - face them head on, and don't sweat the small stuff!

        modified on Saturday, January 22, 2011 8:42 PM

        G L 2 Replies Last reply
        0
        • G grcook00

          I have a control that has different visual appearances and functionality depending on whether or not it is on the design surface or in an executable. The Component.DesignMode property is less than useless; even when it works, it only shows whether my control is currently being designed, not whether the overall solution is. I have had a need for this information in the past but never have been able to get it automatically; here I can use this code in any component/application that I create where this status is required information. I admit that it is really a workaround, and that it isn't automatic either. It is based on some workaround code that I found in various forums (ie: to check if the System.Diagnostics.Process.GetCurrentProcess().ProcessName = "devenv"); however, I found that in VB Express 2010 the current process is, in fact, "vbexpress". I suspect that in other languages/versions/platforms it may be something else; this method enables the programmer in any given situation to tell his or her control when it is in a designer. It also persists that information at the control level (or, rather, at the project level that contains the control - allowing the programmer to store other components that need this info in the same dll, so all can share the persisted list of designer process names). Once implemented, simply use the UI PropertyGrid to set the DesignModeIsActive() boolean property to True (to store the current process as a designer), or to False (if it becomes necessary to remove the process from the list for some reason). Then, within the component's code, reference the property's value as needed to determine the design status. I hope that this clarifies my intent, and that it can help others who have found themselves with a similar need. I know that I will be using it often in my own code.

          Life is full of challenges - face them head on, and don't sweat the small stuff!

          modified on Saturday, January 22, 2011 8:42 PM

          G Offline
          G Offline
          gumi_r msn com
          wrote on last edited by
          #4

          Nice! Thanks for the detailed explanation.

          1 Reply Last reply
          0
          • G grcook00

            I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

            Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

            <CategoryAttribute("Design"), DefaultValueAttribute(True), _
            DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
            DesignOnlyAttribute(True)> _
            Public Property DesignModeIsActive() As Boolean
            Get
            DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
            End Get
            Set(ByVal value As Boolean)
            If value <> Me.DesignModeIsActive Then
            If MsgBox("The current process (" & _ProcessName & ") has" & _
            IIf(value, " not", "").ToString & " been selected a

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Don't you think it's worthy writing an article about it. You're more than halfways there already.

            List of common misconceptions

            1 Reply Last reply
            0
            • G grcook00

              I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

              Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

              <CategoryAttribute("Design"), DefaultValueAttribute(True), _
              DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
              DesignOnlyAttribute(True)> _
              Public Property DesignModeIsActive() As Boolean
              Get
              DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
              End Get
              Set(ByVal value As Boolean)
              If value <> Me.DesignModeIsActive Then
              If MsgBox("The current process (" & _ProcessName & ") has" & _
              IIf(value, " not", "").ToString & " been selected a

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              perhaps it's worthy of an entry in Tips & Tricks. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              L 1 Reply Last reply
              0
              • G grcook00

                I have a control that has different visual appearances and functionality depending on whether or not it is on the design surface or in an executable. The Component.DesignMode property is less than useless; even when it works, it only shows whether my control is currently being designed, not whether the overall solution is. I have had a need for this information in the past but never have been able to get it automatically; here I can use this code in any component/application that I create where this status is required information. I admit that it is really a workaround, and that it isn't automatic either. It is based on some workaround code that I found in various forums (ie: to check if the System.Diagnostics.Process.GetCurrentProcess().ProcessName = "devenv"); however, I found that in VB Express 2010 the current process is, in fact, "vbexpress". I suspect that in other languages/versions/platforms it may be something else; this method enables the programmer in any given situation to tell his or her control when it is in a designer. It also persists that information at the control level (or, rather, at the project level that contains the control - allowing the programmer to store other components that need this info in the same dll, so all can share the persisted list of designer process names). Once implemented, simply use the UI PropertyGrid to set the DesignModeIsActive() boolean property to True (to store the current process as a designer), or to False (if it becomes necessary to remove the process from the list for some reason). Then, within the component's code, reference the property's value as needed to determine the design status. I hope that this clarifies my intent, and that it can help others who have found themselves with a similar need. I know that I will be using it often in my own code.

                Life is full of challenges - face them head on, and don't sweat the small stuff!

                modified on Saturday, January 22, 2011 8:42 PM

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Nice Explanation.

                Regards, Koushik. Most people never run far enough on their first wind to find out if they've got a second. Give your dreams all you've got and you'll be amazed at the energy that comes out of you.

                1 Reply Last reply
                0
                • C Chris Meech

                  perhaps it's worthy of an entry in Tips & Tricks. :)

                  Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Well Said Chris.

                  Regards, Koushik. Most people never run far enough on their first wind to find out if they've got a second. Give your dreams all you've got and you'll be amazed at the energy that comes out of you.

                  1 Reply Last reply
                  0
                  • G grcook00

                    I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

                    Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

                    <CategoryAttribute("Design"), DefaultValueAttribute(True), _
                    DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
                    DesignOnlyAttribute(True)> _
                    Public Property DesignModeIsActive() As Boolean
                    Get
                    DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
                    End Get
                    Set(ByVal value As Boolean)
                    If value <> Me.DesignModeIsActive Then
                    If MsgBox("The current process (" & _ProcessName & ") has" & _
                    IIf(value, " not", "").ToString & " been selected a

                    M Offline
                    M Offline
                    Monjurul Habib
                    wrote on last edited by
                    #9

                    Nice detail.

                    1 Reply Last reply
                    0
                    • G grcook00

                      I have tried for a long time to be able to write controls that are deterministic based on whether or not they are in DesignMode (not for the control itself, but for the project in general). I have finally come up with a method that achieves this result. While searching the internet, I have found a number of 'solutions' to my problem, but they seldom worked (and were not reliable across many platforms - especially since I use VS Express to create my applications). I believe that the answer lies in whether or not the process that is running at the topmost level is a designer application; to that end, I submit the following for review: First of all, your control must be created in its own class library (.dll). This allows for the second requirement: a user setting to determine if the current process is a development (designer) application. All that remains after that is to include a bit of code, adjust a value in the designer interface (if necessary), and then reference the "DesignModeIsActive()" property to determine if, in fact, your project is in design mode. For the first requirement, all you need do is create a new Class Library project solution (or, add a Class Library project to your current solution). Secondly, right click on the project, select Properties -> Settings, and create a String/User entry named "DesignTimeProcesses" (this will ultimately contain the processes that are the designer interfaces that affect your control, but for now it can remain blank*). *If you wish to preload the known DesignTimeProcesses, you can define them in the user property string, beginning with and separated by forward slashes (eg: "/vbexpress/vcsexpress", without the quotation marks). After this, all that remains is to include the following code snippet (or its equivalent in the appropriate language) in your control class definition: For Visual Basic:

                      Shared _ProcessName As String = Diagnostics.Process.GetCurrentProcess().ProcessName

                      <CategoryAttribute("Design"), DefaultValueAttribute(True), _
                      DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden), _
                      DesignOnlyAttribute(True)> _
                      Public Property DesignModeIsActive() As Boolean
                      Get
                      DesignModeIsActive = My.Settings.DesignTimeProcesses.Contains("/" & _ProcessName)
                      End Get
                      Set(ByVal value As Boolean)
                      If value <> Me.DesignModeIsActive Then
                      If MsgBox("The current process (" & _ProcessName & ") has" & _
                      IIf(value, " not", "").ToString & " been selected a

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      The C# code is actually wrong, C# doesn't support the My namespace (it is automatically generated by VB.NET).

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups