Door library (Beta) - Special library

Erel

B4X founder
Staff member
Licensed User
Longtime 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
B4X:
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.
B4X:
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.
B4X:
Sub App_Start
    WebRequest1.New1("http://www.b4x.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.
B4X:
 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):

B4X:
'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:
B4X:
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
 

Attachments

  • Door-0.9.zip
    4 KB · Views: 334
Last edited:

agraham

Expert
Licensed User
Longtime User
After a closer look I have a query.
FromControl (Control As Control) - Gets a reference to a Basic4ppc intrinsic control.
I assume this is also used if the library object does expose a reference (usually with Value or ControlRef).
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.
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!
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 

agraham

Expert
Licensed User
Longtime 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.
 

agraham

Expert
Licensed User
Longtime 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.
B4X:
ofrm.CreateNew2("System.IO.DirectoryInfo,mscorlib", args.Value)
Msgbox(ofrm.RunMethod("ToString"))

But if I try, slightly modified from a sample
B4X:
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:

agraham

Expert
Licensed User
Longtime 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:

agraham

Expert
Licensed User
Longtime 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.
 

Attachments

  • DoorDemo.ZIP
    5.5 KB · Views: 107

BjornF

Active Member
Licensed User
Longtime 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
 

berndgoedecke

Active Member
Licensed User
Longtime 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
 
Top