简体   繁体   English

通过标签查找特定按钮的最佳方法是什么?

[英]What is the best way to find a certain button by a tag?

I am making a chess game and I do this by giving every button a tag with his x and y: 我正在做一个国际象棋游戏,我通过给每个按钮一个带有x和y的标签来做到这一点:

button2.Tag = "21";

and then if I want to find the button above it I want to look for the button with the tag "22". 然后,如果要在其上方找到按钮,则要查找带有标签“ 22”的按钮。

But... How do I do that? 但是...我该怎么做?

You could have all the buttons in a list and then do a search with a foreach or use linq with where to get the one with the tag number you are looking for. 您可以将所有按钮都放在列表中,然后使用foreach进行搜索,或者使用linq以及在哪里获得带有您要查找的标签号的按钮。

In case you are in winforms and dont want to create a list of buttons by yourself, another approach is to have a panel that contains all the buttons in the designer. 如果您使用的是Winforms,并且不想自己创建按钮列表,则另一种方法是使用一个包含设计器中所有按钮的面板。 And then in you code you could do panel.Controls and that gives you access to all the controls that are within the panel. 然后,在代码中可以执行panel.Controls,使您可以访问面板中的所有控件。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM