Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts
in last 360 days

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 

I came across interesting issue with one of our controls. I needed a different behavior during design-time and run-time. I was sure that .NET Framework has a nice solution for me, but I found out this is not the case.

There is a DesignMode property but according to several sources it doesn't always work as expected. There seems to be more that one way to solve the issue. I decided to use

System.Diagnostics.Process.GetCurrentProcess().ProcessName

and check if it equals string "devenv". If it does, the instance is running inside Visual Studio, which means it's design-time for our control. For my problem this simple solution worked as a charm.

Posted on Monday, April 11, 2005 11:05 PM | Filed under: Developement |

Feedback

# re: Detecting Design-time in C# 8/11/2005 11:09 PM Christopher W. Miller
What about people who use another IDE? Or possibly even on a non-Windows system (i.e. our Mono using friends). Please use solutions #1 or #2

1.) We could call GetService(typeof(IDesignerHost)) and see if it returns something.

2.) System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime

I prefer #1 as it seems to go straight to the point.

# re: Detecting Design-time in C# 8/12/2005 8:20 AM David
Thanks for your point, Christopher. I like #1.

# re: Detecting Design-time in C# 2/8/2006 7:07 AM manesh
another option

if(this.ParentForm.Parent !=null &&
this.ParentForm.Parent.ToString().Equals("System.Windows.Forms.Design.DesignerFrame+OverlayControl")) return true;


# re: Detecting Design-time in C# 3/13/2008 1:08 PM Alik
#1 do not work in component constructor. You need to move the code to OnLoad.

# re: Detecting Design-time in C# 11/25/2008 7:53 AM bek
#1 seems to be a bad solution since this will not work when using mdi forms or with complex hierarchy of controls.
Moreover, GetService() is a private method of Form class therefore it makes it hard to write an helper for it ;p

#2 did the trick to me,
Thanks a lot for your help,
I hope my comment will help too ;)

# re: Detecting Design-time in C# 6/2/2009 1:36 PM rashed
If Me.DesignMode = False then
'control/form is in Runtime
end if

# re: Detecting Design-time in C# 6/11/2009 9:03 AM Predrag
Thank's rashed

# re: Detecting Design-time in C# 10/8/2009 1:09 PM Ken Guru
@rashed Your solution will not work on user controls embedded in a form. #2 solves this problem for me.

# re: Detecting Design-time in C# 2/9/2010 3:28 PM Carlos Merighe
The solutions above did not work for my inherited form, and I kept getting an error message. To solve the problem, since my (subclassed) form has a connection string property, it is null at design time. So I just do this:

if (ConnectionString == null)
return;

Carlos Merighe.

# re: Detecting Design-time in C# 6/4/2010 12:26 AM Joshua03
Excelente. Gracias!!!

# re: Detecting Design-time in C# 2/27/2011 3:43 AM Guest
This code works well for us:

protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (RunTime())
MessageBox.Show("RunTime", (this.Name + " " + this.GetType().Name).Trim());
else
MessageBox.Show("DesignTime", (this.Name + " " + this.GetType().Name).Trim());
}

private bool RunTime()
{
Form parentForm = this.FindForm();
Control parentControl = this.Parent;
// Start with highest level and work down.
if (parentForm != null)
if (parentForm.Site != null && parentForm.Site.DesignMode)
return false;
else
return true;
else if (parentControl != null)
{
if (parentControl.Site != null && parentControl.Site.DesignMode)
return false;
else
return true;
}
else if (this.Site != null && this.Site.DesignMode)
return false;
else
return true;
}


Comments have been closed on this topic.