c# - .NET (non-visual) component -


i need create non-visual component, foocomponent, management controls of type bar resides in form.

i have following constraints:

  1. the foocomponent can added forms.
  2. only 1 foocomponent per form allowed.
  3. foocomponent should register form closing event, , when fires , function on bar's , sent e.cancel value based on returned values.

#1 , #2 above should enforced on run-time design time. #3 event registration should made automatically , not foocomponent's users.

i searched google , msdn , read component , componentdesigner classes, didn't find rescue.

what should do?

(1) control component can added form, use foocomponent constructor passed form, , don't define default constructor. it's called like:

foocomponent component = new foocomponent(this); 

where component created within form itself. not-defining default constructor, this:

foocomponent component = new foocomponent(); 

will not compile.


(2) expose foocomponent property on form itself, , in constructor of foocomponent, set passed form's foocomponent this.


(3) same thing, in constructor foocomponent, register closing event form passed


put , get:

public class myform : form {     public foocomponent ownedcomponent { get; set; } }   public class foocomponent {      public foocomponent (myform ownerform) {         ownerform.ownedcomponent = this;         ownerform.formclosing += mycallback;     }      private void mycallback(object sender, formclosingeventargs e) {         ...     }  } 



edit
unfortunately, if need default constructor, , if has true drop-on-the-form component, there's no way enforce component created on form, or form has 1 instance of component (not within component, anyway).

the problem twofold:
(1) dropping component doesn't add component form, adds form's components collection. if handle parent/owner, never form.

(2) neil pointed out, dropping component onto form calls default constructor, passes no parameters, and, of course, none of component's properties (such site or container) populated.


possibly helpful: component can designed notified when created in couple of ways:

(1) implementing constructor takes icontainer parameter. when component dropped on form, generated code call constructor, instead. however, @ runtime, not design time. container handle form's components collection.

public foocomponent(icontainer container) {...} 

(2) implementing isupportinitialize. when component dropped on form, generated code additionally call begininit() , endinit(). in endinit(), can access properties such site , container. again, you'll @ runtime, not designtime, , throwing exception here won't stop component being created.

old, excellent articles on components , controls msdn magazine michael weinhardt , chris sells.
april 2003 building windows forms controls , components rich design-time features
may 2003 building windows forms controls , components rich design-time features, part 2

these .chm files. need unblock in file's property page enable reading contents after downloading.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -