Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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


}

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().

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.

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

DialogResult does not close a non-modal form.

You need to call the Close method of the form or in this case dialog.

EDIT

Or use ShowDialog() instead of Show().

EDIT AGAIN

According to DialogResult documentation:

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top