C# 使用数组更改picturebox的背景色
我是c#编程的初学者。我将用c#制作游戏。在这种情况下,我想在单击图片框时更改颜色。这是我的代码C# 使用数组更改picturebox的背景色,c#,mysql,arrays,image-processing,arduino,C#,Mysql,Arrays,Image Processing,Arduino,我是c#编程的初学者。我将用c#制作游戏。在这种情况下,我想在单击图片框时更改颜色。这是我的代码 private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int[] R = { 0, 255, 255, 34, 249,255 }; int[] G = { 0, 255, 0, 235, 255 ,153}; int[] B= { 255
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int[] R = { 0, 255, 255, 34, 249,255 };
int[] G = { 0, 255, 0, 235, 255 ,153};
int[] B= { 255, 255, 0, 27, 40,51 };
pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]);
this.index++;
}
此代码有效。当我单击picturebox时,颜色发生了变化。根据数组R,G,B可以得到6种颜色。在最后一种颜色之后,我收到此消息。。
索引超出了数组的边界。
请有人告诉我这个问题的解决方法。谢谢…我找到了方法
int[] R = { 0, 255, 255, 34, 249, 255 };
int[] G = { 0, 255, 0, 235, 255, 153 };
int[] B = { 255, 255, 0, 27, 40, 51 };
pictureBox2.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]);
this.index++;
if (this.index == 6)
{
this.index -= 6;
}
在第六种颜色之后。同样,我们可以得到第一种颜色数组索引,从0开始,它的长度是6,因此只需像这样在代码周围换行
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if(this.index <= 5){
int[] R = { 0, 255, 255, 34, 249,255 };
int[] G = { 0, 255, 0, 235, 255 ,153};
int[] B= { 255, 255, 0, 27, 40,51 };
pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]);
this.index++;
}
}
private void pictureBox1\u鼠标单击(对象发送器,MouseEventArgs e)
{
如果(this.index请用这个试试。
你需要考虑数组的长度。
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int[] R = { 0, 255, 255, 34, 249,255 };
int[] G = { 0, 255, 0, 235, 255 ,153};
int[] B= { 255, 255, 0, 27, 40,51 };
if(this.index <= (R.Length - 1) ||this.index <= (G.Length - 1) || this.index <= (B.Length - 1))
{
pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]);
this.index++;
}
else
{
// Do nothing since you don't have colors
}
private void pictureBox1\u鼠标单击(对象发送器,MouseEventArgs e)
{
int[]R={0,255,255,34,249255};
int[]G={0,255,0,235,255,153};
int[]B={255,255,0,27,40,51};
如果(this.index一个解决方案是只单击5次-),或者您需要环绕索引,即当它点击6时将其重置为0:index=(index+1)%6;
注意,%
是我将检查的。谢谢this.index-=6;
如果您只需编写this.index=0;
就更容易理解了;现在大家都可以看到您正在重置索引。