PDA

View Full Version : چگونه با هر بار کلیک کردن روی Picture Box تصویر آن را تغییر دهم



MostafaMohammadi
دوشنبه 28 مهر 1393, 18:56 عصر
می خوام یه کاری کنم هر بار کاربر کلیک کرد از تصاویر 1 تا 9 به صورت گردشی روی Picture box قرار بگیرن. این کد رو نوشتم ارور میده، فکر می کنم از طریقه آدرس دهی عکس هام ایراد گرفته. چطوری باید عکسی که تو پوشه Resources هاست رو آدرس بدم؟
byte[] tile = new byte[9];
private void pictureBox1_Click(object sender, EventArgs e)
{
tile[1]++;
switch (tile[1])
{
case 1:
pictureBox1.Image = Image.FromFile("../T1.jpg");
break;
case 2:
pictureBox1.Image = Image.FromFile("/T2.jpg");
break;
case 3:
pictureBox1.Image = Image.FromFile("/T3.jpg");
break;
case 4:
pictureBox1.Image = Image.FromFile("/T4.jpg");
break;
case 5:
pictureBox1.Image = Image.FromFile("/T5.jpg");
break;
case 6:
pictureBox1.Image = Image.FromFile("/T6.jpg");
break;
case 7:
pictureBox1.Image = Image.FromFile("T7.jpg");
break;
case 8:
pictureBox1.Image = Image.FromFile("/T8.jpg");
break;
case 9:
tile[1]=0;
pictureBox1.Image = Image.FromFile("/T0.jpg");
break;
}

124763
عکس هام اینجان ولی هر جور آدرس میدم ارور میده

hamid_hr
دوشنبه 28 مهر 1393, 19:12 عصر
اینطوری ادرس بده ببین نمیشه

Properties.Resources.T0

MostafaMohammadi
دوشنبه 28 مهر 1393, 19:35 عصر
خیلی ممنون، اینطوری آدرس دادم اررور آدرس دهی اش رفع شد ولی به اینکه tile رو اونجا تعریف کردم گیر داد. من کجا تعریف اش کنم درست باشه پس؟
namespace _8_puzzleTEST
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
public int[] tile = new int[9];
for (int i = 0; i < 9; i++)
{
tile[i] = i;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
tile[1]++;
switch (tile[1])
{
case 1:
pictureBox1.Image = Properties.Resources.T1;
break;
case 2:
pictureBox1.Image = Properties.Resources.T2;
break;
case 3:
pictureBox1.Image = Properties.Resources.T3;
break;
case 4:
pictureBox1.Image = Properties.Resources.T4;
break;
case 5:
pictureBox1.Image = Properties.Resources.T5;
break;
case 6:
pictureBox1.Image = Properties.Resources.T6;
break;
case 7:
pictureBox1.Image = Properties.Resources.T7;
break;
case 8:
pictureBox1.Image = Properties.Resources.T8;
break;
case 9:
tile[1]=0;
pictureBox1.Image = Properties.Resources.T0;
break;
}
}

MostafaMohammadi
دوشنبه 28 مهر 1393, 20:40 عصر
مشکل متغییر تعریف کردنم هم حل شد، یه شاخه بالاتر تعریف کردم Scopeاش جور در اومد.
namespace _8_puzzleTEST
{
public partial class Form1 : Form
{
int[] tile = new int[9];
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{

}
private void pictureBox1_Click(object sender, EventArgs e)
{
tile[1]++;
switch (tile[1])
{
case 1:
pictureBox1.Image = Properties.Resources.T1;
break;
case 2:
pictureBox1.Image = Properties.Resources.T2;
break;
case 3:
pictureBox1.Image = Properties.Resources.T3;
break;
case 4:
pictureBox1.Image = Properties.Resources.T4;
break;
case 5:
pictureBox1.Image = Properties.Resources.T5;
break;
case 6:
pictureBox1.Image = Properties.Resources.T6;
break;
case 7:
pictureBox1.Image = Properties.Resources.T7;
break;
case 8:
pictureBox1.Image = Properties.Resources.T8;
break;
case 9:
tile[1]=0;
pictureBox1.Image = Properties.Resources.T0;
break;
}
}

حالا می خوام اون T0، T1 و ... به جای عدد هاش متغییر بزارم، ممکنه این کار یا باید نوع آدرس دهی ام رو عوض کنم.

meysamsh91
دوشنبه 28 مهر 1393, 20:47 عصر
سلام
برای اینکه سرعت عمل بالاتر بره یک imageList به پروژه اضافه کنید عکس هاتون رو در imageList اضافه کنید و به این شیوه عمل کنید.

int n=0;
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.BackgroundImage = imageList1.Images[n];
n++;
if (n == imageList1.Images.Count)
n = 0;
}