Door library (Beta) - Special library

Discussion in 'Official Updates' started by Erel, Mar 26, 2008.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Edit: Door library v1.0 is included in Basic4ppc v6.30.
    Edit: Door library v0.9 is released. Thanks to Agraham it now supports events.

    The Door library is a special library.
    It allows you to access .Net objects, methods and properties from Basic4ppc code without creating new libraries.
    It won't replace any existing library but rather make these libraries and Basic4ppc itself more complete.
    For example you can use this library to access all the properties of a control (on the desktop or device) and not only the subset supported by Basic4ppc.

    You should have some knowledge with the .Net Framework if you want to fully exploit this library.
    However I hope that in the future many examples and solutions will be available on the forum for everyone to use.

    This is a beta version and it doesn't yet include a proper manual.

    The Door library includes three types of objects:

    Object:

    Holds a reference to an existing or a new .Net object.
    CreateNew (Type As String) - Creates a new static or instance object. No arguments are passed to the constructor.
    CreateNew2 (Type As String, Args As Object()) - Creates a new instance object. Passes the objects array to the constructor.
    FromControl (Control As Control) - Gets a reference to a Basic4ppc intrinsic control.
    FromLibrary (ObjectName As String, FieldName As String, B4PObject2) - Gets a reference to an object which was added from an external library.
    You should only use this method if the object does not expose a reference (usually with Value or ControlRef). FieldName must be known.
    GetProperty (Property As String) As Object - Returns the value of the object's specified property.
    GetProperty2 (Property As String, Index As Int32) As Object - Returns the value of an indexed property.
    New1 (InitializeDataGrid As Boolean) - Initializes the Object control.
    InitializeDataGrid should be set to true if you are using the object with a Table control.
    RunMethod (Method As String) As Object- Runs a method without any arguments.
    RunMethod2 (Method As String, Arg1 As String, Type1 As String) As Object- Runs a method with one argument. The argument cannot be an object.
    RunMethod3 (Method As String, Arg1 As String, Type1 As String, Arg2 As String, Type2 As String) As Object - Runs a method with two arguments. Arguments cannot be objects.
    RunMethod4 (Method As String, Args As Object()) As Object - Runs a method with any number and type of arguments.
    SetProperty (Property As String, Value As String) - Sets the value of the specified property. Value cannot be an object.
    SetProperty2 (Property As String, Value As Object) - Sets the value of the specified property. Value is an object.
    SetProperty3 (Property As String, Value As String, Index As Int32) - Sets the value of the specified indexed property. Value cannot be an object.
    SetProperty4 (Property As String, Value As Object, Index As Int32) - Sets the value of the specified indexed property. Value is an object.
    System_NS, System_Data, System_Drawing, System_Windows_Forms, System_Windows_Forms_Datagrid - Returns the fully qualified named of these assemblies. System_Windows_Forms_Datagrid will only be set if at least one of the objects was initialized with New1(true).
    Value - Gets or sets a reference to the object.

    ObjectArray

    ObjectArray is mostly used to pass arguments to constructors or methods.
    Get (Index As Int32) As Object - Returns the object in the specified index.
    New1 (Size As Int32) - Initializes the array to the specified size.
    SetObject (Index As Int32, Object As Object) - Sets the value of the specified index. The value is an object.
    SetValue (Index As Int32, Value As String, Type As String) - Sets the value of the specified index. Value cannot be an object.
    Value - Gets or sets a reference to the ObjectsArray.

    Event

    Event objects allow you to catch miscellaneous events.
    The following types of events handlers are supported:
    - EventHandler
    - KeyEventHandler
    - KeyPressEventHandler
    - MouseEventHandler
    - PaintEventHandler

    Data - Returns an object which holds the EventArgs object.
    Dispose - Frees the resources of the Event object and removes the event handler.
    New1 (Object As Object, Event As String) - Wires the event sub to the specified object and specified event. See the following examples.
    NewEvent Event - The event that will be raised.

    Examples:
    Change the Form's KeyPreview property to true - All keystrokes will be first handled by Form_KeyPress event:
    Form1 is a regular Form, ofrm is an Object
    Code:
    Sub App_Start
        Form1.Show
        ofrm.New1(
    false)
        ofrm.FromControl(
    "Form1")
        ofrm.SetProperty(
    "KeyPreview",true)
    End Sub
    Desktop only: Change the icon of a specific form.
    Form1 is a regular Form, ofrm and oIcon are Objects, args is an ObjectArray.
    Code:
    Sub App_Start
        Form1.Show
        ofrm.New1(
    false)
        oIcon.New1(
    false)
        ChangeIcon(
    "Form1",AppPath & "\SomeIcon.ico")
        
    'ChangeIcon("Form2",AppPath & "\AnotherIcon.ico")
    End Sub

    Sub ChangeIcon (FormName, IconFile)
        ofrm.FromControl(FormName)
        args.New1(
    1)
        args.SetValue(
    0,IconFile,"System.String")
        oIcon.CreateNew2(
    "System.Drawing.Icon" & oIcon.System_Drawing, args.Value)
        ofrm.SetProperty2(
    "Icon",oIcon.Value)
    End Sub
    Set the WebRequest Referer header's value.
    WebRequest1 is a WebRequest object (HTTP library), obj is an Object.
    Code:
    Sub App_Start
        WebRequest1.New1(
    "http://www.basic4ppc.com")
        obj.New1(
    false)
        obj.FromLibrary(
    "WebRequest1","req",B4PObject(2))
        obj.SetProperty(
    "Referer","http://xxx.xxx.xx")
        
    'Msgbox(obj.GetProperty("Referer"))
    End Sub
    Passing color value.
    Code:
    Sub App_Start
        Form1.Show
        clr.New1(
    false)
        txt.New1(
    false)
        txt.FromControl(
    "textbox1")
        clr.CreateNew(
    "System.Drawing.Color" & clr.System_Drawing)
        txt.SetProperty2(
    "BackColor",clr.RunMethod2("FromArgb",cRed,"System.Int32")) 'Use Color.FromArgb(Int32) to pass the color
    End Sub
    Events examples:
    Handle the TextChanged event (fires whenever the text changes):

    Code:
    'obj is an Object, TextBox1ChangedEvent is an Event.
    Sub App_Start
        Form1.Show
        obj.New1(
    false)
        obj.FromControl(
    "textbox1")
        TextBox1ChangedEvent.New1( obj.Value,
    "TextChanged")
    End Sub

    Sub TextBox1ChangedEvent_NewEvent
        form1.Text = textbox1.Text
    End Sub
    Show which mouse button was pressed:
    Code:
    obj and o are Objects, MouseDownEvent is an Event.
    Sub App_Start
        Form1.Show
        obj.New1(
    false)
        o.New1(
    false)
        obj.FromControl(
    "form1")
        MouseDownEvent.New1( obj.Value,
    "MouseDown")
    End Sub

    Sub MouseDownEvent_NewEvent
        o.Value = MouseDownEvent.Data 
    'Get the event's data.
        form1.Text = o.GetProperty("Button"'Returns Left, Middle or Right
    End Sub
     

    Attached Files:

    Last edited: Apr 4, 2008
  2. agraham

    agraham Expert Licensed User

    :sign0098: This supercedes my ControlProperties library and is much more powerful.

    Can I point out a typo in one of the method signatures :-

    RunMethod (Method As String) As Object - Runs a method without any arguments.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Thanks agraham.
     
  4. agraham

    agraham Expert Licensed User

    After a closer look I have a query.
    I assume this is also used if the library object does expose a reference (usually with Value or ControlRef).
    I am probably being thick but I don't understand what FieldName might be? The only thing that I can think of is that it is the instance variable that most such libraries load with a new instance of whatever object they are instantiating, in which case I assume this variable would have to be Public to work!
     
  5. agraham

    agraham Expert Licensed User

    :sign0013: I didn't really mean to generate this much traffic but there isn't a System_NS property in the library! Which assembly is it meant to reference? I don't know of one called System.NS.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    If the library exposes a reference then you should use: obj.Value = bar.ControlRef, although obj.FromControl(bar.ControlRef) will also work.

    FieldName is the instance variable that holds the .Net object in many libraries.
    Your assumption is logical but not correct. You can access private fields with Reflection. A little but strange, I agree.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    I hope to see a lot of traffic around this library as it is really an interesting one :)

    Download the attached file again. The first uploaded file didn't include it.
    System_NS stands for System.dll.
    It didn't behave properly when it was named System.
     
  8. agraham

    agraham Expert Licensed User

    More traffic! :) - CASE-SENSITIVITY!

    If anyone else is playing with this library, like I am, it might save a bit of frustration to know that, unlike Basic4PPC, the Method and Property names passed to this library are case-sensitive and so must be the exact .NET name or an exception will occur. e.g TabIndex not tabindex or tabIndex etc.
     
  9. agraham

    agraham Expert Licensed User

    I was experimenting to see if I really needed a fully qualified Assembly name for CreateNew2.

    I can successfully make a DirectoryInfo object and invoke a method using just the assembly name for mscorlib.
    Code:
    ofrm.CreateNew2("System.IO.DirectoryInfo,mscorlib", args.Value)
    Msgbox(ofrm.RunMethod("ToString"))
    But if I try, slightly modified from a sample
    Code:
    oIcon.CreateNew2("System.Drawing.Icon,System.Drawing", args.Value)
    I get a "Could not load file or assembly 'System.Drawing" error. Any idea why the difference Erel?

    EDIT :- Note that, undocumented in Erel's first post, you do get the return value of a RunMethod call - I'll wait and see what happens for some of the more esoteric returned objects ;)
     
    Last edited: Mar 26, 2008
  10. agraham

    agraham Expert Licensed User

    TabIndex example

    The hoary old TabIndex problem. :)

    Put the control names in an arraylist (or array) in the tab order you want
    Code:
    For i = 0  To CtlArray.Count - 1
       Obj1.FromControl( CtlArray.Item(i) )
       Obj1.SetProperty (
    "TabIndex",  i )
    Next
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    The CLR searches mscorlib by default.
    You can just write:
    Code:
    ofrm.CreateNew2("System.IO.DirectoryInfo", args.Value)
    (Assuming that System.IO.DirectoryInfo belongs to mscorlib.dll)
    The full name is required for all other libraries:
    Code:
    oIcon.CreateNew2("System.Drawing.Icon" & oIcon.System_Drawing, args.Value)
     
  12. agraham

    agraham Expert Licensed User

    @Erel. Have you thought of adding a way of invoking static methods and acessing static fields?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Fields are not accessible at all.
    Static methods are accessible:
    Code:
    Sub App_Start
        o.New1(
    false)
        o.CreateNew(
    "System.Math")
        x = o.RunMethod3(
    "Max",12,"System.Int32",54,"System.Int32")
        
    Msgbox(x)
    End Sub
     
  14. agraham

    agraham Expert Licensed User

    Thanks Erel. I was so sure that couldn't work I didn't even try it :sign0161: I had assumed that CreateNew would make an instance of a class and so hide the static methods.
     
  15. agraham

    agraham Expert Licensed User

    A couple of problems :(

    a)
    Playing with enums "obj1.SetProperty("FormBorderStyle", "4")" works in the IDE but the optimising compiler throws a "must implement IConvertible" exception.


    b)
    I can't find a use for SetProperty2 and SetProperty4. Within the IDE it always throws an "unable to cast System.Windows.Forms.MainMenu to System.String" exception and, due to B4PPC typing behaviour the optimising compiler compiles it to SetProperty(String, String) not SetProperty(String, Object). Is the intended use to pass objects returned from libraries. Similarly ObjectArray.SetObject?

    EDIT :- I've knocked up a library to return types such as Size and Point and SetProperty2 seems to work with some of them them. Still having trouble with enums though!

    EDIT :-:sign0161: I've cracked enums! I was assuming that I would need to set the numeric equivalent of an enum but actually it needs the String name of the member of the enum. Having trouble with Color now, I can get it but not set it.
     
    Last edited: Mar 28, 2008
  16. agraham

    agraham Expert Licensed User

    A not very good demo of the Door library. However it does contain solutions to the slight difficulties I had with Enum, Char (a bug to be fixed later) and Color. Thanks go to Erel for his patience in helping me out.

    If anyone wants to play but doesn't know how to find the necessary information the .NET 2.0 Class Library docs are here http://msdn2.microsoft.com/en-gb/library/ms229335(VS.80).aspx

    Mostly it will be System.Windows.Forms that you will need. Find, say, the TextBox Class page, scroll to the bottom and click TextBox Members to get a list of the Properties and Methods for a TextBox. Those applicable to the device have a little picture of a device on their left.
     

    Attached Files:

  17. BjornF

    BjornF Active Member Licensed User

    Thank you Erel and agraham, it certainly looks very impressive :sign0188:. I am certain we are many that are looking on and just hesitating before taking the plunge. The web-address with the information has certainly captured my interest!

    all the best / Björn
     
  18. berndgoedecke

    berndgoedecke Active Member Licensed User

    DataGrid with Door.lib

    Hello Erel and agraham,
    I'm very interested in using the DataGrid. Is it possible to get some more usage hints with Door.dll ?

    Best regards

    berndgoedecke
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    The Table control is actually a DataGrid control in the .Net world.
    What exactly do you need?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Door V0.9 is released.
    Events are now supported. See the first post.
     
Loading...