Highlighted
Absent Member.
Absent Member.
1384 views

COBOL class calling WinForms

Jump to solution

Hi, 

In most of the example i found that Winform is loaded from the main programs and on the events of the Controls ( Like Button) call to COBOL program or the invoking of obj for COBOL class is made. here we can pass the values between the Form and Cobol by using reference in procedure division.

Can we call a Winform from the COBOL class or program and how would we pass the values between them ? Below is the code where in i want to load the Cobol Class first and then invoke Winform inside it and then access the values of the Form control from the COBOL Class? 

Class:

class-id CircleWithMessageBox.Main.

method-id Main static
attribute System.STAThread.
local-storage section.
01 mainForm type CircleWithMessageBox.Form1.
procedure division.

set mainForm to new CircleWithMessageBox.Form1()
invoke type System.Windows.Forms.Application::Run(mainForm)
goback.

end method.

end class.

Form code:

$set sourceformat(free) preservecase ilusing(System.Windows.Forms)
class-id CircleWithMessageBox.Form1 is partial
inherits type System.Windows.Forms.Form.

method-id NEW.
procedure division.
invoke self::InitializeComponent
goback.
end method.

method-id btnExit_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
invoke self::Close()
end method.

method-id btnCalculate_Click final private.

Local-storage Section.
01 lo-radius pic 99.
01 lo-radius-X
redefines lo-radius pic xx.
01 lo-circumference pic 999V9.
01 lo-circle-area pic 99999V9.
01 lo-edit-circumference pic ZZ9.9.
01 lo-edit-circumference-X
redefines lo-edit-circumference pic X(5).
01 lo-edit-circle-area pic ZZ,ZZ9.9.
01 lo-edit-circle-area-X
redefines lo-edit-circle-area pic x(8).

procedure division using by value sender as object e as type System.EventArgs.

*> Process radius entry
Move self::txtRadius::Text to lo-radius-X
If lo-radius > 50
Invoke type MessageBox::Show
("Radius entry greater than 50" "WARNING")
End-if
Evaluate True
When lo-radius-X = " "
Move " 0" to self::txtRadius::Text
Move 0 to lo-radius
When lo-radius-X(2:1) = " "
Move lo-radius-X(1:1) to lo-radius
*> When other
*> Move lo-radius-X to lo-radius
End-Evaluate *>True
Call "circle-calculations" using
lo-radius
lo-circumference, lo-circle-area
Move lo-circumference to lo-edit-circumference
Move lo-edit-circumference-X to self::lblCircumference::Text

Move lo-circle-area to lo-edit-circle-area
Move lo-edit-circle-area-X to self::lblArea::Text

Invoke self::txtRadius::Focus
Invoke self::txtRadius::SelectAll
end method.


method-id Form1_Load final private.
procedure division using by value sender as object e as type System.EventArgs.
end method.

end class.

Main :

class-id CircleWithMessageBox.Main.

method-id Main static
attribute System.STAThread.
local-storage section.
01 mainForm type CircleWithMessageBox.Form1.
procedure division.

set mainForm to new CircleWithMessageBox.Form1()
invoke type System.Windows.Forms.Application::Run(mainForm)
goback.

end method.

end class.

0 Likes
2 Solutions

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: COBOL class calling WinForms

Jump to solution

A Windows Form is a .NET class just like any other class and you can create an instance of a Windows Form and invoke its methods from within a non-Forms class without a problem.

The properties of controls within a Forms class are marked as private by default which means that they cannot be accessed directly from outside of the class itself. If you wish to share data between a WinForm and its invoking class you can do so in a number of ways. You can define public properties in the working-storage of the WinForm class and access this data from outside the class. You could create a special Data Object containing parameters and pass an instance of this into the WinForm so that you can populate it with data and access it from outside of the class. You can simply create methods in the WinForm class to pass parameters back and forth as required.

The following is an example of how to display a WinForm from a COBOL class and then get the data entered in a Textbox back to the invoking class. This example uses a property get and set to access the data within the textbox.

       class-id WinFormAccess.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       property-id textParam string.
       setter.
          set textBox1::Text to property-value.
       getter.
          set property-value to textBox1::Text
       end property.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke self::Close
       end method.
       
       end class.

The class that invokes the Windows Form and passes parameters thru a property:

       class-id WinFormAccess.myClass.
       working-storage section.
       01 mainForm type WinFormAccess.Form1.
       01 myText  string  value "From myClass".
       method-id showForm.
       local-storage section.
       procedure division.
           set mainForm to new Form1
           set mainForm::textParam to myText
           invoke mainForm::ShowDialog
           set myText to mainForm::textParam
           goback.
       end method.

       end class.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: COBOL class calling WinForms

Jump to solution
0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: COBOL class calling WinForms

Jump to solution

A Windows Form is a .NET class just like any other class and you can create an instance of a Windows Form and invoke its methods from within a non-Forms class without a problem.

The properties of controls within a Forms class are marked as private by default which means that they cannot be accessed directly from outside of the class itself. If you wish to share data between a WinForm and its invoking class you can do so in a number of ways. You can define public properties in the working-storage of the WinForm class and access this data from outside the class. You could create a special Data Object containing parameters and pass an instance of this into the WinForm so that you can populate it with data and access it from outside of the class. You can simply create methods in the WinForm class to pass parameters back and forth as required.

The following is an example of how to display a WinForm from a COBOL class and then get the data entered in a Textbox back to the invoking class. This example uses a property get and set to access the data within the textbox.

       class-id WinFormAccess.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       property-id textParam string.
       setter.
          set textBox1::Text to property-value.
       getter.
          set property-value to textBox1::Text
       end property.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           goback.
       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
           invoke self::Close
       end method.
       
       end class.

The class that invokes the Windows Form and passes parameters thru a property:

       class-id WinFormAccess.myClass.
       working-storage section.
       01 mainForm type WinFormAccess.Form1.
       01 myText  string  value "From myClass".
       method-id showForm.
       local-storage section.
       procedure division.
           set mainForm to new Form1
           set mainForm::textParam to myText
           invoke mainForm::ShowDialog
           set myText to mainForm::textParam
           goback.
       end method.

       end class.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: COBOL class calling WinForms

Jump to solution
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: COBOL class calling WinForms

Jump to solution

Thanks Chris , i understood now that we use Showdialog to invoke the forms instead of Run.

Tried it and ran successfully , now i am able to pass data to and from between the form and cobol code.

I queired this as in our old programs we load the screens after entering the cobol code and then pass the data to and fro . But in .Net application we have GUI layer as top layer and the Business layer and then the data layer under it .

Do we have any tools/techniques where in i can covert my screen sections of cobol into Winforms easily so that i dont break my existing cobol code flow and directlty invoke forms instead of screens.

Regards,

Zoeb

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.