为什么ShowDialog的总是返回DialogResult.Cancel? [英] Why does ShowDialog always return DialogResult.Cancel?
问题描述
我有一个定制对话框的winform。对此我有一个标签,文本框和2个按钮(确定和取消)。它还声明和定义重载执行方法传递不同的参数列表。
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.
对话框的Winform被称为如下:
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;
对话框的Winform执行定义如下:
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;
在执行方法返回给调用者,它总是返回DialogResult.Cancel甚至当你点击OKBtn。
When execute method returns back to the caller, it always returns DialogResult.Cancel even when you click on OKBtn.
按钮DialogResult的相应设置。
The Buttons' dialogresult are set accordingly.
我已经设置了AcceptButton和CancelButton在对话框的winform。
I have set the AcceptButton and CancelButton on the Dialog winform.
我不明白,为什么ShowDialog方法总是返回DialogResult.Cancel。
I can't figure out why the showdialog method is always returning DialogResult.Cancel.
更新做了一些测试后,我发现我的其他定制对话框窗口工作正常时,显示通过调用ShowDialog的= DialogResult.Ok。所以,我检查他们两个,看看是否有在他们的性能有一定的差异的设置也绝对没有什么区别。我不明白。 在此先感谢,
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,
推荐答案
我想通了,我的问题。这就是为什么对话形式始终返回取消的DialogResult。对于我定做的对话窗口,我还实施Form_Closing事件的方法。很显然,你是不是有这种开放使用ShowDialog方法对话框时,会跳转程序计数器出其步骤的事件。因此,它已经关闭之前,它有机会读到它的DialogResult。因此,我的程序只看到默认的DialogResult我的对话窗口。
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.
在我删除了Form_Closing事件方法,它工作的方式,它应该是。
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.
这篇关于为什么ShowDialog的总是返回DialogResult.Cancel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!