Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToolStripControlHost hosting a UserControl designer Serializing won't occur

I am currently developing an application in which I want to display a UserControl inside a context menu. I was able to (somewhat achieve this using ToolStripControlHost). Shown in (NumericUpDownToolStripItem code): below is code for the object (written in VC++.net 2.0). Note: There are semi-similar SO questions on this, but none seem to be dealing with serializing usercontrols, just standard object in the usercontrols.

Shown following the object is the code for the actual usercontrol, which is a usercontrol with a label, and a numericupdown control.

The problem: When I load the designer for my application, I can add my NumericUpDownToolStripItem just fine, however, when I open up the use the exposed propertly to edit my usercontrol, None of that data is serialized into the InitializeComponent method of my NumericUpDownToolStripItem object. The effect of this is my control loads with all defaults at runtime. And every time I reload my form, the modifications are lost.

I have tried using the TypeConverter tutorial located On Msdn, but it didn't work properly. Everything compiled just fine, except my object became completely greyed out in the design grid (just the accessor property, not the entire menupic). Another problem I noticed is that this method isn't particularly designed for UserControls, which may have several different modifiable properties, and can't possibly have an overload for each.

So, I have the following questions:

  1. Is what I'm doing practical, or is my structure way-off the norms. I'm sure there are a lot of redundancy in the attributes.
  2. What is the correct method to serialize a usercontrol 'child' contained in another UserControl\toolstriphost 'parent'. Any properties in 'child' are simple values (Strings, Decimals, etc).
  3. When the TypeConverter class Isn't implemented, every time I changed a property (a labels text for instance), the painting of the object would get all jacked up and behave strangely, until I releaded the context\menu or form. Is there a proper way to inform the designer to repaint because I've made a change? (I used invalidate which has been dodgy at best).

Thanks in advance. I'm going to continue to research this and keep the question updated.

NumericUpDownToolStripItem Class:
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability::All)]
    public ref class NumericUpDownToolStripItem : public ToolStripControlHost
    {
       public: 
       [DesignerSerializationVisibility(DesignerSerializationVisibility::Content | 
          DesignerSerializationVisibility::Visible)]
       property LabeledNumericUpDown ^LabeledNumericUpDownControl
       {
         LabeledNumericUpDown ^get() { return (LabeledNumericUpDown^)this->Control; }
       }

       public: NumericUpDownToolStripItem(void) : 
          ToolStripControlHost(gcnew LabeledNumericUpDown()) {}

       protected: void OnSubscribeControlEvents(Control ^control) new  { //irrelevant to question }
       protected: void OnUnsubscribeControlEvents(Control ^control) new { //irrelevant to question }       
    };

public ref class LabeledNumericUpDown : public UserControl
{
   public: [ DesignerSerializationVisibility(DesignerSerializationVisibility::Content | 
    DesignerSerializationVisibility::Visible)]
   property String ^DisplayText {
      String ^get() {
         return this->label->Text;
      }
      void set(String ^val) {
         if(this->label->Text != val)
         {
            this->label->Text = val;
            this->Invalidate();
         }
      }
   }

//constructor
//destructor
//initiailecomponent
};
like image 529
greggorob64 Avatar asked Nov 06 '22 19:11

greggorob64


1 Answers

Well, after much searching, I found my answer. My methodology was just fine, except for one major problem: I didn't need typeconverters at all. My problem was the need for a custom CodeDomConverter. Shown below is my solution.

    generic<typename T>
    ref class CustomCodeDomSerializer : CodeDomSerializer
    {
    public: virtual Object ^Deserialize(IDesignerSerializationManager ^manager, Object ^codeObject) override
       {
          // This is how we associate the component with the serializer.
          CodeDomSerializer ^baseClassSerializer = (CodeDomSerializer^)manager->
             GetSerializer(T::typeid->BaseType, CodeDomSerializer::typeid);

           //This is the simplest case, in which the class just calls the base class
           //   to do the work. 
          return baseClassSerializer->Deserialize(manager, codeObject);
       }

       public: virtual Object ^Serialize(IDesignerSerializationManager ^manager, Object ^value) override
       {
           //Associate the component with the serializer in the same manner as with
           //   Deserialize 
          CodeDomSerializer ^baseClassSerializer = (CodeDomSerializer^)manager->
             GetSerializer(T::typeid->BaseType, CodeDomSerializer::typeid);

          Object ^codeObject = baseClassSerializer->Serialize(manager, value);

           //Anything could be in the codeObject.  This sample operates on a
           //   CodeStatementCollection. 
          if (dynamic_cast<CodeStatementCollection^>(codeObject))
          {
             CodeStatementCollection ^statements = (CodeStatementCollection^)codeObject;

             // The code statement collection is valid, so add a comment.
             String ^commentText = "This comment was added to this Object by a custom serializer.";
             CodeCommentStatement ^comment = gcnew CodeCommentStatement(commentText);
             statements->Insert(0, comment);
          }
          return codeObject;
       }

};




////////////////////////////////////////////////////////////////////////////////////////////////////
///   <summary>   
///   This Usercontrol is a simple label coupled with a numericupdown.  The class following
///   it will wrap this item in toolstrip container so that it can be part of a contextmenu
///   </summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
[DesignerSerializer(CustomCodeDomSerializer<LabeledNumericUpDown^>::typeid, CodeDomSerializer::typeid)]
public ref class LabeledNumericUpDown : UserControl
{
   public: event EventHandler ^NumericUpDownValueChanged;

   public: [Category("Custom Information"), Description(L"Text to display"), 
            DefaultValue(L"Default Text"), Browsable(true), Localizable(true), NotifyParentProperty(true)]
   property String ^DisplayText
   {
      String ^get()
      {
         return this->label->Text;
      }
      void set(String ^val)
      {
         this->label->Text = val;
         if(this->DesignMode || 
            LicenseManager::UsageMode == LicenseUsageMode::Designtime) 
            this->Invalidate();

      }
   }
  //designer stuff not important
}




[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability::All),
 ToolboxBitmap(::NumericUpDown::typeid)]
public ref class NumericUpDownToolStripItem : ToolStripControlHost
{
   //replace this type
   private: LabeledNumericUpDown ^_Control;

   public: [Category("Object Host"), Description(L"Hosted usercontrol object"), 
    DisplayName("UserControl Object"), Browsable(true), NotifyParentProperty(true),
    DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
    //replace this properties type
   property LabeledNumericUpDown ^UserControlObject
   {
     //replace this properties return type
     LabeledNumericUpDown ^get() { return this->_Control; }
   } 

   public: NumericUpDownToolStripItem(void) : 
      System::Windows::Forms::ToolStripControlHost(gcnew FlowLayoutPanel())
    { 
      //replace this constructor type
      _Control = gcnew LabeledNumericUpDown();

      //don't touch this
      FlowLayoutPanel ^thePanel = (FlowLayoutPanel ^)this->Control;
      thePanel->BackColor = Color::Transparent;
      thePanel->Controls->Add(_Control);
   }   
};
like image 70
greggorob64 Avatar answered Nov 10 '22 01:11

greggorob64