在 C# 中的特定位置获取面板 [英] get panel at specific positions in C#

查看:26
本文介绍了在 C# 中的特定位置获取面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了例如 25 个像网格一样的面板(5 行,5 列),如果我点击我想要获得的其中之一,例如上面的面板也是.起初,我将面板命名为它们的位置,例如面板X1Y1、面板X1Y2 ...然后我从名称中取出坐标并创建了新坐标...获得新名称后,我使用 foreach 循环遍历所有项目并获得具有正确名称的项目.

ive got for example 25 panels that are formed like a grid (5 rows, 5 columns), if i click at one of those i want to get e.g. the panel above too. at first i named the panels like their positions e.g. PanelX1Y1, PanelX1Y2 ... then i took the coords out of the name and created the new coords... and after i got the new name, i used a foreach loop to go through all the items and get that one with the right name.

我已经尝试过使用 winforms 的位置,所以我采用了点击的一个 addet 的位置,例如25 像素,浏览表单中的所有项目,并通过它们的位置检查它们.

ive already tried it with the winforms positions so i took the positions of the clicked one addet e.g. 25 pixels and lopped through all the items in form and checked them via their location.

但我不想遍历所有项目...如果我知道它的名字,我怎么能得到它的名字 whitout 循环遍历所有项目并检查它们的名字..

But i dont want to looped through all the items ... how can i get the item if i know its name whitout to loop through all items and check for their names..

我可以用这个吗:this.Controls["name"];

can i use this: this.Controls["name"];

好的,我的网格:p11 p12 p13p21 p22 p23p31 p32 p33

okay my grid: p11 p12 p13 p21 p22 p23 p31 p32 p33

如果我点击 p31 我想在 p31 和 p21 上改变一些东西所以我需要对象 p21

if i click on p31 i want to change something at p31 and p21 so i need the object p21

推荐答案

要直接回答您的问题,您可以使用 ControlCollection 类的 Find() 方法.例如:

To directly answer you question, you can use the Find() method of the ControlCollection class. e.g.:

myForm.Controls.Find("panelX1Y2")

为了提出更好的方法,请勿将字符串用于此类.它是骇人听闻和草率的.

To suggest a better method, don't use strings for something like this. It is hackish and sloppy.

相反,在二维数组中初始化面板,并使用数组索引找到正确的面板.

这篇关于在 C# 中的特定位置获取面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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