private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
iterationForm.ShowDialog();
}
当我显示对话框时,它会使用 ProjectID 显示与项目关联的迭代列表:
public partial class IterationForm : Form
{
public IterationForm(string projectID)
{
InitializeComponent();
LoadIterationsForProject(projectID);
}
private void LoadIterationsForProject(string projectID)
{
IterationRepository iterationRepo = new IterationRepository();
Int64 ID = Convert.ToInt64(projectID);
dgvIterations.DataSource = iterationRepo.FindAllIterations().Where(i => i.IDProject == ID).Select(i => new { Codigo = i.ID, Descripcion = i.Description, Inicio = i.StartDate, Fin = i.EndDate });
}
}
我的问题是,如何从数据 GridView 中捕获选定的值并将其传递给我的调用表单以使用选择的 IterationID 打开另一个表单?
感谢您的帮助。
所以也许是这样的:
private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
var result = iterationForm.ShowDialog();
showTheThing(result); //this uses the iterationID
}
最佳答案
ShowDialog() 的结果是 DialogResult 类型的枚举。 通常您会测试显示的表单的 DialogResult 是否为“Ok”。 您可以在表单中设置一个属性来保存结果,例如在属性“ResultProperty”中,如果您想在表单关闭后读取它,请使用以下内容:
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
if ( iterationForm.ShowDialog() == DialogResult.Ok )
{
// read result
var result = iterationForm.ResultProperty
}
else
{
// user has not clicked ok - do some other stuff here
}
在您显示的表单中,您可以在按钮上放置一个事件处理程序,用户在接受时应单击该按钮,并添加如下代码:
private void Accept_Click(object sender, EventArgs e)
{
this.ResultProperty = someResult;
this.DialogResult = DialogResult.Ok;
this.Close();
}
如果用户单击了取消按钮,您将设置如下属性:
private void Decline_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
关于c# - 如何显示对话框并捕获响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4204977/