Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / April 2005

Tip: Looking for answers? Try searching our database.

DesignMode in TreeView derived control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeronimo Bertran - 20 Apr 2005 03:07 GMT
Hi
I have  created a treeview derived control which needs to perform some
initialization only at runtime (not at design time) because I am not able
to add it to a form due to he fact that some information required in the
initialization is not available at design time.

How can I do this?

Thanks
Jeronimo
"Jeffrey Tan[MSFT]" - 20 Apr 2005 09:09 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jeronimo Bertran - 20 Apr 2005 22:26 GMT
Got you.... thanks Jefery.
"Jeffrey Tan[MSFT]" - 21 Apr 2005 04:21 GMT
You are welcome!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 20 Apr 2005 09:10 GMT
Sorry, I forget to attach the App.config file's content:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DesignTimeIdentity" value="1" />
</appSettings>
</configuration>

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.