[英]Change pictureBox.Image with c# = FileNotFoundException
[英]Visual C# cannot change picturebox.Image on click
我在Visual Studio 2010(在Visual C#上)的應用程序上有一個圖片框。
圖片框默認情況下具有圖像。 我希望用戶能夠單擊它並將其更改為另一個圖像,然后如果他再次單擊它,則picturebox.Image將變回第一個圖像,依此類推。
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == WindowsFormsApplication2.Properties.Resources.English_flag)
{
pictureBox1.Image = WindowsFormsApplication2.Properties.Resources.Greek_flag;
}
else
{
pictureBox1.Image = WindowsFormsApplication2.Properties.Resources.English_flag;
}
}
這是我所擁有的,但是不起作用。 我知道我的if語句出了點問題,但我不知道是什么。
編輯:第一個圖像出現在我的窗體上,但是當我單擊它時,它不會更改為第二個圖像。
您應該嘗試的是一個布爾標志,您可以在每次單擊時進行切換。
bool flag = true;
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = flag ? WindowsFormsApplication2.Properties.Resources.Greek_flag
: WindowsFormsApplication2.Properties.Resources.English_flag;
flag = !flag;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image first;
private Image reitmi;
private Image reitmi2;
private Image selectForCancel;
private void Form2_Load(object sender, EventArgs e)
{
first = Properties.Resources.Open;
reitmi = Properties.Resources.Select;
reitmi2 = Properties.Resources.Reserve;
pictureBox1.Image = Properties.Resources.Open;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (pictureBox2.Image == first)
{
pictureBox2.Image = reitmi;
listHoldingSeats.Items.Add("B1");
txtListCount.Text =listHoldingSeats.Items.Count.ToString();
}
else
{
pictureBox2.Image = first;
listCancelledList.Items.Add("B1");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.