User Control design

bpayne111

Well-known member
Joined
Feb 28, 2003
Messages
326
Location
BFE
Is there a way that i may find out if a control is drawn during run-time or design time?
I was considering some playing around with the sub new method but that wont work.... there has to be to do it somewhere in this framework.
Ive looked aroudn in the Reflection Namespace but have had no luck.

Im trying to create a control design interface when a control i have designed is dropped on the form.
the idea is very simple...
If the control is dropped on a form during design time... give the user an option of of using a custom form designer to dictate the properties for the new control
if the control is added during runtime however i would like this feature dropped an automatically added to a form

the idea is virutaully identical with dropping a DataAdapter on a form and using its form designer.

any ideas to this one will be very appreciated
 
And the DesignMode property is likely what youre looking for, although it wont work in the constructor.
 
are you sure it doesnt work in the constructor? it seems to do fine.. although i havnt tested it yet.
if you are correct do you mind a brief explanation of why this property wont work in Sub New()?

if sub new doesnt work will this defeat its purpose in my case?
all of my initialization occurs in Sub New() and i would like to check the DesignMode property just before i initialize anything.

I think im going to start a divil fan club.
 
The DesignMode property on a component echoes the DesignMode property of the ISite associated with a component. A component only gets an ISite once it has been sited on a surface, which obviously cannot be done until its been instantiated - i.e. after the constructor has run. If the component does not yet have an ISite associated with it, the DesignMode property simply returns false.

If you are wanting to intitialize object only at design time, Id suggest you do so in a designer for the control. Its best to keep things separate like this, that way your control has as little design time code running at runtime as possible.
 
However this doesnt help you with the task to find out *at runtime* whether a control was created at designtime or at runtime.

Since I didnt want to inherit all relevant controls (and add an extra property) I used the .Tag property and "left a note" there, for controls created at runtime.

Anyone with a better solution?
 
I cant for the life of me imagine any situation where one would need to differentiate between the two. The whole point is that they behave exactly the same whether you create them in code or the designer creates the code to create them in code :P
 
We have this situation here. Your life is mine then, hehe.

We have "parameterdriven" controls, which are being read from a database. however, as this will lead to rather bad UI design, the developer may also position the parameterdriven controls on their respective containers at designtime.

now, if the context switches, I need to remove the controls that were added at runtimt and keep those that were added at designtime.

He.he.

Can I have your soul too?
 
All this seems to be getting rather confusing...
Id like to restate my purpose real quick.
During my controls instantiation id like to determine run-time or design-time at that moment... it seems DesignMode wont allow me to do that.
AHHHH BUT WAIT I JUST HAD AN IDEA...
the Paint event is called after Sub New correct?
if this is so... then i may use the following code to produce a designer for my control this way

Overrides Sub Paint(sender as object, e as painteventargs)
If Me.DesignMode = True Then
ShowCustomDesigner
End If

is this the solution to my problem? i think i got it figured out...
but im sure you one of you will shoot it down rather quickly :)

BTW if anyone gets divils life its me... but ill just take his brain and you can have the rest.
 
Hey! Dont I get a say in how Im divided? :P

Anyway - I wouldnt rely on the Paint event for what you need to do. What would probably best is making a designer for your class (see my article, Introduction to Designers) and in the overridden Initialize method of that class, you can call back to the main control to tell it to initialize design-time objects.
 
yes i read that maybe 2 mintues after my post... very interesting..

im glad you posted that site... ill use that in the near future
 
What about the Initialize member in ControlDesign class?
Im gonna play with it for a little bit... i think that might be the one.
Interested in your thoughts/experiences

I noticed i could use a verb to do it but, thats not quite what im looking for. Id like to provide the user with form that lets them choose a from a few options for the setup of my control. I know this seems like something that should be done during run-time... but i feel this would be more user friendly than a giant parameter list.
 
It sounds like a verb could do that quite nicely, Ive seen them used before to pop up configuration boxes. You could also override DoDefaultAction, so when the user double-clicks the control the dialog is popped up.
 
Using Initialize in the Designer Class worked great..
a funny thing happened though... i forgot to code my form that i displayed the first time and there was no control box on it either... so when the form popped up it was stuck on the screen. I had to close VS to get rid of it lol
ohh and im using a verb as well to do it, so the user may do it at either time.
im glad you posted that in your site. it was exactly what i needed
 
I agree that the tutorial makes the concept easy to grasp. I am still having trouble though. I am trying to implement a designer in my own control project that I was already working on. Below is the code that I added to this project to start a designer for it (code for the control is collapsed since it should be unecessary for this example):

Code:
Imports System.Windows.Forms.Design
Imports System.ComponentModel

+  <Designer(GetType(ToolPanelDesigner))> Public Class ToolPanel...

-  Friend Class ToolPanelDesigner
       Inherits ControlDesigner

   End Class


The problem is that "ControlDesigner" is underlined in blue and the tool tip says it is not defined. Everything else seems to be ok. What am I missing? Just for reference I tried cutting and pasting the example code on divils tutorial to a new project and got the same thing. (I am using Enterprise Architect)
 
Ok... that did the trick. LOL

Pretty basic and I have done that type thing before. Now I will know what someone means when they say add an Assembly reference. LOL
 
Back
Top