如何以静态方法访问当前表单的图片框? [英] How to access the current form's picturebox in a static method?

查看:107
本文介绍了如何以静态方法访问当前表单的图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有静态方法



Suppose I have a static method

static void GetImage(ref Byte pbyBuffer) // can not modified the function,which is provided by third parties
{
   //do sth,and then get a bitmap 
   //bitmap Now want to assign to the current form'picturebox ,how to do?
}

推荐答案

这不是正确的方法。



您应该将该Bitmap返回给调用函数。

在Calling / Parent函数中,您应该将其分配给 PictureBox
No this is not the correct way.

You should return that Bitmap to the calling function.
Inside the Calling/Parent function, you should assign that to PictureBox.


您需要:



1.如Tadit Dash所示,修改静态方法,使其返回一个位图/图像给调用者...或者......



2.传递一个对位图/的PictureBox的引用将图像作为参数分配给静态方法。



请记住静态方法属于类它定义如下:它在类的所有实例中是可见的/可用的。



考虑这个类:
You need to either:

1. as Tadit Dash suggests, modify the static method so it returns a bitmap/image to the caller ... or ...

2. pass a reference to the PictureBox to which the bitmap/image is to be assigned to the static method as a parameter.

Keep in mind that a static method "belongs" to the Class it is defined in: it is visible/usable from/in all instances of the Class.

Consider this Class:
public class Class1
{
    public string ID { get; private set; }

    public Class1(string id)
    {
        ID = id;
    }

    public static void showID()
    {
        // error:
        // Keyword 'this' is not valid in a static property, 
        // static method, or static field initializer
        // MessageBox.Show(this.ID);

        // error:
        // An object reference is required for the non-static field, 
        // method, or property
        // MessageBox.Show(ID);
    }
}

什么这表明静态方法没有知道调用它的类的哪个实例:你必须显式地将对当前类的引用传递给静态方法。或者,您需要另一个定义为static的变量/字段,其中包含对静态方法可以访问的当前类的引用。

What this demonstrates is that a static method has no "knowledge" of which instance of the Class it was called from: you must explicitly pass a reference to the current Class to the static method. Or, you need another variable/field defined as static that holds a reference to the current Class which the static method can access.


这篇关于如何以静态方法访问当前表单的图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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