8 Replies Latest reply on Mar 21, 2013 1:53 PM by dhollebeek

    Convertion of a Symbol to PBControl

      Hi,

       

       

       

      I'm getting an error when trying to convert a PBObjLib.Symbol to PBSymLib.PBControl in C#.

       

      The error says that the two types cannot be converted. Somebody knows what i'm missing?

       

       

       

      The Code  Example:

       
      m_display.Symbols.Add(PBObjLib.pbSYMBOLTYPE.pbSymbolControl, "myActiveXClass");
      
      foreach (Symbol s in m_display.Symbols)
            if (s.Type == (int) PBObjLib.pbSYMBOLTYPE.pbSymbolControl) 
            {
                   myActiveXClassLib.myActiveXClass _axclass = (myActiveXClassLib.myActiveXClass) ((PBControl)s).Object;
                   MessageBox.Show(_axclass.Created.ToString());
            }
      

       

       

      Thanks in Advance.

        • Re: Convertion of a Symbol to PBControl
          mhamel

          @Orlando: You need to test if you symbol is from the pbSymbolControl category, after you have to cast it into a PBSymLib.PBControl to be able to manipulate it. The PBControl object owns an Object property to manipulate the object from its real type. You need to test the type returned by the Object property before trying to cast it. Thereafter you can proceed with the newly casted object. I provided an example below to show you how.

           

          I hope it helped!

           
          // Make sure to reference the .dll/.tls file containing the ActiveX definition to your project.
               // You need to use the ProgID you have defined with the PhasorDiagram class you have created.          
               m_display.Symbols.Add(PBObjLib.pbSYMBOLTYPE.pbSymbolControl, "PhasorDiagramLib.PhasorDiagram");
               foreach(Symbol s in m_display.Symbols)
               {
                    if(s.Type == PBObjLib.pbSYMBOLTYPE.pbSymbolControl)
                    {
                         // Cast you PBObjLib.Symbol object into a PBSymLib.PBControl               
                         PBSymLib.PBControl pbc = (PBSymLib.PBControl) s;
                         
                         // Get access to your object through its type via the Object property.
                         // Validate if it is of type PhasorDiagramLib.PhasorDiagram
                         if(pbc.Object is PhasorDiagramLib.PhasorDiagram)
                         {
                              PhasorDiagramLib.PhasorDiagram diagram = (PhasorDiagramLib.PhasorDiagram) pbc.Object;
                              
                              // Show creation date.
                              MessageBox.Show(diagram.Created.ToString());
                         }
                         // Remove these comments to test for other type of controls.
                         // elseif(pbc.Object is ...)
                         // {
                         //
                         // }
                    }
               }
          

           

            • Re: Convertion of a Symbol to PBControl

              Thanks Mathieu!

               

              I resolved the error by doing the casting (PBSymbol to PBControl) inside a VB.NET function.

               

              The cast to the PhasorDiagram don't  fully  work, it does the cast but don't  retrieve results from methods calls, only by doing .NET InvokeMember calls to access the  PhasorDiagram methods/properties.

                • Re: Convertion of a Symbol to PBControl
                  dhollebeek

                  If you're using the latest C#, you can use a dynamic variable to cause the binding of method calls to be at run-time vs compile time.

                    • Re: Convertion of a Symbol to PBControl

                      Hi David,

                       

                      Thanks for the tip, i've tried this code:

                       
                      dynamic phasor = ((PBSymLib.PBControl)c).Object;
                      
                      MessageBox.Show(((PhasorDiagramLib.PhasorDiagram) phasor).Created.ToString());
                      

                       But get this by executing it:

                       

                      8712.erro_5F00_acesso_5F00_created.png

                       

                      As i said, by this code i have success by geting the Created property:

                       
                      Type type = typeof(PhasorDiagramLib.PhasorDiagram);
                      Object o = type.InvokeMember("[DispID=65565]",
                      BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty,
                      null,
                      c.Object, 
                      null);
                      MessageBox.Show(((bool)o).ToString());
                      

                      But i don't know why this approach works instead of the first one.