如何在事件处理程序中访问richtextboxcontrol. [英] how to access richtextboxcontrol in eventhandler....
问题描述
我正在使用SharpPcap库捕获数据包..
在事件处理程序device_OnPacketArrival(对象发送方,CaptureEventArgs e)中
我试图在Richtextbox控件中显示值,但是出现了这样的错误:非静态字段,方法或属性" packet_capture.Form1.richTextBox1"需要对象引用.
因此,我该怎么办,给我一些代码来克服此问题..
i am using SharpPcap library to capture packets..
in eventhandler device_OnPacketArrival(object sender, CaptureEventArgs e)
i am trying to display values in richtextbox control but there was error like this:"An object reference is required for the non-static field, method, or property ''packet_capture.Form1.richTextBox1".
so what should i do, give me some code to overcome this problem..
推荐答案
事件处理程序是否是Form1的成员?如果是这样,您可以使用此
来引用表单.目前,您似乎正在尝试通过类型访问属性,除非该属性为静态属性,否则该属性将不起作用.
尝试这样的事情:
Is the event handler a member of Form1? If so, you can usethis
to reference the form. At the moment you appear to be trying to access the property through a Type, which doesn''t work unless the property is static, which it obviously isn''t.
Try something like this:
this.ricTextBox1
如果此处理程序不是Form1的成员,则可能需要使用
If this handler isn''t a member of Form1, you are probably going to need to use the
Application.OpenForms
属性来查找对该表单的引用.
property to find a reference to the form.
在编写任何分组代码之前,您应该考虑学习一些基本的编程技能.该错误非常简单,无论出于何种原因,您都有一个静态方法,这意味着您只能访问静态属性而无需使用实例.出于相同的原因,"this"在静态方法中将不起作用.
You should consider learning some basic programming skills, before you write any packet code. The error is very simple, you have a static method, for whatever reason, and that means you can only access static properties without an instance to work with. ''this'' will not work inside a static method, for the same reason.
这篇关于如何在事件处理程序中访问richtextboxcontrol.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!