我正面臨一個奇怪的問題。Form.ShowDialog()導致'無效參數'錯誤
我創建了一個包含主窗體和輔助窗體的小型WinForms項目。我創建的代碼的第一個內部的第二形式類似於這樣:
private void btnOpenForm_Click(object sender, EventArgs e)
{
NewForm form = new NewForm(value1, value2);
form.ShowDialog();
}
當我使用ShowDialog()
方法,程序與Invalid parameter
錯誤方法執行後立即失效,而普通Show()
方法工作正常。通過stacktrace,它說我不能使用與所有者相同的表單,但是我應該如何顯示一個模態對話框,從主窗體調用?
主窗口構造:
public MainWindow()
{
InitializeComponent();
}
輔助形式構造:
public VectorPlot(Point2D OriginalVector, Point2D RotatedVector)
{
InitializeComponent();
_originalVector = OriginalVector;
_rotatedVector = RotatedVector;
}
主窗口調用輔助窗口:
VectorPlot Plot = new VectorPlot(OriginalVector, ModifiedVector);
Plot.ShowDialog();
Paint事件:
private void VectorPlot_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
GraphicsContainer container = g.BeginContainer(new RectangleF(0f, 0f, 300f, 300f), new RectangleF(0f, 0f, 300f, 300f), GraphicsUnit.Pixel);
g.FillRectangle(new SolidBrush(Color.White), new RectangleF(0f, 0f, 300f, 350f));
g.DrawLine(new Pen(Color.Gray), 150f, 0f, 150f, 300f);
g.DrawLine(new Pen(Color.Gray), 0f, 150f, 300f, 150f);
float xOrigVect = (float)_originalVector.X;
float yOrigVect = (float)_originalVector.Y;
float xRotVect = (float)_rotatedVector.X;
float yRotVect = (float)_rotatedVector.Y;
xOrigVect = 150f + xOrigVect;
yOrigVect = 150f - yOrigVect;
xRotVect = 150f + xRotVect;
yRotVect = 150f - yRotVect;
g.DrawLine(new Pen(Color.Blue, 2f), 150f, 150f, xOrigVect, yOrigVect);
g.DrawLine(new Pen(Color.Red, 2f), 150f, 150f, xRotVect, yRotVect);
g.DrawString("Legenda: ", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(0f, 310f));
g.DrawLine(new Pen(Color.Blue, 2f), 0f, 330f, 10f, 330f);
g.DrawString("Vetor original", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(10f, 322f));
g.DrawLine(new Pen(Color.Red, 2f), 0f, 340f, 10f, 340f);
g.DrawString("Vetor rotacionado", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(10f, 332f));
g.EndContainer(container);
}
}
堆棧跟蹤:
System.ArgumentException was unhandled
HResult=-2147024809
Message=Parâmetro inválido.
Source=System.Drawing
StackTrace:
em System.Drawing.Graphics.get_Clip()
em System.Drawing.Graphics.GetContextInfo()
em System.Windows.Forms.Internal.WindowsGraphics.FromGraphics(Graphics g, ApplyGraphicsProperties properties)
em System.Windows.Forms.WindowsGraphicsWrapper..ctor(IDeviceContext idc, TextFormatFlags flags)
em System.Windows.Forms.VisualStyles.VisualStyleRenderer.DrawBackground(IDeviceContext dc, Rectangle bounds)
em System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
em System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
em System.Windows.Forms.Control.WmPaint(Message& m)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ScrollableControl.WndProc(Message& m)
em System.Windows.Forms.ContainerControl.WndProc(Message& m)
em System.Windows.Forms.Form.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
構造函數中的代碼是什麼。 –
@ AlessandroD'Andria'InitializeComponent()'(自動生成)併爲我設置的2個參數。在父窗體上,只有'InitializeComponent()'。 – Mauren
您的構造函數正在做太多工作,並且在調用Show/Dialog()之前獲取創建的本機窗口。這很不健康,你沒有留下面包屑爲什麼會出現這種情況。給對話一個明確的所有者,而不是讓Winforms找到一個是解決方法,使用ShowDialog(this)。 –