简体   繁体   English

使用另一个类 c# 中的 PictureBox

[英]use PictureBox from another class c#

I hope my question will be clear我希望我的问题会很清楚

In Form1.cs i have PictureBox named: ico_ok在 Form1.cs 中,我有一个名为:ico_ok 的图片框

i would like to use this PictureBox in my new class that i bulit.我想在我建立的新班级中使用这个 PictureBox。 when i start typing ico... nothing appears.当我开始输入 ico 时……什么也没有出现。 what is the way to use this object in another class?在另一个类中使用这个对象的方法是什么?

here the code:这里的代码:

public void button2_Click(object sender, EventArgs e)
    {
        lbl_check.Visible = true;
        btn_continue.Visible = false;
        txtbox_cusnumber.Enabled = false;
        string userID = (txtbox_cusnumber.Text.ToString());

        CheckOUinADexist checkou = new CheckOUinADexist(userID);
    }

after that look at the new class:之后看看新类:

namespace ChekingOUinActiveDirectory

{ {

      class CheckOUinADexist
      {

              public CheckOUinADexist(string userID)
              {
              //this place i would like to use ico_ok
              }

 }

} }

Thank you for helping.感谢您的帮助。 Maayan玛雅人

The simplest approach is probably to provide that class with the dependency on the PictureBox :最简单的方法可能是为该类提供对PictureBox的依赖:

public CheckOUinADexist(string userID, PictureBox pbox)
{
    pbox.[your code]
}

Then supply it when calling the method:然后在调用方法时提供它:

CheckOUinADexist checkou = new CheckOUinADexist(userID, ico_ok);

Whether or not this is the ideal approach depends on what you're going to be doing with that PictureBox inside that object, how portable that object needs to be across technology platforms, etc.这是否是理想的方法取决于您将在该对象内使用该PictureBox做什么,该对象需要跨技术平台的可移植性等。

In general you don't want UI elements to permeate into non-UI logic.通常,您不希望 UI 元素渗透到非 UI 逻辑中。 If CheckOUinADexist is a UI-bound class and exists solely to help the UI, then this isn't a problem.如果CheckOUinADexist是一个 UI 绑定的类并且存在只是为了帮助 UI,那么这不是问题。 If it's part of business logic then you wouldn't want to couple that logic with the UI technology.如果它是业务逻辑的一部分,那么您不会希望将该逻辑与 UI 技术结合起来。 Instead, you'd likely pass it the data needed from the PictureBox , but not the PictureBox itself.相反,您可能会将PictureBox所需的数据传递给它,而不是PictureBox本身。

This all depends a lot on the overall architecture of what you're trying to achieve here, which we don't know.这在很大程度上取决于您在这里尝试实现的整体架构,我们不知道。

Basically you'd give the target class a reference to the "shared data" -- picture box in this case.基本上你会给目标类一个对“共享数据”的引用——在这种情况下是图片框。

class CheckOUinADexist
{
    PictureBox _picBox
    public CheckOUinADexist(string userID, PictureBox picBox)
    {
        //this place i would like to use ico_ok
        _picBox = picBox;
        _picBox.myAction();
    }
}

Whether you want to actually stored Picturebox as a field (as opposed to just use a parameter) depends on whether you need access to the field throughout the lifetime of the instance(s) or whether it is just needed for object construction.是否要将Picturebox实际存储为字段(而不是仅使用参数)取决于您是否需要在实例的整个生命周期中访问该字段,或者是否仅用于对象构造。 If you are not sure, you are safer (IMHO) just storing a reference in a field.如果你不确定,你更安全(恕我直言)只是在一个字段中存储一个引用。 Make further uses of it a lot easier.使进一步使用它变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM