如何在C#一个循环命名多个按钮 [英] How to rename multiple buttons in one loop in C#

查看:134
本文介绍了如何在C#一个循环命名多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个战舰般的程序,其中有按钮的10×10格。在节目的开始,我想所有的按钮的文字更改为---,这表明没有人开枪的坐标。我似乎无法找到一个方法来重命名所有的按钮在一个循环中。按钮都有名字B00,B01,B02 ......这表明它们的坐标。第一个数字是行,第二个数字是列。 (即B69再presents第7行,10列)。

I have a battleship-like program where there is a 10 x 10 grid of buttons. In the beginning of the program, i would like all of the buttons' text to be changed to "---", which shows that no one has shot at that coordinate. I can't seem to find a way to rename all of the buttons in one loop. The buttons all have names b00, b01, b02... which show their coordinates. the first number is the row and the second number is the column. (i.e. b69 represents row 7, column 10).

希望能帮到你!

感谢你在前进!

推荐答案

您也可以使用的扩展方法的OfType()滤波基于指定type.See在下一个例子

You can also use Extension Method OfType() to filtering based on specified type.See the next example

foreach (Button btn in this.Controls.OfType<Button>())
   {

   btn.Text="New Name";
   }

,你可以通过使用OfType扩展方法看你不需要铸造控制按钮类型

as you can see by using OfType extension method you do not need to casting the control to Button Type

希望帮助。

问候

这篇关于如何在C#一个循环命名多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆