Identifying DesignTime Mode for Forms
-
Hi, Is there any way for differentiating whether a Forms Constructor is called at DesignTime (by the FormsDesigner) or at Runtime. I have some code in a Forms Constructor that should be executed only when the Application is Run, and not when the Form is opened by the Forms Designer. Thanks, Firoz
-
Hi, Is there any way for differentiating whether a Forms Constructor is called at DesignTime (by the FormsDesigner) or at Runtime. I have some code in a Forms Constructor that should be executed only when the Application is Run, and not when the Form is opened by the Forms Designer. Thanks, Firoz
-
If possible, you can add another constructor and those values there. The desingner will (:~ ) allways use the default constuctor, else you can check the DesignTime property, but that can be a bit over the top for something small. Hope this helps :)
Hi leppie, Thanks, leppie wrote: add another constructor That is a great Idea. It works. But, (in my project) this will require code modification in a lot of places. Actually, I have a base form and around 40 forms derived from this base form. It would be wonderful, if I could put some code in the Base Form constructor itself, instead of modifying the constructor of all derived forms. But I liked your Idea, and I will keep it as a last option. leppie wrote: check the DesignTime property I had tried this before, but this property always returns false. Can you put some more light into this (how to use the DesignTime property). Thanks, Firoz
-
Hi leppie, Thanks, leppie wrote: add another constructor That is a great Idea. It works. But, (in my project) this will require code modification in a lot of places. Actually, I have a base form and around 40 forms derived from this base form. It would be wonderful, if I could put some code in the Base Form constructor itself, instead of modifying the constructor of all derived forms. But I liked your Idea, and I will keep it as a last option. leppie wrote: check the DesignTime property I had tried this before, but this property always returns false. Can you put some more light into this (how to use the DesignTime property). Thanks, Firoz
so this does not work?
public MyClass()
{
if (DesignMode)
{
//add some extra stuff
//Add a msgbox to popup maybe???
}
else
{
//do some different stuff, so u can see the difference, change color or something
}
//do normal stuff
}Note you can never check for this yourself... Hope this helps :)
-
so this does not work?
public MyClass()
{
if (DesignMode)
{
//add some extra stuff
//Add a msgbox to popup maybe???
}
else
{
//do some different stuff, so u can see the difference, change color or something
}
//do normal stuff
}Note you can never check for this yourself... Hope this helps :)
leppie wrote: so this does not work? Nope, that doesn't work... Why? Think about it this way; the DesignMode mode property uses the ISite property to do its work. But at that point of your code no properties have been set so ISite is still null which is why DesignMode will always return false. Shortly after code execution has left the constructor for your object, then ISite will be set and DesignMode will return its proper value. James "And we are all men; apart from the females." - Colin Davies
-
leppie wrote: so this does not work? Nope, that doesn't work... Why? Think about it this way; the DesignMode mode property uses the ISite property to do its work. But at that point of your code no properties have been set so ISite is still null which is why DesignMode will always return false. Shortly after code execution has left the constructor for your object, then ISite will be set and DesignMode will return its proper value. James "And we are all men; apart from the females." - Colin Davies
-
leppie wrote: So the only way to go is an overloaded constructor? I don't really like that tactic because it forces the user's code to be modified from what the form designer spits out. If at all possible I would try to delay the creation/use of the stuff that can't be done in DesignMode until the Load event or some other time. Of course this isn't always possible so you are stuck with things like creating a different constructor. James "And we are all men; apart from the females." - Colin Davies
-
leppie wrote: So the only way to go is an overloaded constructor? I don't really like that tactic because it forces the user's code to be modified from what the form designer spits out. If at all possible I would try to delay the creation/use of the stuff that can't be done in DesignMode until the Load event or some other time. Of course this isn't always possible so you are stuck with things like creating a different constructor. James "And we are all men; apart from the females." - Colin Davies
Maybe I'm missing the point here but why not do it in the Form_Load event? Paul
-
Maybe I'm missing the point here but why not do it in the Form_Load event? Paul
I think thats what James is telling us. We should only attempt to get a correct value from DesignMode after the form has been Loaded. Something like this:
bool loaded = false;
protected override void OnLoad(EventArgs e){
loaded = true;
base.OnLoad(e);
}Then we use:
public int SomeProperty {
get {
if (loaded & DesignMode) dosomethingelse();
return someint;
}
}Correct me if I understand this wrong...:~
-
I think thats what James is telling us. We should only attempt to get a correct value from DesignMode after the form has been Loaded. Something like this:
bool loaded = false;
protected override void OnLoad(EventArgs e){
loaded = true;
base.OnLoad(e);
}Then we use:
public int SomeProperty {
get {
if (loaded & DesignMode) dosomethingelse();
return someint;
}
}Correct me if I understand this wrong...:~
leppie wrote: I think thats what James is telling us. Ooops! Missed the change of name part-way down this thread. leppie wrote: Something like this: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. Paul
-
leppie wrote: I think thats what James is telling us. Ooops! Missed the change of name part-way down this thread. leppie wrote: Something like this: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. Paul
Paul Riley wrote: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. I guess thats to do with your own programming style. To me, its easier to override the function calling the event. You mite rather want to utilize the event, but in that case u need to keep track of it.