如果更改RightToLeft,ShowInTaskbar属性,Form.ShowDialog()意外结束
时间:2020-03-06 14:54:39 来源:igfitidea点击:
对话框关闭,并显示"取消"结果,没有例外,就像我们按下了它的关闭按钮一样。
设置RightToLeft属性的唯一安全位置是在窗体构造函数中。
在我看来,这些信息可能会节省别人的时间。
如果我们可以解决此问题:如果有正式的错误确认,还有什么可能导致ShowDialog意外结束,请执行。
回复:靠近托盘MSDN论坛
在运行时更改Form RightToLeft属性
引用第二个链接:
I have found a second bug in less than two days . This new bug is very critical . I have Normal Form with RightToLeft property set to its default value ( RightToLeft=False) . Let us show this form with Show Function ( Form1.Show(me) ) At this Form there is a Button which change Form RightToLeft to Yes instead of No: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.RightToLeft = Windows.Forms.RightToLeft.Yes End Sub The Form will change its Title successfully to Right Side. Up To This there is no problem. Problem Occure as following If we Display this Form to the user using ShowDialog(Me) Function instead of display it using Show(Me) . Then Click Button which will change Form RightToLeft to Yes instead of No , Form will Close Suddenly with no reasons , and even not throw any exceptions . This is the new problem & it's exist also in .NET 3.0 ( Orcase ) Too .
解决方案
好的,我有个快速修复方法。这很讨厌,虽然很hack,但还是可以的。
从我对原始问题的回答:
private bool _rightToLeft; private void SetRTL(bool setRTL) { _rightToLeft = true; ApplyRTL(setRTL, this); } private void ApplyRTL(bool yes, Control startControl) { if ((startControl is Panel) || (startControl is GroupBox)) { foreach (Control control in startControl.Controls) { control.Location = CalculateRTL(control.Location, startControl.Size, control.Size); } } foreach (Control control in startControl.Controls) ApplyRTL(yes, control); } private Point CalculateRTL(Point currentPoint, Size parentSize, Size currentSize) { return new Point(parentSize.Width - currentSize.Width - currentPoint.X, currentPoint.Y); } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_rightToLeft) { _rightToLeft = false; e.Cancel = true; } }
它添加到窗体关闭事件的摇动部分,然后告诉我们如果刚刚进行了从右到左的交换(_rightToLeft),则不要关闭它。告诉它没有关闭,我们删除了从右到左的标志,让生活继续。
- bug:关闭用.Show(this)打开的窗体时,会发生一个bug,但是我敢肯定我们可以解决这个问题!