Button的DialogResult.OK问题?

billrobin 2012-08-10 06:23:00

如果将button的DialogResult设为OK,验证失败后,它会直接关闭窗口,这时,我们要求在验证失败后重新输入,而不是关闭窗口,请问怎么做?

主窗口:
#region
//if (RValue == "")
//{
// MessageBox.Show(this, "请选择需要修改的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
Login.FLAGID = "10";
NewClientAdd editor = new NewClientAdd();
editor.ShowDialog();
if (editor.DialogResult == DialogResult.OK)
{
DataGridViewBind2();
}
#endregion


子窗口:


Button的DialogResult.OK



...全文
228 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
billrobin 2012-08-11
  • 打赏
  • 举报
回复
to Jia_H,,,,,问题已解决,谢谢!!
zhui22222 2012-08-10
  • 打赏
  • 举报
回复
楼上的++
Jia_H 2012-08-10
  • 打赏
  • 举报
回复
写了个小程序 :)
在TestForm的输入框中输入123时才会返回OK。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestDialog
{
class Program
{
static void Main(string[] args)
{
Application.Run(new MainForm());
}
}

class MainForm : Form
{
Button cmdTest;

public MainForm()
{
this.SetBounds(0, 0, 300, 400);
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "Main Form";

cmdTest = new Button();
cmdTest.SetBounds(100, 180, 100, 40);
cmdTest.Text = "Test";
cmdTest.Click += (o, e) =>
{
TestForm frm = new TestForm();
frm.ShowDialog();

MessageBox.Show("DialogResult: " + frm.DialogResult);
};

this.Controls.Add(cmdTest);
}
}

class TestForm : Form
{
Button cmdTest;
TextBox txtTest;

public TestForm()
{
this.SetBounds(0, 0, 200, 200);
this.StartPosition = FormStartPosition.CenterParent;
this.Text = "Test Form";

txtTest = new TextBox();
txtTest.SetBounds(50, 50, 100, 30);

cmdTest = new Button();
cmdTest.SetBounds(70, 100, 60, 40);
cmdTest.Text = "Test";
cmdTest.Click += (o, e) =>
{
if (txtTest.Text == "123") this.DialogResult = System.Windows.Forms.DialogResult.OK;
};

this.Controls.Add(cmdTest);
this.Controls.Add(txtTest);
}
}
}
Jia_H 2012-08-10
  • 打赏
  • 举报
回复 1
MSDN中Form.DialogResult的说明如下:

窗体的对话框结果是当窗体显示为模式对话框时从该窗体返回的值。如果窗体显示为对话框,用 DialogResult 枚举中的值设置此属性将设置该窗体的对话框结果值、隐藏模式对话框并将控制返回给调用窗体。 此属性通常由窗体上 Button 控件的 DialogResult 属性设置。 当用户单击 Button 控件时,分配给 Button 的 DialogResult 属性的值将分配给该窗体的 DialogResult 属性。

看来你不要设定Button的DialogResult,在验证通过时再设定为OK才行。动手试试看吧。
Jia_H 2012-08-10
  • 打赏
  • 举报
回复
Form的一个属性。
billrobin 2012-08-10
  • 打赏
  • 举报
回复
是不是设定了Accept Button呀? 在什么地方呀,没有找到。。
Jia_H 2012-08-10
  • 打赏
  • 举报
回复
是不是设定了Accept Button呀?不要设定,在验证通过后再设定form的DialogResult为DialogResult.OK。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧