Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 4th, 2004, 10:21 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Minor User Control Initialization Problem

I'm having a minor problem in a user control I've written, caused by the fact that the Initialization sequence fires when I drop the control on the form rather than when the form it's on is loading at run time. I assume this is because the IDE needs to fire a Paint event to draw the control in the IDE and this fires the Initilization event. The problem is that this is causing an error in the designer because an object I'm looking for in the initilization code doesn't exist at design time, only at run time. I've wrapped the reference in a try statement which prevents the error from causing serious problems in IDE, so this isn't a big deal.

But I recall there being a state variable you could check in VB6 to see whether the object was being displayed in the IDE GUI at design time or actually running and avoid any user defined initialization except when the code is being run. Does such a variable exist in VB.NET?
  #2 (permalink)  
Old May 4th, 2004, 10:37 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ron,

If your control implements the IComponent interface (which it does when you inherit from Control), you can use this syntax:
Code:
if (this.Site.DesignMode == true)
{
  // Design Mode
}
else
{
  // Run-time
}
I only discovered this recently, and haven't really implemented it yet in my controls (I check for ClientID == null), but I think this should work.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Aqueous Transmission by Incubus (Track 13 from the album: Morning View) What's This?
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #3 (permalink)  
Old May 4th, 2004, 11:59 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

That was what I was looking for but...it hasn't fixed the problem. The IDE just does not like bad references and there appears no way to trap for thenm in a user control. Thanks anyway!
  #4 (permalink)  
Old May 8th, 2004, 08:27 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ron,

After playing around with this a bit, I found out that at run-time this.Site does not exist. So, to successfully detect design-time, you'll need something like this:

if (this.Site != null && this.Site.DesignMode == true)
{
  // Design Mode
}
else
{
  // Run-time
}

With this code, it should be possible to skip the "bad references". That is, declare the variable outside this if block, and assign them a value only in the else clause (run-time).

If this is still an issue for you, can you post some code for us to look at?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #5 (permalink)  
Old May 10th, 2004, 07:38 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Imar,

Thanks for your follow-up. I did try your original suggestion, but removed this.site because this is a Windows application and I assumed this.site would not therefore exist. However, in the meantime, I've corrected the premature reference so that the problem no longer exists, which is no doubt the right response to this issue to begin with. Thanks again for your assistance.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem while using User Control vivekshah ASP.NET 1.0 and 1.1 Professional 0 August 7th, 2008 02:46 AM
Much much problem with the User Control! daffodils ASP.NET 1.0 and 1.1 Basics 7 October 20th, 2006 10:46 AM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
User Control Problem alyeng2000 ASP.NET 1.0 and 1.1 Basics 4 March 19th, 2004 11:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.