Custom dialog box in C#?

后端 未结 5 2101
执笔经年
执笔经年 2021-02-20 17:19

I have a button that when clicked, a dialog box opens up with various controls on it such as radio buttons and text boxes. If OK then the values in that dialog box is passed ba

相关标签:
5条回答
  • 2021-02-20 17:45

    You can do this. Create a new form. From your main form you can call custom form using:

    CustomForm customForm = new CustomForm();
    customForm.ShowDialog(); 
    

    Make sure that you add relevant button to custom form and set their DialogResult property to OK, Cancel or anything else.

    0 讨论(0)
  • 2021-02-20 17:54

    One way would be to create an event in your dialog form. Depending on how many values you want to send back then you can just have parameters in the event delegate. A better way is to create a small class or struct for arguments containing a list of properties that you want to return.

    If you click OK then you fire the event with the values from the dialog. For cancel the event is not fired.

    In the form with the button you hook up a handler for the event. This receives your values and you may then do with them whatever you need to.

    http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

    0 讨论(0)
  • 2021-02-20 17:55
    • Add buttons to windows form. I usually name the buttons as either cmdOK or cmdCancel

    • Define the Cancel and OK Buttons in the dialog form

    0 讨论(0)
  • 2021-02-20 17:58

    Somewhere in the code that disposes of the dialog you can also explicitly set the result. For example, you could put the following code in a button click event handler.

    OnOKButton_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Dispose();
    }
    
    0 讨论(0)
  • 2021-02-20 18:01

    1.) Create the form you were talking about with all of the neccessary UI elements. Also add an OK and Cancel button to it.

    2.) In the property pane for the OK and Cancel button, set the DialogResult values to OK and Cancel, respectively. Additionally, you can also set the Form's CancelButton property to be that of the Cancel button you've created.

    3.) Add additional properties to the dialog that correspond to the values you'd like to return.

    4.) To display the dialog, do something along the lines of

    using( MyDialog dialog = new MyDialog() )
    {
       DialogResult result = dialog.ShowDialog();
    
       switch (result)
       {
        // put in how you want the various results to be handled
        // if ok, then something like var x = dialog.MyX;
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题