Highlighted
Absent Member.
Absent Member.
9598 views

Show message box from a class with no form

Jump to solution

Is it possible to show a message box from a class that has no form?  Two separate projects, one has a form but calls the class without a form to read, write data etc.  If there is an error, can I show the message box from the called class or do I need to pass the error back to the calling class and display it there?  The routing I use in the calling pgm does not compile in the called class.  Thanks.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

Sure you can use the MessageBox class in a console application if you like but you will have to add the proper assembly to the project.

In the Visual COBOL Help search for MessageBox.

The help will list a number of items because there are a couple different MessageBox classes depending on the technology you are using such as Windows Forms or WPF.

Since you are using Windows Forms. click on the entry for MessageBox Class in System.Windows.Forms.

The docs will tell you that the assembly name where this class resides is called System.Windows.Forms.

In Solution Explorer, right click on References under the project name in which you wish to display the message and then select Add Reference.

From the .NET tab scroll to find System.Windows.Forms for the .NET Framework version that you are using. This will be .NET Framework 4 by default.

Click on OK to add the assembly to the project.

In order to be able to reference this class without fully qualifying the name like System.Windows.Forms.MessageBox you will need to add the namespace to the project or to the program.

To add it to the project, open up project properties by double clicking on Properties under the project name and then navigate to the Namespace tab.

Scroll down the list and check System.Windows.Forms and save.

If you wish to add the namespace to just that program then add the directive:

$set ilusing"System.Windows.Forms" to the top of the program.

Example: this is a console program that displays a messagebox.

     $set ilusing"System.Windows.Forms"

      program-id. Program1 as "testpopup.Program1".

      procedure division.

          invoke type MessageBox::Show("This is a message")

          goback.

View solution in original post

0 Likes
15 Replies
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

The routine I use in the calling pgm does not compile in the called class. (Typo above)

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

Sure you can use the MessageBox class in a console application if you like but you will have to add the proper assembly to the project.

In the Visual COBOL Help search for MessageBox.

The help will list a number of items because there are a couple different MessageBox classes depending on the technology you are using such as Windows Forms or WPF.

Since you are using Windows Forms. click on the entry for MessageBox Class in System.Windows.Forms.

The docs will tell you that the assembly name where this class resides is called System.Windows.Forms.

In Solution Explorer, right click on References under the project name in which you wish to display the message and then select Add Reference.

From the .NET tab scroll to find System.Windows.Forms for the .NET Framework version that you are using. This will be .NET Framework 4 by default.

Click on OK to add the assembly to the project.

In order to be able to reference this class without fully qualifying the name like System.Windows.Forms.MessageBox you will need to add the namespace to the project or to the program.

To add it to the project, open up project properties by double clicking on Properties under the project name and then navigate to the Namespace tab.

Scroll down the list and check System.Windows.Forms and save.

If you wish to add the namespace to just that program then add the directive:

$set ilusing"System.Windows.Forms" to the top of the program.

Example: this is a console program that displays a messagebox.

     $set ilusing"System.Windows.Forms"

      program-id. Program1 as "testpopup.Program1".

      procedure division.

          invoke type MessageBox::Show("This is a message")

          goback.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

Chris,

Excellent answer thank you.

As most of our messages are 'update messages' in that they keep the user informed as to whats happening as a process runs how do I REMOVE the box from the screen at the end of the program ?

e.g

A-PROCESS

   invoke type MessageBox::Show("This is a message")

B-PROCESS

  Perform process until eof.

C-END.

< get rid of message box >

What goes in the < get rid of message box > bit to remove the box from screen at end of process?

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

Also I've noticed that there is a DEFAULT <Ok> button on the message box.

How can I have a message box WITHOUT a button that just repopulates itself when I pass it the appropriate message?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

You should check the .NET Framework documentation for the MessageBox class to get a full description of the options that are available. You can do this opening up the Visual COBOL Help in the IDE and then doing a search on "system.windows.forms.messagebox".

For Example you can get an OK button by modifying the statement to read:

invoke type MessageBox::Show("This is a message", "Message Box Name", type MessageBoxButtons::OK)

But I don't think that the MessageBox class is going to work for you because the MessageBox requires that one of its buttons is pressed prior to it disappearing.

You might want to look at using a custom popup form like a tooltip so that you can control the display of the message. Either that or perhaps you should place a Label or Textbox control on the main form that can be updated with a string value at required intervals.

I will work on putting together a sample for using the popup window approach.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

Cheers Chris.

I went away and tried calling a new dialog form (rather than a message box) from the legacy code program but run into the same problem in that without any user action on the newly called dialog I can't get the thing to come back to the program calling the new dialog.

Tried using a 'timer' when I'd count to 10k as a LOAD action but to no avail.

As these legacy programs perform ALL processing at one go they aren't built to keep returning to the main program with an update as to whether each individual record got processed or not.

I'm looking for a solution that simply pops up a box at the start of the program, repopulates the box with an ever changing message while processing the data then closes the box at the end of the program.

Simples????

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

I think the problem is that using a form to do this will change the focus from the main form to the new form so you would have to change the focus back to the main form thru code.

I created an example that kind of does what you want only it uses a statusStrip control on the bottom of the main form instead of popping up a new form.

Let me know if this will work for you...

I changed the attached file because it had a bug in the code where it needed a call to refresh method in order to cause a redraw to occur on the status bar.

The new example also utilizes a prograss bar if you are interested in perhaps using one of these to report current status.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

Chris,

This is proving to be trickier than we thought....

Your example files work perfectly well IF the subform (Form1) is part of the SAME solution as the main program (Program1) as it references.

In your example program1.cbl references the form to call as 'mainform as type displaystatus.Form1' where, being part of the same solution this is recognisable.

In my situation I have a top level .EXE (PROGA) which holds the main form stuff.

PROGA calls out to SUBPRG1.DLL to do some processing. SUBPRG1 is legacy COBOL code in which I've added the appropriate namespaces to allow for direct form use. This DLL knows nothing about the mainform only stuff that resides within it.

SO...in my SUBPRG1 code I have written the following code...

set MessageDialog to new Infomessage() (where InfoMessage is my dialog box)

invoke MessageDialog::populateLinkage(Progress-Message) (where Progress-Message is my message I'm trying to show)

invoke MessageDialog::ShowDialog()

 OK. So far so good...my initial message pops up.

Problem is now is that I need to return to SUBPRG1 (without user action) in order to continue processing (and to continually update my message) and, at the end of the program, close the MessageDialog (which I can do with invoke MessageDialog::Close()

Is there a way I can attach a process within the InfoMessage.cbl code to perform ONCE the dialog loads and displays and then to return to the calling program (Tried using Sleep(1000) upon the 'Load' event.

 

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

What happens if you do a Show on MessageDialog instead of ShowDialog?

I still think that the statusstrip on the main form is the best way to do this.
I am attaching a reworked version of my sample that now works with the mainform in a different project than the legacy code.

To do this I created a new mainprog windows application project which will be the applications .EXE which only contains the main.cbl.
I removed the main.cbl from the original displaystatus project and made displaystatus into a Class Library (.dll) so that I could add a reference to it.

I changed the reference to Form1 in main.cbl to point to displaystatus.Form1.

So now I have a solution with 3 projects.

1. mainprog (main .EXE)
2. displaystatus (.DLL containing Forms)
3. ClassLibrary1 (.DLL containing legacy programs)

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Show message box from a class with no form

Jump to solution

Chris,

On attempting to BUILD your solution I get the following errors...

img705.imageshack.us/.../builderrors.jpg>

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Show message box from a class with no form

Jump to solution

OK, I have a new simplified example that should work for you. (I hope!)

Because this forum only allows for 64K byte attachments I am forced to delete the bin folders prior to zipping them so they are small enough to attach.

The previous example was using a reference back to the calling assembly in order to saitisfy the reference to the form class and this reference caused a build error if the projects were not built in the correct sequence.

This new example uses an interface definition instead which allows it to reference the form without requiring a reference to the calling assembly.

This is the way I should have done it to begin with as it allows me to heep the forms in the original .EXE instead of moving it to a new class library.

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.