2017-07-08 69 views
-1

对于我的其中一个项目,我需要编写一个程序来显示产品列表,并在其左侧显示一个支票和/或十字。我把一切都整理,除了部分 我也碰到过这个问题的形式:c#中的窗体错误

class Presentation 
{ 
    static Form Window = new Form(); 
    static public void Configuration() 
    { 
     Window.Height = 800; 
     Window.Width = 800; 
     Window.Text = "Homework"; 

     Graphics draw = Window.CreateGraphics(); 

     Window.Paint += Window_Paint(); 

     Window.Show(); 
    } 
    void Window_Paint(System.Object sender, PaintEventArgs e) 
    { 
     AnyOtherConfigurations(e); 
    } 
    static public void AnyOtherConfigurations(PaintEventArgs e) 
    { 
     Pen pen1 = new Pen(Color.Red); 
     Font font = new Font(FontFamily.GenericSansSerif, 8, 
     FontStyle.Regular, GraphicsUnit.Millimeter); 
     Graphics draw = Window.CreateGraphics(); 
     e.Graphics.FillEllipse(new SolidBrush(Color.Yellow),x,y,80,80); 
     draw.DrawRectangle(pen1, 0, 0, 90, 90); 

    } 
} 

它给我,说错误:

CS7036 There is no argument given that corresponds to the required formal parameter 'sender' of 'UserQuery.Presentation.Window_Paint(object, PaintEventArgs)' 
CS0029 Cannot implicitly convert type 'void' to 'System.Windows.Forms.PaintEventHandler' 

任何人都可以提出一个原因,这可能发生,给我建议如何解决它?

+1

当您附加事件时,您将调用带有零参数的“Window_Paint”。删除parntheses:此时你不想打电话给你的处理程序。 – Richard

+0

_Graphics draw = Window.CreateGraphics(); _和_Graphics draw = Window.CreateGraphics(); _永远不要这样!结果将不会持续!使用已经拥有的'PaintArgs'中的'e.Graphics'! - 切勿使用'control.CreateGraphics'!切勿尝试缓存'Graphics'对象!可以使用'e.Graphics'参数使用'Graphics g = Graphics.FromImage(bmp)'或者在控件的Paint事件中绘制一个'Bitmap bmp'。 – TaW

+0

你可以测试你的持久性通过做一个最小化/最大化序列的图形 – TaW

回答

1

不要写

Object.Event += Method(); 

Object.Event += Method; 

代替。

eventlistener需要引用方法来使用委托,而不是方法本身的返回值。这解释了你的错误。