Properties, Events, and Methods (PEM) and .NET controls: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
Line 68: Line 68:




If the existing functionality of a given control does not completely meet your  
If the existing functionality of a given control does not completely meet your needs, then you may '''customize''' this control's behavior by adding your own code. Suppose you want a MonthCalendar control with event notifications and the ability to add events. For that, you would need to create a control, which '''includes''' a MonthCalendar '''and''' other controls and functions which you will use to create your custom Calendar.
 
needs, then you may '''customize''' this control's behavior by adding your own  
 
code. Suppose you want a MonthCalendar control with event notifications and the  
 
ability to add events. For that, you would need to create a control, which  
 
'''includes''' a MonthCalendar '''and''' other controls and functions which you  
 
will use to create your custom Calendar.




Line 85: Line 75:
Visual C# 2008 http://www.microsoft.com/express/vcsharp/#webInstall
Visual C# 2008 http://www.microsoft.com/express/vcsharp/#webInstall


At some point, the installer will ask you whether you’d also like to install
At some point, the installer will ask you whether you’d also like to install Microsoft SQL Server Express. You can install it if you want, but it is not necessary for this presentation.
 
Microsoft SQL Server Express. You can install it if you want, but it is not  
 
necessary for this presentation.


'''Creating a control library'''
'''Creating a control library'''


Game plan: create a control containing a Calendar, an "Add Event" button,  
Game plan: create a control containing a Calendar, an "Add Event" button, combo boxes for hour and minute selection, a text box for entering an event description, and a timer which will check every 2 seconds whether the user should be notified of an upcoming event.
 
combo boxes for hour and minute selection, a text box for entering  
 
an event description, and a timer which will check every 2 seconds whether
 
the user should be notified of an upcoming event.
 




1) Open Visual C#
1) Open Visual C#


2) Open the File menu and choose '''New Project'''
2) Open the File menu and choose '''New Project'''


[[Image:newProjectIcon.JPG]]
[[Image:newProjectIcon.JPG]]


 
3) When the '''New Project''' dialog pops up, from '''Templates''' choose '''Class Library''' and type in the project name you want to use.
 
3) When the '''New Project''' dialog pops up, from '''Templates''' choose
 
'''Class Library''' and type in the project name you want to use
 


[[Image:ProjectType.JPG]]
[[Image:ProjectType.JPG]]


4) The following code will be auto generated:
4) The following code will be auto generated:


[[Image:AutoCode.JPG]]
[[Image:AutoCode.JPG]]


 
5) In the upper right corner you can find the '''Solution Explorer''', which lists all the classes in your project.
 
5) In the upper right corner you can find the '''Solution Explorer''',  
 
which lists all the classes in your project


[[Image:SolutionExplorer.JPG]]
[[Image:SolutionExplorer.JPG]]


6) We will not use Class1, so right – click on it and '''Delete'''.
6) We will not use Class1, so right – click on it and '''Delete'''.


 
7) To add a control to this project, right – click on the project in the '''Solution Explorer''', choose '''Add''', and then choose '''User Control'''.
 
7) To add a control to this project, right – click on the project  
 
in the '''Solution Explorer''', choose '''Add''', and then choose '''User Control'''
   
   
[[Image:AddControl.JPG]]
[[Image:AddControl.JPG]]


 
8) Rename the control from Control1.cs to a meaningful name. I chose '''DateTimeControl.cs'''.
 
8) Rename the control from Control1.cs to a meaningful name.
 
I chose '''DateTimeControl.cs'''
 


[[Image:RenameNewControl.JPG]]
[[Image:RenameNewControl.JPG]]
   
   
9) You should now see a blank control
9) You should now see a blank control


[[Image:EmptyControl.JPG]]
[[Image:EmptyControl.JPG]]


10) Make the control bigger
10) Make the control bigger


[[Image:BiggerControl.JPG]]  
[[Image:BiggerControl.JPG]]  


11) Open the '''View''' menu and choose '''Toolbox'''
11) Open the '''View''' menu and choose '''Toolbox'''


[[Image:ViewToolbox.JPG]]
[[Image:ViewToolbox.JPG]]


 
12) In the '''Toolbox''', under '''Common Controls''', find '''MonthCalendar''' and drag it onto your blank control.
 
12) In the '''Toolbox''', under '''Common Controls''', find  
 
'''MonthCalendar''' and drag it onto your blank control.
   
   
(before)
(before)


[[Image:CalendarInToolbox.JPG]]
[[Image:CalendarInToolbox.JPG]]


(after)
(after)


[[Image:CalendarOnControl.JPG]]
[[Image:CalendarOnControl.JPG]]
   
   
13) Open the '''View''' menu and choose '''Properties''' Window
13) Open the '''View''' menu and choose '''Properties''' Window
   
   
[[Image:ViewProperties.JPG]]
[[Image:ViewProperties.JPG]]


14) On the right, the '''Properties Window''' will pop up. Find the '''Modifier''' property and change its value to '''Public'''
14) On the right, the '''Properties Window''' will pop up. Find the '''Modifier''' property and change its value to '''Public'''


[[Image:CalendarProperties.JPG]]
[[Image:CalendarProperties.JPG]]
   
   
Now we are ready to add our customization.
Now we are ready to add our customization.


15) Let's make our control wider and drag a '''button''' onto it from the '''Toolbox'''
15) Let's make our control wider and drag a '''button''' onto it from the '''Toolbox'''


(before)
(before)


[[Image:ButtonInToolbox.JPG]]
[[Image:ButtonInToolbox.JPG]]


(after)
(after)


[[Image:ButtonInControl.JPG]]
[[Image:ButtonInControl.JPG]]


16) In the '''Properties''' window, change the '''Text''' property to '''Add Event'''
16) In the '''Properties''' window, change the '''Text''' property to '''Add Event'''


[[Image:AddEventBtn.JPG]]
[[Image:AddEventBtn.JPG]]


17) Now we'll add 2  '''ComboBox''' controls
17) Now we'll add 2  '''ComboBox''' controls


(before)
(before)


[[Image:ComboBoxInToolbox.JPG]]
[[Image:ComboBoxInToolbox.JPG]]


(after)
(after)


[[Image:ComboBoxOnControl.JPG]]
[[Image:ComboBoxOnControl.JPG]]


 
18) Now we will give values 0 through 23 to the first ComboBox (HOURS) and values 00 through 59 to the second ComboBox (MINUTES).  
 
18) Now we will give values 0 through 23 to the first ComboBox (HOURS)
 
and values 00 through 59 to the second ComboBox (MINUTES).  


Select the first ComboBox, then press the small arrow in its upper right corner.  
Select the first ComboBox, then press the small arrow in its upper right corner.  


When the '''ComboBox Tasks''' dialog comes up, press '''Edit Items'''
When the '''ComboBox Tasks''' dialog comes up, press '''Edit Items'''.
 


[[Image:AddItemsToCombo.JPG]]
[[Image:AddItemsToCombo.JPG]]


When the '''String Collection Editor''' comes up, type in 0 - 23, one per line.
When the '''String Collection Editor''' comes up, type in 0 - 23, one per line.


[[Image:HourStrings.JPG]]
[[Image:HourStrings.JPG]]


Similarly, give the 2nd ComboBox values 00 - 59.
Similarly, give the 2nd ComboBox values 00 - 59.


19) Now let's add a '''TextBox''' for event description
19) Now let's add a '''TextBox''' for event description


(before)
(before)


[[Image:TextBoxInToolbox.JPG]]
[[Image:TextBoxInToolbox.JPG]]


(after)
(after)


[[Image:TextBoxOnControl.JPG]]
[[Image:TextBoxOnControl.JPG]]


20) Now we'll add a '''Timer'''.   
20) Now we'll add a '''Timer'''.   


The Timer is '''NOT''' a component that will be visible on our custom control.
The Timer is '''NOT''' a component that will be visible on our custom control.


(before)
(before)


[[Image:TimerInToolbox.JPG]]
[[Image:TimerInToolbox.JPG]]


(after)
(after)


[[Image:TimerInControl.JPG]]
[[Image:TimerInControl.JPG]]


Note that the Timer shows up not on the control, but '''BELOW''' it.  
Note that the Timer shows up not on the control, but '''BELOW''' it.  


Yet the Timer is still '''tied''' to our CalendarControl
Yet the Timer is still '''tied''' to our CalendarControl


21) Now '''double-click''' on the '''Add Event''' button.
21) Now '''double-click''' on the '''Add Event''' button.


 
This should generate an '''event handler''' stub for what happens when this button is clicked while the CalendarControl is running.
This should generate an '''event handler''' stub for what happens  
 
 
when this button is clicked while the CalendarControl is running
 


[[Image:ButtonEventHandler.JPG]]
[[Image:ButtonEventHandler.JPG]]


22) Now we are ready to write some '''CODE'''.
22) Now we are ready to write some '''CODE'''.
Line 340: Line 219:


This would actually be a very '''short''' program if all the comments were removed.
This would actually be a very '''short''' program if all the comments were removed.


[[Image:CalendarControlConstructor_.JPG]]
[[Image:CalendarControlConstructor_.JPG]]
Line 349: Line 227:


[[Image:CalendarEvent_.JPG]]
[[Image:CalendarEvent_.JPG]]


23) Open the Build menu and choose Build Solution
23) Open the Build menu and choose Build Solution
   
   
[[Image:BuildSolution.JPG]]
[[Image:BuildSolution.JPG]]


24) By default, all your projects are saved in
24) By default, all your projects are saved in
Line 365: Line 238:
Navigate to '''pem_library\pem_library\bin\Release'''.
Navigate to '''pem_library\pem_library\bin\Release'''.
Copy pem_library.dll to the directory where Business Rules resides.
Copy pem_library.dll to the directory where Business Rules resides.


25) The following BR program uses our control:
25) The following BR program uses our control:
Line 382: Line 253:
  01300 STOP  
  01300 STOP  
  01400 IGNORE: CONTINUE
  01400 IGNORE: CONTINUE


'''To get PEM examples and source code, download ftp://ftp.ads.net/Dll_Distr/pem_demo/pem_demo.zip'''
'''To get PEM examples and source code, download ftp://ftp.ads.net/Dll_Distr/pem_demo/pem_demo.zip'''

Revision as of 16:27, 10 April 2013

See also: Properties Events and Methods

At the Fall 2008 Business Rules! Conference, Gordon Dye introduced the capability of Business Rules! to use .NET controls as an interactive part of any BR window.

This provides for extending Business Rules!, using graphically appealing controls that are unavailable in BR, and possibly overcoming some of its limitations.

System Requirements

BR Version:

BR 4.2 or later.

Windows components:

1) Windows 2000, or later.

For Windows 2000, install Microsoft .NET Framework 2.0. Link: [1]

For Windows XP or later, install Microsoft .NET Framework 3.5. Link: [2]

2) vcredist_x86.exe - Link: [3]

BR Directory:

1) pemnet.dll - acts as the main bridge between .NET and BR. Link: [4]

2) brconvert.dll - this library converts BR strings and numeric variables to and from .NET data types, such as DateTime.

This library can be changed to convert BR data to user-defined data types.

You may get the compiled library here: [5] or the source code here: [6]

Using Existing .NET controls

.NET controls are contained in a library called System.Windows.Forms.dll.

To use their public methods and properties, you do not need to write any non-BR code.

Simply copy

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll

into your BR directory.

MonthCalendar Example

To get PEM examples and source code, download ftp://ftp.brulescorp.com/Dll_Distr/pem_demo/pem_demo.zip

The MonthCalendar control is contained in System.Windows.Forms.dll.

00100 PRINT NEWPAGE
00200 DIM PROPERTIES$(2)*500,ARGUMENTS$(1)*80
00300 DIM METHODS$(1)*255
00400 OPEN #0: "rows=40,cols=110",DISPLAY,OUTPUT 
00500 OPEN #10:  "srow=11,rows=17,scol=20,cols=67,border=S",DISPLAY,OUTPUT 
00600 PRINT #10, FIELDS "2,11, component 7/16,independent":"System.Windows.Forms:System.Windows.Forms.MonthCalendar", MAT PROPERTIES$
00700 PRINT #10, FIELDS "11,11,C 6,,B1000" : "Exit"
00800 DO 
00900    INPUT FIELDS "14,30,C 1", WAIT=1: DUMMY$ TIMEOUT IGNORE
01000    Print #10, Fields "13,11,C 50" : Get$("#10,2,11","SelectionRange.Start")
01100    IF FKEY=1000 THEN EXIT DO 
01200 LOOP 
01300 STOP 
01400 IGNORE: CONTINUE

Creating Custom .NET controls with Visual C#

To get PEM examples and source code, download ftp://ftp.brulescorp.com/Dll_Distr/pem_demo/pem_demo.zip


If the existing functionality of a given control does not completely meet your needs, then you may customize this control's behavior by adding your own code. Suppose you want a MonthCalendar control with event notifications and the ability to add events. For that, you would need to create a control, which includes a MonthCalendar and other controls and functions which you will use to create your custom Calendar.


Additional System Requirements:

Visual C# 2008 http://www.microsoft.com/express/vcsharp/#webInstall

At some point, the installer will ask you whether you’d also like to install Microsoft SQL Server Express. You can install it if you want, but it is not necessary for this presentation.

Creating a control library

Game plan: create a control containing a Calendar, an "Add Event" button, combo boxes for hour and minute selection, a text box for entering an event description, and a timer which will check every 2 seconds whether the user should be notified of an upcoming event.


1) Open Visual C#

2) Open the File menu and choose New Project

3) When the New Project dialog pops up, from Templates choose Class Library and type in the project name you want to use.

4) The following code will be auto generated:

5) In the upper right corner you can find the Solution Explorer, which lists all the classes in your project.

6) We will not use Class1, so right – click on it and Delete.

7) To add a control to this project, right – click on the project in the Solution Explorer, choose Add, and then choose User Control.

8) Rename the control from Control1.cs to a meaningful name. I chose DateTimeControl.cs.

9) You should now see a blank control

10) Make the control bigger

11) Open the View menu and choose Toolbox

12) In the Toolbox, under Common Controls, find MonthCalendar and drag it onto your blank control.

(before)

(after)

13) Open the View menu and choose Properties Window

14) On the right, the Properties Window will pop up. Find the Modifier property and change its value to Public

Now we are ready to add our customization.

15) Let's make our control wider and drag a button onto it from the Toolbox

(before)

(after)

16) In the Properties window, change the Text property to Add Event

17) Now we'll add 2 ComboBox controls

(before)

(after)

18) Now we will give values 0 through 23 to the first ComboBox (HOURS) and values 00 through 59 to the second ComboBox (MINUTES).

Select the first ComboBox, then press the small arrow in its upper right corner.

When the ComboBox Tasks dialog comes up, press Edit Items.

When the String Collection Editor comes up, type in 0 - 23, one per line.

Similarly, give the 2nd ComboBox values 00 - 59.

19) Now let's add a TextBox for event description

(before)

(after)

20) Now we'll add a Timer.

The Timer is NOT a component that will be visible on our custom control.

(before)

(after)

Note that the Timer shows up not on the control, but BELOW it.

Yet the Timer is still tied to our CalendarControl

21) Now double-click on the Add Event button.

This should generate an event handler stub for what happens when this button is clicked while the CalendarControl is running.

22) Now we are ready to write some CODE.

To get PEM examples and source code, download ftp://ftp.ads.net/Dll_Distr/pem_demo/pem_demo.zip

Replace the above code with the code below.

This would actually be a very short program if all the comments were removed.

23) Open the Build menu and choose Build Solution

24) By default, all your projects are saved in My Documents\Visual Studio 2008\Projects In your Projects directory.

Navigate to pem_library\pem_library\bin\Release. Copy pem_library.dll to the directory where Business Rules resides.

25) The following BR program uses our control:

00100 PRINT NEWPAGE
00200 DIM PROPERTIES$(2)*500,ARGUMENTS$(1)*80
00400 OPEN #0: "rows=40,cols=110",DISPLAY,OUTPUT 
00500 OPEN #10: "srow=11,rows=17,scol=20,cols=67,border=S,caption=Custom Calendar",DISPLAY,OUTPUT 
00600 PRINT #10, FIELDS "2,11, component 9/35,independent": "pem_library:pem_library.CalendarControl", MAT PROPERTIES$
00700 PRINT #10, FIELDS "12,11,C 6,,B1000" : "Exit"
00800 DO 
00900    INPUT FIELDS "14,30,C 1", WAIT=3: DUMMY$ TIMEOUT IGNORE
01100    IF FKEY=1000 THEN EXIT DO 
01200 LOOP 
01300 STOP 
01400 IGNORE: CONTINUE

To get PEM examples and source code, download ftp://ftp.ads.net/Dll_Distr/pem_demo/pem_demo.zip