2013-10-03 182 views
1

我正面臨一個奇怪的問題。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: 
+1

構造函數中的代碼是什麼。 –

+0

@ AlessandroD'Andria'InitializeComponent()'(自動生成)併爲我設置的2個參數。在父窗體上,只有'InitializeComponent()'。 – Mauren

+0

您的構造函數正在做太多工作,並且在調用Show/Dialog()之前獲取創建的本機窗口。這很不健康,你沒有留下面包屑爲什麼會出現這種情況。給對話一個明確的所有者,而不是讓Winforms找到一個是解決方法,使用ShowDialog(this)。 –

回答

1

如果您不通過form.ShowDialog()方法中的參數,則默認將當前窗口/窗體作爲所有者。所以你可以嘗試一下。

form.ShowDialog(App.OpenForms[0]); // as default form 

form.ShowDialog(this); // to make the current window as owner for the form. 

編輯::

方式,一般的Windows(Win32和WinForms.Net)處理,這是等待的MessageQueue空轉,然後處理所有無效的屏幕區域。這是有效的,因爲當某些變化通常落入其他事物(控制)時也會發生變化。

這是您的答案,您必須爲繪畫向量創建新的用戶控件,並使用usercontrols繪畫事件來完成任務。檢查下面的代碼。

private void button1_Click(object sender, EventArgs e) 
     { 

       Form2 Plot = new Form2(new Point2D(10, 10), new Point2D(100, 100)); 
       Plot.ShowDialog(); 

     } 



public partial class Form2 : Form 
    { 
     private Point2D _originalVector; 
     private Point2D _rotatedVector; 

     public Form2() 
     { 
      InitializeComponent(); 
     } 

     public Form2(Point2D OriginalVector, Point2D RotatedVector) 
     { 
      InitializeComponent(); 

      _originalVector = OriginalVector; 
      _rotatedVector = RotatedVector; 
     } 

     private void userControl11_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); 
       } 

     } 
    } 
+0

'Application.OpenForms [0]'也失敗了。 – Mauren

+0

CHeck更新了答案。 – JSJ

+0

由用戶控制你的意思是WPF的? – Mauren

1

也許你應該提供這個方法的參數。試試:

form.ShowDialog(this); 
+0

我也試過這個。它也失敗了,就像無參數的一樣。 – Mauren

+0

如果您捕捉異常並立即執行第二個「form.ShowDialog();」,會發生什麼情況? –

+0

我試過這個: 'try { Plot.ShowDialog(); } catch(ArgumentException除外) { Plot.ShowDialog(); }' 但它似乎沒有發現異常。 – Mauren

0

也許這樣?

form.ShowDialog(this); 

你可以粘貼你的stacktrace嗎?

+0

不幸的是我現在沒有訪問我的堆棧跟蹤,但是這個也沒有通過。 – Mauren

相關問題