User Control Buttons to Close a Dialog Box

12,094

Solution 1

Forms opened modally will close automatically when their DialogResult is set. From the sounds of it, you are setting the DialogResult of the control, not the form. To do what are you are trying to do, you need the control to set or trigger the setting of the parent forms dialog result. You can do this by either:

1. Passing through a reference of the form to the control, which allows the control to set it.

2. Create an event on the control that the forms listens to, which tells it to close. (recomemnded way)

3. (The hacky way) Set the forms dialog result using this code:

this.ParentForm.DialogResult = DialogResult.OK;

The form will also need to be shown modally, otherwise you will need to call the Close() method manually.

Solution 2

You will want to use delegates to solve this problem.

In the code behind of your control, define a delegate which you will use to tell your main form to close itself. Wherever in your control you want to have the parent form close, you will raise this custom event which will be handled by your parent form. For sake of example, we'll assum you want to close the parent form on a button click in the control

public delegate void CloseHostFormEventHandler(Object sender, EventArgs e);

public partial class MyControl : Control {

    public event CloseHostFormEventHandler CloseFormEvent;


    public closeButton_Clicked(object sender, EventArgs) {
        // do your db stuff

        // you could create your own class here and pass the object to your main form if you wanted
        EventArgs myargs = new EventArgs(); 

        // tell host form to close itself
        CloseFormEvent(this, myargs);

    }
}

Now in your parent form, you will want to handle the event raised by the control.

public partial class MyForm : Form {

    public MyForm() {

        InitializeComponent();

        // ill assume your control was added via the designer and thus done in InitializeComponent()

        // hook up event handler
        mycontrol.CloseFormEvent += CloseFormEventHandler(closeformCallback);
    }

    protected void closeformCallback(object sender, EventArgs e) {
        DialogResult = DialogResult.OK;
        this.Close();
    }


}

Solution 3

I'm going to take a wild guess since you haven't given much information.

The dialog doesn't close because it isn't a modal dialog. Try showing it with ShowDialog() rather than Show().

Solution 4

You can put in the Click event handler on your user control something like this:

        Form f = this.ParentForm;
        if (f != null)
        {
            f.DialogResult = DialogResult.OK;
        }

Solution 5

As Tony says. You would need to call the close method.

What you could do is pass a reference to the calling from into your control and call its close method from there.

Share:
12,094
Taryn
Author by

Taryn

developer, techie, nerd.... ex-DBRE at Stack Overflow, previously a Community Manager for Stack Overflow. When I'm not at my desk, I do CrossFit and run (a lot).

Updated on June 25, 2022

Comments

  • Taryn
    Taryn almost 2 years

    I have created a User Control in my application that has a textbox and then 2 buttons on it. One button is to Add data to the DB from the textbox and the second Cancels the action. This User Control is then added to multiple Dialogs (forms) but I want the buttons to trigger the same events, which the reason I placed the buttons on the User Control and not the Dialog.

    What I am trying to do is after they click the Add data button and it is successful, I want it to close the Dialog that the control is located on.

    EDIT:

    I don't know how to go about coding this to close the Dialog when it is triggered from a User Control. I don't know where to start because I haven't used User Controls before. Typically I just have the controls on a Dialog which allows for the DialogResult = DialogResult.OK;

    Thanks