![](/img/trans.png)
[英]How to modify a PictureBox from another class in Windows Form? C#
[英]use PictureBox from another class c#
我希望我的問題會很清楚
在 Form1.cs 中,我有一個名為:ico_ok 的圖片框
我想在我建立的新班級中使用這個 PictureBox。 當我開始輸入 ico 時……什么也沒有出現。 在另一個類中使用這個對象的方法是什么?
這里的代碼:
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);
}
之后看看新類:
namespace ChekingOUinActiveDirectory
{
class CheckOUinADexist
{
public CheckOUinADexist(string userID)
{
//this place i would like to use ico_ok
}
}
}
感謝您的幫助。 瑪雅人
最簡單的方法可能是為該類提供對PictureBox
的依賴:
public CheckOUinADexist(string userID, PictureBox pbox)
{
pbox.[your code]
}
然后在調用方法時提供它:
CheckOUinADexist checkou = new CheckOUinADexist(userID, ico_ok);
這是否是理想的方法取決於您將在該對象內使用該PictureBox
做什么,該對象需要跨技術平台的可移植性等。
通常,您不希望 UI 元素滲透到非 UI 邏輯中。 如果CheckOUinADexist
是一個 UI 綁定的類並且存在只是為了幫助 UI,那么這不是問題。 如果它是業務邏輯的一部分,那么您不會希望將該邏輯與 UI 技術結合起來。 相反,您可能會將PictureBox
所需的數據傳遞給它,而不是PictureBox
本身。
這在很大程度上取決於您在這里嘗試實現的整體架構,我們不知道。
基本上你會給目標類一個對“共享數據”的引用——在這種情況下是圖片框。
class CheckOUinADexist
{
PictureBox _picBox
public CheckOUinADexist(string userID, PictureBox picBox)
{
//this place i would like to use ico_ok
_picBox = picBox;
_picBox.myAction();
}
}
是否要將Picturebox
實際存儲為字段(而不是僅使用參數)取決於您是否需要在實例的整個生命周期中訪問該字段,或者是否僅用於對象構造。 如果你不確定,你更安全(恕我直言)只是在一個字段中存儲一個引用。 使進一步使用它變得更加容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.