为什么 ShowDialog 总是返回 DialogResult.Cancel?
我有一个定制的对话框winform。上面有一个标签、文本框和 2 个按钮(确定和取消)。它还声明并定义了重载的execute方法来传递不同的参数列表。
Dialog Winform 的调用如下:
var theDialog := new InputMsgBox;
if theInputB.Execute('Awesome StackOverflow','Enter Text?',s,var s) = DialogResult.OK then
begin
Invalidate;
SetText(s);
end;
Dialog Winform 的执行定义如下:
method InputMsgBox.Execute(Title, theMessage, defaultanswer:string;var thevalue:string): DialogResult;
begin
result := DialogResult.Cancel;
Requesttext.Text:=themessage;
Requesttext.Enabled:=true;
Requesttext.Visible:=true;
InputTextBox.Text:=defaultanswer;
InputTextBox.Enabled:=true;
InputTextBox.Visible:=true;
CancelBtn.Enabled:=true;
CancelBtn.Visible:=true;
Okbtn.Enabled:=true;
Okbtn.Visible:=true;
self.ShowDialog;
Result := self.DialogResult;
thevalue:=InputTextBox.Text;
end;
当execute 方法返回到调用者时,即使单击OKBtn,它也总是返回DialogResult.Cancel。
相应地设置按钮的对话框结果。
我已经在对话框 winform 上设置了 AcceptButton 和 CancelButton。
我不明白为什么 showdialog 方法总是返回 DialogResult.Cancel。
更新 经过一些测试,我发现我的另一个自定义对话框窗口在通过调用 showdialog = DialogResult.Ok 显示时工作正常。于是,我检查了它们的属性设置是否有一些差异,并且绝对没有差异。我不明白。 提前致谢,
I have a custom made dialog winform. On it I have a label, textbox and 2 buttons (OK and Cancel). It also declares and defines overloaded execute methods for passing different parameter list.
Dialog Winform is called as follows:
var theDialog := new InputMsgBox;
if theInputB.Execute('Awesome StackOverflow','Enter Text?',s,var s) = DialogResult.OK then
begin
Invalidate;
SetText(s);
end;
Dialog Winform execute is defined as follow:
method InputMsgBox.Execute(Title, theMessage, defaultanswer:string;var thevalue:string): DialogResult;
begin
result := DialogResult.Cancel;
Requesttext.Text:=themessage;
Requesttext.Enabled:=true;
Requesttext.Visible:=true;
InputTextBox.Text:=defaultanswer;
InputTextBox.Enabled:=true;
InputTextBox.Visible:=true;
CancelBtn.Enabled:=true;
CancelBtn.Visible:=true;
Okbtn.Enabled:=true;
Okbtn.Visible:=true;
self.ShowDialog;
Result := self.DialogResult;
thevalue:=InputTextBox.Text;
end;
When execute method returns back to the caller, it always returns DialogResult.Cancel even when you click on OKBtn.
The Buttons' dialogresult are set accordingly.
I have set the AcceptButton and CancelButton on the Dialog winform.
I can't figure out why the showdialog method is always returning DialogResult.Cancel.
UPDATE After doing some test, I found out that my other custom-made dialog window works fine when display by calling showdialog = DialogResult.Ok. So, I checked both of them to see if there are some differences in their properties setting and there is absolutely no difference. I don't understand it.
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了我的问题。这就是为什么 Dialog Form 总是返回 Cancel DialogResult 的原因。对于我的定制对话框窗口,我还实现了 Form_Closing 事件方法。显然,当使用 ShowDialog 方法打开对话框时,不会发生使程序计数器跳出其步骤的事件。因此,在它有机会读取其 DialogResult 之前它就已经关闭了。因此,我的程序只看到对话框窗口的默认 DialogResult。
一旦我删除了 Form_Closing 事件方法,它就会按照预期的方式工作。
我没有删除这个问题,而是回答它,我觉得这会对其他人有所帮助。
I figured out my problem. This is why Dialog Form always returned Cancel DialogResult. For my custom-made dialog window, I also implemented Form_Closing event method. Apparently, you are not to have an event that will jump the program counter out of its steps when opening a dialog using ShowDialog method. So, it was already closed before it got a chance to read its DialogResult. Therefore, my program only saw the default DialogResult for my Dialog Window.
Once I removed the Form_Closing event method, it is working the way it is supposed to.
Instead of deleting the question, I am answering it and I feel it will help others.
您应该设置不同的
DialogResult
- 例如,将AcceptButton
的DialogResult
属性设置为DialogResult.OK
。或者以编程方式设置它,通常在按钮事件处理程序中。
我想
DialogResult.Cancel
是默认值,您永远不会更改它。You should set a different
DialogResult
- for example, by setting theDialogResult
property of yourAcceptButton
toDialogResult.OK
.Or set it programatically, often in a button event handler.
I imagine
DialogResult.Cancel
is the default, and you're never changing it.