首页 \ 问答 \ c#按钮点击事件。(c# button click event. Extracting the coordinates from a two dimensional array)

c#按钮点击事件。(c# button click event. Extracting the coordinates from a two dimensional array)

我在c#上制作游戏扫雷,我的代码的这部分是检查用户是否按下网格上的某个按钮。 然而,这样做的方式我相信它分割了他按下的按钮的xy坐标。

这意味着代码的工作方式是这里有两个二维数组。 有100个按钮按钮[10,10]。 后面是一个叫做网格的网格[10,10],在网格上我说-1是炸弹,1是空的空间。 我试图从按钮中提取他按下的xy坐标并用它检查网格。 这是下面的代码:

但是,当我按下一个按钮 - 代码int x = System.Convert.ToInt32(split [0]); 我得到'System.FormatException'。

我究竟做错了什么?

void bttnOnclick(object sender, System.EventArgs e)
    {

        Button bttnClick = sender as Button;

        string[] split = bttnClick.Name.Split(new Char[] { ' ' });

        int x = System.Convert.ToInt32(split[0]);
        int y = System.Convert.ToInt32(split[1]);


        if (grid[x, y] == -1)
        {
            //Game Over!
            for (int xx = 0; xx < SizeX; xx++)
            {
                for (int yy = 0; yy < SizeY; yy++)
                {
                    if (grid[xx, yy] == -1)
                    {
                        buttons[xx, yy].Visible = false;
                    }

                }
            }
        }
    }

I am making the game minesweeper on c# and this part of my code is checking to see if the user pressed a certain button on the grid. However, the way to do that I believe it split the x y coordinates of the button he pressed.

Meaning the way the code works is that there are two 2-dimensional array going on here. There are 100 buttons button[10,10]. and behind that is a grid called grid[10,10] and on the grid I said -1 is a bomb and 1 is an empty space. I am trying to extract the x-y coordinate that he presses from the button and check the grid with it. And this is the code below:

However,when I press a button - at code int x = System.Convert.ToInt32(split[0]); I get a 'System.FormatException'.

What am I doing wrong?

void bttnOnclick(object sender, System.EventArgs e)
    {

        Button bttnClick = sender as Button;

        string[] split = bttnClick.Name.Split(new Char[] { ' ' });

        int x = System.Convert.ToInt32(split[0]);
        int y = System.Convert.ToInt32(split[1]);


        if (grid[x, y] == -1)
        {
            //Game Over!
            for (int xx = 0; xx < SizeX; xx++)
            {
                for (int yy = 0; yy < SizeY; yy++)
                {
                    if (grid[xx, yy] == -1)
                    {
                        buttons[xx, yy].Visible = false;
                    }

                }
            }
        }
    }

原文:https://stackoverflow.com/questions/34227832
更新时间:2023-04-13 12:04

最满意答案

以下是对您的问题的非常好的描述http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html


Here is a very good description of your question http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html

相关问答

更多
  • ACTION_BATTERY_LOW是系统的广播操作,因此出于安全原因您将无法生成它 - 至少不是在“常规”设备上 ACTION_BATTERY_LOW is a system's broadcast action, so you won't be able to generate it for security reasons - at least not on a "regular" device
  • 实际上,我相信大部分有电源问题的应用程序,都有“CPU”问题。 也就是说,应用程序的CPU使用情况的配置文件可能是您的电池消耗的很好的近似值。 如果您的应用程序在GPU,无线网络,存储等方面做的很昂贵,而且昂贵的操作并不花费太多的CPU时间,那么请注意和例外。 这是一个关于“Power Tutor”应用程序的有趣博客,它提供了比内置电池应用程序更准确的运行系统测量: http : //gigaom.com/mobile/android-power-consumption-app/ 。 我还没试过 对于另一个 ...
  • 我可以告诉你,当你使用低功率-startMonitoringSignificantLocationChanges时,不能也不需要定时器。 该方法仅在设备检测到更改时响应委托的回调。 这个位置检查不使用GPS,它使用已经发生的Wifi和蜂窝塔三角测量。 因此,通过使用此方法,无需减慢速度以节省电池寿命。 只需设置委托方法并做出相应的响应。 我不确定您的位置实现是什么,但区域监控也是使用很少甚至没有电池来获取位置更新的另一种好方法。 当您具有要监视的特定位置而不仅仅是一般用户位置时,区域监视会更有帮助。 希望这 ...
  • ADM 点亮 。 作为TrackingAlarmReceiver工作的一部分,您可以调用setExactAndAllowWhileIdle()来安排下一步的工作。 请记住,此类事件的最小粒度为〜10分钟IIRC,即使您已获得控制权,您也可能无法访问网络。 ADM is spot-on. As part of the work in TrackingAlarmReceiver, you call setExactAndAllowWhileIdle() to schedule the next bit of w ...
  • 正如雅罗斯拉夫指出的那样 - 这是一个很难回答的问题,因为它需要分析代码(或者有幸能够识别问题的人)。 这个对github问题的评论对于分析来说是一个很好的起点,就像新的TensorFlow Performance页面一样。 As Yaroslav noted - this is a hard question to answer, because it requires profiling of your code (or someone lucky enough to recognize the pro ...
  • //尝试这个 AlarmManager am=(AlarmManager)getApplicationContext getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); am.setRepeating(AlarmManager.RTC_WAKEUP, ...
  • 这取决于你对“更好”的定义。 您无法从清单中注册ACTION_BATTERY_CHANGED广播,因此您必须始终运行一项服务来管理此广播的动态注册接收器。 许多用户不喜欢这个。 AlarmManager避免了对持续运行服务的需求,但这意味着您的应用程序小部件在查找电池电量变化时会稍微滞后。 但是,您可以允许用户通过SharedPreference控制轮询频率,而不是将其硬编码为五分钟。 这样,用户可以控制应用程序从轮询中消耗多少CPU /电池,因此可以控制将会有多少延迟。 That depends upon ...
  • 我做了很多测试,这是结果: -Alarm Manager比使用handler + wakelock节省更多电池以进行长时间操作。 但是,您必须对警报启动的活动/服务使用额外的唤醒锁定,因为警报管理器唤醒锁定不会覆盖它。 即使这种方法使用两个WakeLock,电池似乎更高效,寿命更长! 在测试期间(2天), AlarmManager使用比其他方法少6倍的电池 。 就我个人而言...... 希望这可以帮助一些人! I have made many test and this is the result: -Al ...
  • 以下是对您的问题的非常好的描述http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html Here is a very good description of your question http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
  • 这个功能很难开发,因为它取决于很多因素。 您最好为每个特定平台实施本机电池通知。 有些平台提供有关功耗和剩余电池寿命的全面信息。 这将(可能)导致更好的估计并为您节省大量工作:) iPhone的例子; UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@select ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。