0
我是Windows窗體的新手。我試圖實例化一個公共類的對象,並在button1被按下時調用一個方法drawBoard()
。方法drawBoard()
我想通過它設置pictureBox2的屬性。但代碼無效。在窗體類下實例化一個類的對象
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class gameBoard :Form1
{
public void drawBoard()
{
pictureBox2.ImageLocation = @"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
}
}
private void button1_Click(object sender, EventArgs e)
{
gameBoard a = new gameBoard();
a.drawBoard();
}
}
還試圖在其他兩種方式來實現這一點:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox2.ImageLocation = @"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.BackColor = Color.Transparent;
}
}
和
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.ImageLocation = @"E:\My Data\DoCx\CS\3rd Sem\OOP\proj\images\a.png";
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
}
}
都工作。代碼運行時直接加載圖像。而且當按鈕1以第二種方式按下時。我想知道如何在用戶定義的類的方法中定義時調用pictureBox屬性。
你的內部類gameBoard不知道任何關於名爲pictureBox2的變量。它看不到這是在容器類中定義的東西。調用它時,將pictureBox2傳遞給drawBoard方法 – Steve