简体   繁体   English

我如何在 winforms 中制作我的自定义对话框?

[英]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:我想创建一个自定义单元格选择对话框(用户从 dataGridView 中选择一个单元格,然后按 OK 或 Cancel 按钮),以便我可以获得如下结果:

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.这里的关键是,if 语句在 dialogresult 出现之前不会执行。

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...我开始做的是 - 创建一个新的 Form ChooseFileFromDBForm : Form ,好吧..很难弄清楚哪些类应该通知哪些类关于按钮点击(该表单中有一个 gridview 和按钮)等......

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)我不能告诉你细节,因为它不会简要解释你我选择了什么设计,但如果你愿意 - 这里有我最后一次提交的“糟糕设计”: github (ChooseFileFromDBForm.cs,DBFilePicker.cs 文件)

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?问题是 - 我如何创建自己的 DialogResult 东西 - 以便它等到用户选择一行/单元格(结果表中只有一列)并按下“确定”或“取消”按钮?

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.您可以使用 Windows 窗体创建自己的对话框,并为 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.在保存 DataGrid 的表单中将其设置为类变量并将其设置为 null。

CellSelectionDialog csd = null

then on your DataGridView subscribe to the selectionChanged event.然后在您的 DataGridView 上订阅 selectionChanged 事件。 In the event check to see if your Dialog is null.在事件中检查您的 Dialog 是否为空。 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使用 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.因为 Form 类默认有 ShowDialog 方法。

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.为了使 DialogResult 为 OK 或 Cancel 或类似的结果,请将表单中按钮的“Behavior”中的“DialogResult”设置为 OK 或 Cancel 或任何您想要的。 If nothing is set, your DialogResult will always be None.如果未设置任何内容,您的 DialogResult 将始终为 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:因此,如果我单击将 DialogResult 设置为 OK 的按钮,我会得到该属性的值:

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

That's how it all works.这就是一切的运作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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