简体   繁体   中英

How do i make my custom dialog in winforms?

I've stuck with a bad design.

I want to create a custom cell selection dialog (user selects a cell from dataGridView and then presses the button OK or Cancel) so that I can get the result like below:

public string GetFile()
{
    DialogResult dialogResult = _open.ShowDialog();

    if (dialogResult == DialogResult.OK)
        return _open.FileName;
    else
        return null;
}

The key here is, that the if statement won't execute until dialogresult appears.

What I started doing was - creating a new Form ChooseFileFromDBForm : Form , and well.. It was very hard to figure out what classes should inform what classes about button clicking (there's a gridview and button in that form) etc...

Now I think that it's a bad design. I can't tell you the details as it won't be any brief explaining you what design I chose, but if you want - there's my last commit "bad design" here: github (ChooseFileFromDBForm.cs, DBFilePicker.cs files)

The question is - how do I create my own DialogResult thing - so that it waits until a user selects a row/cell (there's only one column in the resulting table) and presses the "OK" or "Cancel" buttons?

There may be a better way but you can......

You could create your own dialog using windows forms and add in a OK, Cancel button and a property or member variable for the DataGridSelectedCellCollection.

public partial class CellSelectionDialog : Form
{
    public DataGridViewSelectedCellCollection cells { get; set; }
    public CellSelectionDialog()
    {
        InitializeComponent();
    }
}

Set this as class variable in the form that holds the DataGrid and set it to null.

CellSelectionDialog csd = null

then on your DataGridView subscribe to the selectionChanged event. In the event check to see if your Dialog is null. If it is then you are not looking to obtain the cell. If it is not null then set the value

if (csd != null)
{
    csd.cells = dgvGrid.SelectedCells;
    csd.BringToFront();
}

add an event handler to handle the closing of the form on your form with the DataGridView

private void CellSelectionDialog_FormClosing(object sender, FormClosingEventArgs e)
{
    if (csd.DialogResult == DialogResult.OK)
    {
        //Do something with csd.cells
        MessageBox.Show(csd.cells[0].Value.ToString());
        //set the form to null;
        csd = null
    }
}

and you can call the dialog as

csd = new CellSelectionDialog();
csd.FormClosing += CellSelectionDialog_FormClosing;
csd.Show();

The answer is, that one can create one's own dialog with a default form. One doesn't need to create any special classes or anything like that. Because the Form class has the ShowDialog method by default.

In order to get DialogResult as OK or Cancel or something like that, set the "DialogResult" in "Behavior" of your button in your form to OK or Cancel or anything you want. If nothing is set, your DialogResult will always be None.

I have a public property in my "custom dialog" form (it's still just a form) that returns the value of the selected row:

public string SelectedFileName
{
   get
   {
       if (IsSelected)
           return filesDBdataGridView.SelectedCells[0].Value.ToString();
       return null;
   }
}

So, if I click on the button that has its DialogResult set to OK, I get that property's value:

_chooseForm.ShowDialog();
if (_chooseForm.DialogResult == DialogResult.OK)
   return _chooseForm.SelectedFileName;
else
   return null;

That's how it all works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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