简体   繁体   English

C# 处理对话框的按钮单击另一种形式

[英]C# Handle a dialog's button click in another form

I have a form.我有一个表格。 In that I got to show a dialog (on some circumstances) with Text and a Cancel button.在那我必须显示一个对话框(在某些情况下),带有文本和一个取消按钮。 I want to catch the event of that button in my form Or know if that cancel button was clicked.我想在我的表单中捕获该按钮的事件,或者知道是否单击了该取消按钮。

How can this be done?如何才能做到这一点? I believe this should be possible but can't make out how?我相信这应该是可能的,但无法弄清楚如何?

From my mainForm I have BackgroundWorker.从我的 mainForm 我有 BackgroundWorker。 When the backgroundWorker is started I open a childForm (with a Label and a button) and when the background task is over, I close the childForm.当 backgroundWorker 启动时,我打开一个 childForm(带有 Label 和一个按钮),当后台任务结束时,我关闭 childForm。 What I want more is: when the button of childForm is clicked the ongoing task of backgroundWorker should be cancelled.我更想要的是:当点击 childForm 的按钮时,backgroundWorker 正在进行的任务应该被取消。 SOLUTION解决方案

In my childForm I have set CancelButton property as cancelBtn for the form.在我的 childForm 中,我将表单的 CancelButton 属性设置为 cancelBtn。 The othe code is:其他代码是:

    private bool cancel;
    public bool Cancel
    {
        get { return cancel; }
        set { cancel = value; }
    }
    // Set the flag as true to indicate that Cancel button was actually pressed
    private void cancelBtn_Click(object sender, EventArgs e)
    {
        Cancel = true;
    }

In mainForm:在主窗体中:

    childDlg = new ChildDialog();

    // wHILE cALLING
            backgroundWorker1.RunWorkerAsync();
            msg = "Connecting...";
            childDlg .set(msg, "");
            if (!childDlg .IsAccessible)
            {
                // This is caught even when the dialog is closed   
                if (childDlg .ShowDialog() == DialogResult.Cancel) {
                     if (childDlg.Cancel == true) { // Was really cancelBtn pressed  
                         // NOW ONLY do my stuff 
                     }   
                }
            }  

I had tried using @DeveloperX technique ie EventHandler in parent class, but the parent class method was nver being called.我曾尝试在父 class 中使用 @DeveloperX 技术,即 EventHandler,但从未调用父 class 方法。 Tried a lot but couldn't success.尝试了很多,但无法成功。 Then tried of @RobinJ's technique and it worked.然后尝试了@RobinJ 的技术,它奏效了。 I just had to add flag to identify was really cancel button pressed or jjst windw was closed normally.我只需要添加标志来识别是否真的按下了取消按钮或 jjst windw 已正常关闭。

Thanks to all of you for tryig to help me out.感谢大家尝试帮助我。 I really appreciate your help.我真的很感谢你的帮助。

Thanks谢谢

Set DialogResult property to either DialogResult.Ok or DialogResult.CancelDialogResult属性设置为DialogResult.OkDialogResult.Cancel

Then in the parent form:然后在父窗体中:

Form form = new Form();
DialogResult results = form.DialogResult;

if(results == DialogResult.Ok)
{

... make magic

}
else
{
...
}

Put this in the form that should catch the event:把它放在应该捕获事件的形式中:

frmDialog.ShowDialog();

And this in the btnCancel_Click event of the dialog:这在对话框的 btnCancel_Click 事件中:

return DialogResult.Cancel();

Sorry if I'm confusing VB and C# here, but it's pretty much the same.对不起,如果我在这里混淆了 VB 和 C#,但它几乎是一样的。

Simply you can create an event for the form that shows the dialog and handle this event in parent form in case the user clicks on ok fire event with specefic parameter and for cancel another parameter (such dialogresult.cancel)只需为显示对话框的表单创建一个事件并在父表单中处理此事件,以防用户单击带有特定参数的 ok fire 事件并取消另一个参数(例如 dialogresult.cancel)

an pseudo implementation can be like this一个伪实现可以是这样的

public class FormChild : System.Windows.Forms.Form
{
    public event EventHandler DialogCanceled;
    public event EventHandler DialogConfirmed;
    public void ShowDialog()
    {
        using (var dialogForm = new   FormDialog())
        {
            if (dialogForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (DialogConfirmed != null)
                    DialogConfirmed(this,new EventArgs());
            }
            else
            {
                if (DialogCanceled != null)
                    DialogCanceled(this,new EventArgs());
            }
        }
    }
}
public class ParentForm : System.Windows.Forms.Form
{
    public void callChild()
    {
        using (var f = new FormChild())
        {
            f.DialogCanceled += new EventHandler(f_DialogCanceled);
            f.DialogConfirmed += new EventHandler(f_DialogConfirmed);
            f.ShowDialog();
        }
    }

    void f_DialogConfirmed(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    void f_DialogCanceled(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

You should be using the ShowDialog method on the form you need to show as a dialog and then use the DialogResult property to communicate to the parent form the result of the dialog operation.您应该在需要显示为对话框的窗体上使用ShowDialog方法,然后使用DialogResult属性将对话框操作的结果传达给父窗体。

This way you handle the button click on the form that owns the button but set the DialogResult to DialogResult.Cancel to specify that the user pressed the cancel button.这样,您可以在拥有该按钮的表单上处理按钮单击,但将DialogResult设置为DialogResult.Cancel以指定用户按下了取消按钮。

Use the following:使用以下内容:

Form form = new Form();
switch (form.ShowDialog())
{
    case DialogResult.Ok:
    {
        ....
        Break;
    }
    case DialogResult.Cancel:
    {
        ....
        Break;
    }
}

Set the Form.AcceptButton and Form.CancelButton properties to the appropriate buttons.Form.AcceptButtonForm.CancelButton属性设置为适当的按钮。


Refer to the following:请参阅以下内容:

Form.ShowDialog Method Form.ShowDialog 方法

DialogResult Enumeration DialogResult 枚举

Form.AcceptButton Property Form.AcceptButton 属性

Form.CancelButton Property Form.CancelButton 属性

A dialog is usually a blocking event, where eventhandling by the parent form would make no sense at all.对话框通常是一个阻塞事件,父表单的事件处理根本没有意义。

If it isn't a modal dialog, you can always create one or several public events in the popup form, that are triggered when the buttons are clicked.如果它不是模态对话框,您始终可以在弹出表单中创建一个或多个公共事件,这些事件在单击按钮时触发。 These events can then be caught by the parent form.然后这些事件可以被父表单捕获。

Don't expose your buttons to the parent form, it would be terrible oo-programming.不要将按钮暴露给父表单,这将是可怕的 oo 编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM