![](/img/trans.png)
[英]Using LINQ, can I give the "grouping" a name in VB.NET like I can in C#?
[英]Can I give controls an index property in C# like in VB?
我之前已經找到了與我的問題類似的答案,但並不完全符合我想要做的......
在 Visual Basic 中(我最后一次使用它,是在 06/07)有一個“索引”屬性,您可以將它分配給多個具有相同名稱的控件。 我主要用它來循環控制,即:
For i = 1 to 500
picSeat(i).Print "Hello"
Next i
有沒有辦法在 C# 中做到這一點? 我知道有一個.IndexOf()
,但這對我正在做的事情真的有幫助嗎? 我想要多個具有相同名稱的控件,只是索引不同。 如果我不清楚,請提問,不要投反對票。
編輯:這是一個Windows窗體應用程序,我使用的Visual Studio 2012年我說的控制,而不是數組/列表; 這在 VB 中是可能的,我想知道在 C# 中是否可能。 所以我想在劇院里有 30 個座位。 我想讓每個座位都由一個名為“picSeat”的圖片框表示。 VB 會讓我給幾個完全相同的對象命名,並為控件屬性“索引”分配一個值。 這樣,我可以使用上面的循環在每個圖片框中只用 3 行代碼打印“Hello”。
不,此功能在 C# 中不存在,並且從未在從經典 VB 到 VB.Net 的過渡中實現。
我通常做的是將每個有問題的控件放在一個公共父容器中。 Form 本身可以工作,但是如果您需要將它們與其他相同類型的控件區分開來,GroupBox 或 Panel 控件也可以工作。 然后,您可以像這樣訪問控件:
foreach (var picBox in parentControl.Controls.OfType<PictureBox>())
{
// do something with each picturebox
}
如果要使用特定控件,只需按名稱寫入:
pictureBox6.SomeProperty = someValue;
如果您需要更改在運行時確定的特定控件,通常這是為了響應用戶事件:
void PictureBox_Click(object sender, EventArgs e)
{
var picBox = sender As PictureBox;
if (picBox == null) return;
//picBox is now whichever box was clicked
// (assuming you set all your pictureboxes to use this handler)
}
如果你真的想要的控件數組功能,您可以通過添加代碼來創建數組到窗體的Load事件中做到這一點:
PictureBox[] pictureBoxes = Me.Controls.OfType<PictureBox>().ToArray();
我們在這里談論的是 WinForms 嗎? 我不確定,但我不認為你可以在同名的 winform 中擁有多個控件。 但我依稀記得做過類似的事情,解決方案是將它們命名為 Button_1、Button_2 等。然后您可以遍歷所有控件並獲得自己的索引。
但請注意,如果您想為劇院中的每個座位實例化一個單獨的控件,您可能會遇到一些嚴重的性能問題:) 我也做過類似的事情,最終在畫布上繪制了整個內容並使用鼠標坐標以正確處理事件。
您可能需要查看控件的 Uid 屬性。
( http://msdn.microsoft.com/en-us/library/system.windows.uielement.uid(v=vs.110).aspx )
您可以使用以下 Uid 屬性訪問 Control
private static UIElement FindUid(this DependencyObject parent, string uid)
{
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (el == null) continue;
if (el.Uid == uid) return el;
el = el.FindUid(uid);
if (el != null) return el;
}
return null;
}
並簡單地使用
var control = FindUid("someUid");
我從這篇文章中復制了代碼
如果您創建用戶控件的索引字典,它的行為將與 VB6 中的幾乎相同,盡管您不會在 VS C# GUI 上看到它。 您必須手動解決放置問題。 仍然 - 最重要的是 - 您將能夠通過索引引用任何實例。
為清楚起見,以下示例分為 3 個部分,但當然您可以使用適當的循環自動執行流程的每個步驟。
public partial class Form1 : Form
{
...
Dictionary<int, UserControl1> NameOfUserControlInstance = new Dictionary<int, UserControl1>()
{
{ 1, new UserControl1 {}},
{ 2, new UserControl1 {}},
{ 3, new UserControl1 {}}
};
private void Form1_Load(object sender, EventArgs e)
{
NameOfUserControlInstance[1].Location = new System.Drawing.Point(0, 0);
NameOfUserControlInstance[2].Location = new System.Drawing.Point(200, 0);
NameOfUserControlInstance[3].Location = new System.Drawing.Point(400, 0);
Controls.Add(NameOfUserControlInstance[1]);
Controls.Add(NameOfUserControlInstance[2]);
Controls.Add(NameOfUserControlInstance[3]);
}
...
}
我喜歡使用標簽來應用有關控件的任何類型的元數據
for (int i = 0; i< 10; ++i)
{
Button button = new Button();
button.Tag = i;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.