English 中文(简体)
C#以另一种形式处理对话框的按钮单击
原标题:C# Handle a dialog s button click in another form

我有一个表格。在表格中,我可以显示一个对话框(在某些情况下),其中有文本和取消按钮。我想在表单中捕捉该按钮的事件,或者知道是否单击了取消按钮。

如何做到这一点?我相信这应该是可能的,但我搞不清楚怎么可能?

From my mainForm I have 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. What I want more is : when the button of childForm is clicked the ongoing task of backgroundWorker should be cancelled. SOLUTION

在我的childForm中,我已将表单的CancelButton属性设置为cancelBtn。其他代码为:

    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;
    }

在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 
                     }   
                }
            }  

我曾尝试在父类中使用@DeveloperX技术,即EventHandler,但父类方法未被调用。尝试了很多,但都没能成功。然后尝试了@RobinJ的技术,结果成功了。我只需要添加标志来识别是真的按下了取消按钮还是jjst windw正常关闭。

感谢你们所有人帮助我。我真的很感激你的帮助。

谢谢

最佳回答

把它放在应该捕捉事件的形式中:

frmDialog.ShowDialog();

在对话框的btnCancel_Click事件中:

return DialogResult.Cancel();

如果我在这里混淆了VB和C#,很抱歉,但它几乎是一样的。

问题回答

DialogResult属性设置为对话框结果。确定对话框结果。取消

然后在父窗体中:

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

if(results == DialogResult.Ok)
{

... make magic

}
else
{
...
}

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)

伪实现可以是这样的

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();
    }
}

您应该在需要显示为对话框的窗体上使用ShowDialog方法,然后使用DialogResult属性将对话框操作的结果传达给父窗体。

通过这种方式,您可以在拥有按钮的窗体上单击按钮,但将DialogResult设置为对话框结果。Cancel指定用户按下了取消按钮。

对话框通常是一个阻塞事件,由父窗体进行的事件处理根本没有意义。

如果它不是一个模式对话框,您总是可以在弹出窗体中创建一个或多个公共事件,这些事件在单击按钮时触发。然后这些事件可以被父窗体捕获。

不要将按钮暴露在父窗体中,这将是糟糕的oo编程。

使用以下内容:

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

Form.AcceptButtonForm.CancelButton


请参阅以下内容:

Form.ShowDialog方法

DialogResult枚举

Form.AcceptButton属性

Form.CancelButton属性





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.