DesignMode "bug" workaround?

  • Thread starter Thread starter Andreas Håkansson
  • Start date Start date
A

Andreas Håkansson

Hello,

Does anyone know of a working solution to the problem with
Component.DesignMode
returning false when a component is placed on a form during designtime? I
have seen some
VB.NET code to recursivly check the parents for a UserControl, but this
won't work on a
Component since it does not expose the parent property.


//Andreas Håkansson
 
Hi

There is absolutely no problem.
In order to use this property you have to:
declare your ISite in your control like below...:
------------
//declare var
private ISite MyISite;
//override ISite like...
public override ISite Site
{
get { return MyISite; }
set { MyISite = value;} // include this line to see how does it work -
MessageBox.Show(value.ToString());
}
//and then your prop MyISite.DesignMode will be containing a valid value
anytime :)

Jerry
GUI tuning made easy with ToolThemeBox
http://www.pisyslabs.com/ToolThemeBox.aspx
 
Jerry,

I do fail to see why overriding the Site property and setting your own
local
variable would be any different than having the internal state of the
component
be set using the default DesignMode property. The problem is that the Site
is
not set, despite overriding the property. The modification would have to be
made at the calling end of the code and that would mean altering the
designed
behaviour of VS.NET.

One workaround that someone might be able to use in their code is to provide
an override which does the following:

public new bool DesignMode
{
get
{
return (Process.GetCurrentProcess().ProcessName == "devenv");
}
}

However this code would only work in VS.NET, thus preventing the component
from being used in other environements, which support visual designers, such
as
the borland tools and SharpDevelop. To me, this is a totally unacceptable
limitiation
if you are not writing a component for your own use and can ensure that
VS.NET
will be the only tool used to consume the code.

So the question still remains: Is there (there must be, or so one would
think) a more
reliable and environement neutral solution to this ? =)

//Andreas
 
Does anyone know of a working solution to the problem with
Component.DesignMode
returning false when a component is placed on a form during designtime?

You can use a static variable that is set in the Main method. Since the
designer does not call Main() you can determine on the value set of the
variable wheather designmode is active or not.
 
Cody,

True, however would you use this in a component which you would have
other developers use in thier applications? This is a concern I have and I
do
not want to have them "first add a static variable to your Main method
called
ABC and then drop the component on the form". Maybe I am picky, but I
won't go for the "quick hack" without investingating my options =)

//Andreas
 
True, however would you use this in a component which you would have
other developers use in thier applications? This is a concern I have and I
do
not want to have them "first add a static variable to your Main method
called
ABC and then drop the component on the form". Maybe I am picky, but I
won't go for the "quick hack" without investingating my options =)

//Andreas


Oh, you're right. I didn't thought so far..
But there are libraries which requires you to call a InitXYZLibrary(which
could be used to set such a static variable) somewhere in your program so
this idea is not absolutely wrong if you don't find another resulution.
 
Back
Top