Eventargs不包含图形定义 - C# [英] Eventargs does not contain a definition for graphics - C#

查看:335
本文介绍了Eventargs不包含图形定义 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'System.EventArgs'不包含'Graphics'的定义,并且没有扩展方法'Graphics'可以找到接受类型'System.EventArgs'的第一个参数(你是否缺少using指令或汇编引用? )。



当我在按钮点击事件中使用图形时出现此错误



请帮助我有这个错误



我的要求:当我点击按钮时,应该绘制一些图形(例如:椭圆形)

Thnk you。



我尝试过:



private void btnsadsmile_Click(object sender,EventArgs e)

{

图形g = e.Graphics; //此处出现错误没有图形定义

Pen p = new Pen( Color.RosyBrown,width:3);

int w = this.ClientSize.Width;

int h = this.ClientSize.Height;

g.DrawEllipse(p,w / 3 + 67,h / 3 + 50,20,10);

}

解决方案

显然该属性不存在,你可以创建一个 Graphics 对象:如何:为绘图创建图形对象 [ ^ ]


点击事件与绘图没有任何关系:因此他们不提供图形上下文。只有Paint事件会为你做这件事。

此外,如果它确实提供了这样的上下文,它在逻辑上是一个上下文,允许你绘制用户点击的Button(或其他控件),不是整个表格。



在Paint事件处理程序中执行您的实际绘图,并通过调用Invalidate拟合您想要绘制的Panel的形式来获取该信号。

'System.EventArgs' does not contain a definition for 'Graphics' and no extension method 'Graphics' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?).

this error was occured when i was using graphics in button click event

please help me with this error

my requirement: when i click the button some graphics(eg: ellipse) should be drawn
Thnk you.

What I have tried:

private void btnsadsmile_Click(object sender, EventArgs e)
{
Graphics g = e.Graphics;// here the error occurs no definition for graphics
Pen p = new Pen(Color.RosyBrown, width: 3);
int w = this.ClientSize.Width;
int h = this.ClientSize.Height;
g.DrawEllipse(p, w / 3 + 67, h / 3 + 50, 20, 10);
}

解决方案

Obviously that property does not exist, you can create a Graphics object with : How to: Create Graphics Objects for Drawing[^]


Click events do not have anything to do with drawing: as a result they do not provide a Graphics context. Only Paint events do that for you.
Besides, if it did provide such a context, it would logically be a context which allows you to draw on the Button (or other control) which the use clicked, not the whole form.

Do your actual drawing in the Paint event handler and get that signaled by calling Invalidate fit the form of Panel you want to draw onto.


这篇关于Eventargs不包含图形定义 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆