Solved

Simulate a message box, center commondialog box

Posted on 1998-09-30
17
385 Views
Last Modified: 2010-04-30
Dear Experts,
    I am tryiny to create something just like a message. Except that it contains a few checkboxes to enable people to select differently. I am trying to use a form to do that.
    However I find that for a message box provided by VB, after you click OK, the program execution returns to the line immediately after the message box. The form I created to simulate the message box cannot do that. "Goto" only works within a procedure thus it is not useful.
     How can I simulate using a form that will return execution to the point(in the original form) where it was originally called and pass back a variable (of a user created type) to the original form?

     status=myOwnMessageBox()

     The second question is how to center the file open commondialog box?

     Thanks in advance.

Cheers,
0
Comment
Question by:happydanny
  • 5
  • 4
  • 4
  • +3
17 Comments
 
LVL 1

Expert Comment

by:kacklehorn
ID: 1437406
You need to make your form modal. i.e frmMessage.Show vbmodal,me.

This will not execute any more lines of code until the user hit OK or closes the form.
0
 
LVL 1

Expert Comment

by:kacklehorn
ID: 1437407
As far as centering the commondialog box, if you look in the help for it, it says you can't do it. However I'm sure that there is an API that will allow this. I'll check on it for you.
0
 
LVL 2

Expert Comment

by:polygon
ID: 1437408
you may declare a public variable of type Boolean, say MyBool, then run the code like

MyMsgBox(......)
do
loop until MyBool
..

you will have to declare another variable to store the info that should have been retuned as this method does not provide a retun value. You will set MyBool True when you hide you MsgBox form.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:happydanny
ID: 1437409
Dear Polygon,
    I am afraid the do loop will not even be executed. After the point where another form is called (the simulated message box), the control does not return to the same procedure that calls it.

Regards,
Dan
0
 
LVL 2

Expert Comment

by:Grdv
ID: 1437410
In answer to your first question I must agree kacklehorn and then in reply to the other question... if you declare the commondialogcontrol through api, you can use set parent with .hInstance as child to get the commondialogs topleft aligned together with the topleft of the parent...from that you can make it centered...
 I don't know if this is possible with a usual commondialog...

Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As  Long, ByVal hWndNewParent As Long) As Long
0
 

Author Comment

by:happydanny
ID: 1437411
Dear Polygon,

    You are right. I don't know what got into me the first time I tried it. But your solution works!
    Please propose a solution again so I can pass you the points. Not that you will need them, since you know so much of VB you won't need help anyway. :)
    Thanks!

Regards,
Dan

0
 
LVL 2

Expert Comment

by:Grdv
ID: 1437412
Just one question happydanny, what was wrong with the suggestion from kacklehorn??
and btw, that doesn't take that amout of comp power that the other method do...
I vbModal window works exactly the same as a msgbox that itself is a MODALFRAME object...
but it's up to you...

Gustav
0
 

Expert Comment

by:ZoroZor
ID: 1437413
The right answer is from polygon.

It's not possible to wait for an input issued from an object event. In POO, many events can occur silmutanously. If you want to wait for a data modified in an other sub ... the only way is to make a do while loop in the first sub as Polygon said.
0
 
LVL 2

Expert Comment

by:Grdv
ID: 1437414
ZoroZor:
first of all I don't agree with you, but it doesn't matter...
you can try it yourself...make a project with 2 different forms (frm1,frm2)
in frm1 (which is the mainform,startupform) print in the load event
Private sub form_load()
  frm2.show vbModal, Me
  MsgBox "here you see", , "test"
end sub
you will see that the program show's the frm2 dialog and wait's until it's closed to continue with the next line of code... which displays a messagebox...
if you instead wrote
Private sub form_load()
  frm2.show
  MsgBox "here you see", , "test"
end sub
then both of the boxes would appear as the frm2 wouldn't be a MODALFRAME of frm1 and the code would continue to execute....
and secondly, if you think polygon was right, you should have sent that as a comment and not as an answer... if nothing else your preventing polygon from getting his points??
even though that answer is incorrect...
but it's all up to happydanny, who he wants to give the points to...
as far as I'm concerned noone except me has even tried to come up with a solution for the center commondialog problem... but well, well...
so, what should be the most fair thing to do is for happydanny, to try my thing and then tell here what was wrong and reject ZoroZor's answer and let somebody answer to both his questions...
0
 

Author Comment

by:happydanny
ID: 1437415
Dear All,
    Polygon method works but I think Grdv and kacklehorn method is much more efficient. I initially rejected his kacklehorn's answer because I mistyped(in fact I knew about the modal thing before kacklehorn mentioned it but implement it wrongly)

    form.show modal, me
 
    This did not give me an error but it did not go into modal mode. Since Grdv is so insistant on his claims I took a second look and realised my mistaked. It should be "vbModal".

    Sorry I can't pass you the points ZorZor. If I have more points I would like to pass you some for making the effort to help. But I am kinda low now so....sorry.

    Thanks to Grdv and kacklehorn for proposing the right solution. I will hand the points to either one of them which register a proposed answer first.

    I will have to reject ZorZor's answer to enable them to do that. Sorry about that.

Regards,
Dan
0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1437416
You need to Set the form As a Modal Form.
Also.. in the Declare a Form level Variable.
Public Answer as Integer

In CmdCancel Button..
   say Answer = -1
Then in your Form Unload Event..
   say Answer = BoxOne * -1 + Box2 * -2
This Sets that Variable to an answer.

In your Form that calls this maybe do this

MyMessage.show vbmodal
'This line wont get hit until the user gets off that other form

then do this..
Select case MyMessage.Answer
case -1
  call DothisBecauseUserHitCancel
case 0
  Call DoThisBecasueUserSelectNothing
case 1
  call DoThisBecasueUserSelectedTheFirstBox
case 2
  call DoThisBecasueUserSelectedTheSecondBox
case 3
  call DoThisBecasueUserSelectedBothboxes
end select


maybe here is an example
'0- = cancel
'1 = Box One selected
'2 = Box Two Selected
'3 = Both One and Two Selected

0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1437417
and i Should Get ALL the points.. becasue I answered them ALL  (you wanted a return Value) instead of arguing over modality.
0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1437418
and i Should Get ALL the points.. becasue I answered them ALL  (you wanted a return Value) instead of arguing over modality.
0
 

Author Comment

by:happydanny
ID: 1437419
Dear AnswerMan,
     There is no merit in showing me how to use a Global variable. Others please kindly do not propose an answer as I am waiting for Grdv or kacklehorn to collect their points.
     Thanks!

Cheers,
Dan
0
 
LVL 2

Accepted Solution

by:
Grdv earned 50 total points
ID: 1437420
What you need is as I said to launch the frame in vbModal-mode... that's all there is to it.
just like this:
Form1.Show vbModal, Me.

thanks for understanding...

// Gustav
0
 

Author Comment

by:happydanny
ID: 1437421
Dear Grdv,

    Don't worry, you already told me the answer long ago. I am not asking you for another answer, just asking you to put a line in the proposed answer to collect the points that's all. Here's the points you well deserved!
    Thanks for the help man.

Cheers,
Dan
0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1437422
you puss.. Thats not a global variable..
ITs a Public Variable that ges created when you create a
New Instance of the form you want.  If you create 5 forms.. you get 5 variables.  try that with your "global Variable"

Modality was only half the problem.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question