code_junkie

DesignMode with Controls

Nov 10th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. System.ComponentModel.DesignMode property
  2.  
  3. System.ComponentModel.LicenseManager.UsageMode property
  4.  
  5. private string ServiceString()
  6. {
  7. if (GetService(typeof(System.ComponentModel.Design.IDesignerHost)) != null)
  8. return "Present";
  9. else
  10. return "Not present";
  11. }
  12.  
  13. public bool IsDesignerHosted
  14. {
  15. get
  16. {
  17. Control ctrl = this;
  18.  
  19. while(ctrl != null)
  20. {
  21. if((ctrl.Site != null) && ctrl.Site.DesignMode)
  22. return true;
  23. ctrl = ctrl.Parent;
  24. }
  25. return false;
  26. }
  27. }
  28. public static bool IsInDesignMode()
  29. {
  30. return System.Reflection.Assembly.GetExecutingAssembly()
  31. .Location.Contains("VisualStudio"))
  32. }
  33.  
  34. if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
  35. {
  36. bla bla bla...
  37. }
  38.  
  39. /// <summary>
  40. /// The DesignMode property does not correctly tell you if
  41. /// you are in design mode. IsDesignerHosted is a corrected
  42. /// version of that property.
  43. /// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
  44. /// and http://decav.com/blogs/andre/archive/2007/04/18/1078.aspx )
  45. /// </summary>
  46. public bool IsDesignerHosted
  47. {
  48. get
  49. {
  50. Control ctrl = this;
  51.  
  52. while (ctrl != null)
  53. {
  54. if ((ctrl.Site != null) && ctrl.Site.DesignMode)
  55. return true;
  56. ctrl = ctrl.Parent;
  57. }
  58. return false;
  59. }
  60. }
  61.  
  62. public bool RealDesignMode()
  63. {
  64. if (Parent is MyBaseUserControl)
  65. {
  66. return (DesignMode ? true : (MyBaseUserControl) Parent.RealDesignMode;
  67. }
  68.  
  69. return DesignMode;
  70. }
  71.  
  72. static bool IsDesignMode(Control control)
  73. {
  74. PropertyInfo designModeProperty = typeof(Component).
  75. GetProperty("DesignMode", BindingFlags.Instance | BindingFlags.NonPublic);
  76.  
  77. while (designModeProperty != null && control != null)
  78. {
  79. if((bool)designModeProperty.GetValue(control, null))
  80. {
  81. return true;
  82. }
  83. control = control.Parent;
  84. }
  85. return false;
  86. }
  87.  
  88. public MyUserControl()
  89. {
  90. InitializeComponent();
  91. m_IsInDesignMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
  92. }
  93.  
  94. private bool m_IsInDesignMode = true;
  95. public bool IsInDesignMode { get { return m_IsInDesignMode; } }
  96.  
  97. /// <summary>
  98. /// Gets a value indicating whether this instance is in design mode.
  99. /// </summary>
  100. /// <value>
  101. /// <c>true</c> if this instance is in design mode; otherwise, <c>false</c>.
  102. /// </value>
  103. protected bool IsDesignMode
  104. {
  105. get { return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime; }
  106. }
Add Comment
Please, Sign In to add comment