Hi Jeronimo,
Thanks for your post!!
Based on my understanding, you want to find a way to distinguish the
design-time and runtime, so that you can execute some runtime specific code.
Yes, in the usercontrol inherited control, we can use
Control.Site.DesignMode property in UserControl.Load event to determine if
the control is running at design-time, sample code lists below:
private void UserControl1_Load(object sender, System.EventArgs e)
{
if(this.Site!=null&&this.Site.DesignMode==true)
{
MessageBox.Show("Design time");
}
else
{
MessageBox.Show("Runtime");
}
}
But for TreeView inherited control, there is no Load event, and we can not
use this way in control's constructor, in constructor, Control.Site
property will be null. This is because design time support is not hooked up
until the control is sited. Siting happens after the control is created,
but before any properties are set.
For this issue, I think we can add a App.config file to the project, then
add a design-time appSettings item in this file. Then in the constructor,
we can dynamicallly retrieve this appSettings item. If the user control is
running in devenv(design-time), they will read devenv.exe.config(VS.net
IDE's config file) and the key will be missing. While at runtime, this code
can get the appSettings correctly. Sample code lists below:
public class UserControl1 : System.Windows.Forms.TreeView
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
try
{
System.Configuration.AppSettingsReader configurationAppSettings =
new System.Configuration.AppSettingsReader();
Object obj=configurationAppSettings.GetValue("DesignTimeIdentity",
typeof(int));
if(obj!=null&&(int)obj==1)
{
MessageBox.Show("Runtime");
}
}
catch(Exception ex)
{
MessageBox.Show("Design time");
}
}
}
This works well on my side. Hope it helps.
=================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.