A Question About Design Time Support

Azrael

Newcomer
Joined
Feb 19, 2003
Messages
7
Hello One and All,

I have been kicking around a problem (for a bit of fun) for the last few days and haven't made much progress and am wondering if anyone can point me in the correct direction...

My problem: Consider a type that has a custom type property - eg (using an mvc paradigm on a visual component) a tree (the view) which is seperated out from its tree model (the data model).

Now I've rooted around at the MSDN docs at design time support and various other hints but they don't seem to answer my question of being able, to say have an instance of the data model instantiated in a section of code and from the properties viewer at design time set a "model attribute" of the tree to point to that model instance.

eg:

C#:
class Foo
{
      // a simple tree model for this example
      TreeModel model = new TreeModel();

      private void initialize() {
         Tree treeView = new Tree();

         // this is the line that I want the designer to be able to 
         // insert into my code from the properties view.  The
         // properties view should autodiscover that a TreeModel
         // instance has been created in the class already and
         // offer its name in a drop down combo
         treeView.setModel( model );
      }
}

Anyone got any helpful pointers or more clear examples of messing around in the CodeDom, Design time stuff and type converters?

Regards
 
Last edited:
It sounds like you might want to investigate the UITypeEditor class, that's how you provide custom support for dropdown lists for propertes in the propertygrid, where the build-in behaviour isn't enough for what you need.

There are a lot of articles out there that include using UITypeEditors.
 
Greets,

I'd thought as much - but the problem is:

a) hooking into the current source code thats being displayed to grab instance names of that custom type that is defined at the head of the code (which will then be displayed in say a custom uitypedesigner dropdown list) so that in the init method the custom control's custom type property is assigned that handle..

b) injecting that back into the current source code - which I assume will be with the CodeDom although that beast at the moment appears akin to black magic ;).

Thanks
 
Last edited:
I'm still not entirely sure what it is you're trying to do - can you give an example which illustrates it?

Try not to think of it as hooking in to source code - the designer architecture is a little weird at first but it's a lot like doing things at runtime - your form exists, and the controls on it are instantiated and running.
 
Greets,

Apologies if its a bit unclear - I'll give it another bash...

Consider a custom control which is going to be used as the view and controller for some underlying data model.

At design time you have a form which the control gets dropped onto and placed etc.

Now the underlying datamodel (in the earlier example we had a custom tree and a treemodel), is
a) a non visual component and
b) a cusotm type.

The client programmer is going to put a declaration that creates (in the simple case) an instance of that in the form code (won't bother abstracting out a whole document underlying the data that the form is a view of - aka ye olde pDoc*).

The tree has a property called Model eg:

C#:
class Tree {

   // lots of visual drawing code etc...
   private TreeModel model;


   // this will have to be decorated with type designer hints...
   public TreeModel Model {
      get {
         return model;
      }
      set {
         // do some real funky work in here to get the
         // bloomin thing working (I presume at the moment)
      }
   }
}

The property viewer of the custom tree control shows the property "Model" but it is uneditable - because its a custom type etc.

Now the aim is to have design time support (via the UITypeEditor and I presume CodeDom) so that actually, the uieditor can pick up if we have code like this:

C#:
public class ViewForm : System.Windows.Forms.Form {

   // the views tree model that represents the underlying
   // data.  The views tree control needs to have a handle
   // on this.
   private TreeModel custom_model = new TreeModel();
   private Tree      custom_tree = new Tree();

   
   // other windows form code and methods etc...
}

the property grid for the tree control under the Model property will have a selectable intance of custom_model.

In other words its discovered our custom type declaration (and any other if we so wished) so when selected (excuse the ascii art ;) ) we'll see.

Code:
| .. other properties in the display ..    |
|---------------------------------- -------|
|Model   |  none                       | V |
|--------|  custom_model               |---|
|Size    |------------------------------***|
|------------------------------------------|

it will insert a piece of code such as:

C#:
public class ViewForm : System.Windows.Forms.Form {

  // other code

  private void InitializeComponent() {
      // this is inserted by the designer by selecting
      // the discovered instance name of our tree_model
      custom_tree.Model = custom_model;

      // other code
   }
}

The feature is possible without actually having to do any programming in other language IDE's that shall, remain nameless here ;)

The whole crux is that we have a view and a model that need to be linked and it'd be desirable to avoid the necessity of the client programmer having to go enter that assign statement in the InitializeComponent method himself.

Is that any better?

Thanks for your patience...
 
Ok, now I think I get it. I had a feeling you were getting at that before but wasn't sure. Exactly what you're suggesting isn't possible. That is, the designer cannot get access to the underlying code of the form. Imagine, if someone used your component from, say, VB.NET and your designer was trying to parse C#?

I have a better idea though. Create yourself a class that inherits from Component. That means that when it is dropped on to the form it will not be visible, it will be placed in the component tray. Use that class to manage the created models, and act as a container for them. You can expose this collection editing behavior however you like.

Then, in your original custom controls, have a property that allows the user to bind it to a Manager class (that I just described). Once bound, there will be another property that allows the user to select from one of the models exposed in the manager's collection. You will be able to do this by writing a UITypeEditor that shows a dropdown list based on the bound manager's collection.

You could skip having to bind the control to a manager manually by assuming that there will only ever be one manager per form, and walk the components list to find it instead.
 
Back
Top