2013-08-04 38 views
0

所以這應該很簡單,但我已經看過一些類似的問題,找不到答案。從Panel_Paint中調用另一個類的方法不會繪製任何東西

我有Form1類和Resistor類。在Form1類中,我有一個Panel(我將名稱更改爲Canvas),在Canvas_Paint方法中,我從Resistor類調​​用方法Draw,但沒有畫任何東西。

Form1類:

public partial class Form1 : Form 
{ 
    static float lineWidth = 2.0F; 
    static float backgroundLineWidth = 2.0F; 
    static Pen pen = new Pen(Color.Yellow, lineWidth); 
    static Pen backgroundPen = new Pen(Color.LightGray, backgroundLineWidth); 
    private bool drawBackground = true; 
    private List<Resistor> resistors = new List<Resistor>();     

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Canvas_Paint(object sender, PaintEventArgs e) 
    { 
     if (drawBackground) 
     { 
      Console.WriteLine("Drawing background..."); 
      Draw_Background(e.Graphics, backgroundPen); 
     } 

     if (resistors != null) 
     { 
      foreach (Resistor r in resistors) 
      { 
       //This does not work. 
       r.Draw(e.Graphics); 
      } 
     } 

     //The line below draws the line fine. 
     e.Graphics.DrawLine(pen, 0, 0, 100, 100); 
    } 

    private void Draw_Background(Graphics g, Pen pen) 
    {    
     for (int i = 0; i < Canvas.Width; i += 10) 
     { 
      g.DrawLine(pen, new Point(i, 0), new Point(i, Canvas.Height));   
     } 
     for (int j = 0; j < Canvas.Height; j += 10) 
     { 
      g.DrawLine(pen, new Point(0, j), new Point(Canvas.Width, j)); 
     } 

     drawBackground = false; 
    } 

    private void AddResistor_Click(object sender, EventArgs e) 
    { 
     resistors.Add(new Resistor()); 
     Console.WriteLine("Added a Resistor..."); 
    }   
} 

電阻類別:

public class Resistor 
{ 
    static private Point startingPoint; 
    static Pen defaultPen; 
    private Point[] points; 

    public Resistor() 
    { 
     startingPoint.X = 100; 
     startingPoint.Y = 100; 

     defaultPen = new Pen(Color.Yellow, 2.0F); 

     points = new Point[] { 
      new Point(10, 10), 
      new Point(10, 100), 
      new Point(200, 50), 
      new Point(250, 300) 
     }; 

    } 

    public void Draw(Graphics g) 
    { 
     //Is this drawing somewhere else? 
     g.DrawLines(defaultPen, points);    
    } 
} 

我已經搜索這question這表明在這種情況下,通過e.Graphics對象到Draw方法在Resistor類,但是不工作。

我是C#的新手,所以我非常感謝任何幫助。

編輯: 如果你想下載並試用它,我把項目放在github上。

編輯: 所以問題是,點擊按鈕後,面板漆方法沒有被調用。解決的辦法是添加Canvas.InvalidateAddResistor_Click方法

+1

您應該總是在'Paint'事件處理程序中使用'e.Graphics'。不惜一切代價避免'CreateGraphics';它不會像你期望的那樣工作。 –

+0

他也忘記訂閱Paint事件。 –

+0

@CodyGray謝謝你,我將它改爲'e.Graphics'。 – 0gravity

回答

0

問題是,當按鈕被點擊時,面板的paint方法沒有被調用,因爲我強硬的是paint方法總是被調用。解決方法是在AddResistor_Click方法內添加Canvas.Invalidate

private void AddResistor_Click(object sender, EventArgs e) 
    { 
     resistors.Add(new Resistor()); 
     Console.WriteLine("Added a Resistor..."); 
     Canvas.Invalidate(); 
    } 
1

內運行在調試你的代碼,把一個斷點在你的事件處理程序,你就可以檢查你的代碼實際上是想畫點什麼。如果不是,那麼你的事件處理程序是否曾被調用?你的電阻列表中有什麼?如果它正在繪製,但你沒有看到任何東西,那麼你沒有使用正確的Graphics上下文,或者你沒有在控件的可見部分繪製東西,或者正在繪製隨後的繪製代碼。

+0

+1:謝謝你的回答,它實際上讓我想到了,我注意到當我點擊按鈕添加電阻時,面板的Paint方法沒有被調用,所以爲了解決這個問題,我添加了一個Canvas.Invalidate在AddResistor_Click方法中。 – 0gravity

相關問題