Building a custom blocking dialog

P

pswulius

Hey everyone,

for reasons I can't explain quickly, I'm developing a completely custom
OK/OK_CANCEL dialog, though I think many people would benifit from this
knowledge. I basically need to re-invent the wheel and provide a
simple API like:

Result result = CustomDialog.Show( Mode.OK_CANCEL, aMessage );

The problem that I'm not sure how to solve is the blocking aspect that
dialogs I've used in the past offer. Obviously I'd like to invoke this
line of code from various points in my application and have it block
until user input is received.

Does anyone have experience with this type of problem? I think I need
a way to "hand-off" processing to a secondary GUI thread, or something
like that. Especially since this dialog will arise as a result of user
interaction with the main UI.

Any and all advice would be great. The solution may not be available
from a high-level C# api, but I still wanted to post here. Thanks.

--pete
 
G

Guest

I think you should use the ShowModal (if such a method exists) instead of Show.

Regards,
Jv
 
S

Spidey

You simply need to create a Form to represent your custom dialog box.
Invoke your dialog box using the ShowDialog method of the Form passing
the current Form as the owner. The ShowDialog method returns a
DialogResult value.

DialogResult customDialogResult = CustomDialog.ShowDialog(this);

if(customDialogResult==DialogResult.OK)
{
//OK button was clicked
}
else
{
//Cancel button was clicked
}

In your CustomDialog form, you need to set the DialogResult property
appropriately.

i.e. in the click handler for the OK button
this.DialogResult=DialogResult.OK;

and in the click handler for the Cancel butotn
this.DialogResult=DialogResult.Cancel;

Hope this helps.

Regards,
Sarin.
 
G

Guest

Sorry about the previous post. I did not have VS.Net readily available so had
to guess. But the following class works.

You can use the following class. Set the WindowPosition to CenterOwner. The
user will not be able to quit this dialog without clicking on the Ok or
Cancel button.

using System;
using System.Windows.Forms;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for CustomDialog.
/// </summary>
public class CustomDialog:Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public CustomDialog()
{
//
// TODO: Add constructor logic here
//

this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Ok";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(152, 128);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "Cancel";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.IndianRed;
this.ClientSize = new System.Drawing.Size(272, 168);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowDialog();
}
bool okClicked=false;

private void button1_Click(object sender, System.EventArgs e)
{
okClicked=true;
this.Hide();
}

private void button2_Click(object sender, System.EventArgs e)
{
okClicked=false;
this.Hide();
}
}
}

Regards,

Jv
 
P

pswulius

Thank you Sarin and J.V.

The problem is that I'm developing a completely custom dialog, so I
don't have the luxury of using .net platform UI components. I've
gotten some traction with some win32 experts who have pointed me in the
direction of making my own message loop to handle the UI events while
my dialog is blocking the main UI thread. I'm currently trying to get
that working. I'll let you know how it turns out.

--pete
 
P

pswulius

Norm's idea (from the user32 group) to take over the responsibility of
handling messages was just what I needed. I ended up very closely
following his workflow. Thank you very much for the help.

Reference
title: Building a custom blocking dialog
group: microsoft.public.win32.programmer.ui
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top