C#索引超出了数组界限,问题,高手帮忙

zhangyujiaokimi 2010-10-14 10:18:24
我在写一个俄罗斯方块程序,遇到一个小问题,希望高手帮忙哦~~~~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 俄罗斯方块
{
public partial class FrmConfig : Form
{
public FrmConfig()
{
InitializeComponent();
}
private bool[,] struArr = new bool[5, 5];
private Color blockColor = Color.Red;

private void lblMode_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
gp.Clear (Color .Black );
Pen p=new Pen (Color .White );
SolidBrush s=new SolidBrush (Color .White );
for (int i=31;i<155;i=i+31)
gp.DrawLine (p,1,i,155,i);
for (int i=31;i<155;i=i+31)
gp.DrawLine (p,i,1,i,155);
}

private void lblMode_MouseClick(object sender, MouseEventArgs e)
{
bool a = true;
if (e.Button != MouseButtons.Left)//判断是否点击了鼠标左键,如果不是就退出
return;
int xPos,yPos;//xpos表示数组第一个下标,ypos表示数组第二个下标
xPos = e.X / 3;//把屏幕像素值转换为数组下标
yPos = e.Y / 3;
if (struArr.Count() > 0) a = struArr[0,0];
struArr [xPos ,yPos ]=!struArr [xPos ,yPos ];
bool b=struArr [xPos ,yPos ];//这里索引超出了数组界限
Graphics gp = lblMode.CreateGraphics();//得到lblMode的Graphics
SolidBrush s= new SolidBrush(b? blockColor :Color .Black );//创建一个刷子,并确定它的颜色
gp.FillRectangle(s,31*xPos +1,31*yPos +1,30,30);
gp.Dispose();

}
}
}
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2010-10-14
  • 打赏
  • 举报
回复
这里就应该越界了哈
struArr [xPos ,yPos ]=!struArr [xPos ,yPos ];

struArr你定义的是5个元素

xPos = e.X / 3;//把屏幕像素值转换为数组下标
yPos = e.Y / 3;

看看e.X, e.Y是不是就超过了你的设计值?
wuyq11 2010-10-14
  • 打赏
  • 举报
回复
断点。看看xPos ,yPos 值

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧