我目前在大面板上添加了动态数量的较小面板。我有一个单独的表单,它返回一个workspaceID列表。
每个WorkSpaceControl都有一个全局变量get,set,用于存储workspaceID,我看不到在当前范围内访问这些内容的方法。
WorkSpaceControl wsp = wch.GenerateWorkspaceControl(items, space.Id, space.Name, point);
workSpaceList.Add(wsp);
k++;
col++;
foreach (WorkSpaceControl p in workSpaceList)
{
/// allow something to be dragged onto the space and catches something
p.AllowDrop = true;
p.DragEnter += formDragEnter;
p.DragDrop += formDragDrop;
// Event handler for draggin picturebox
p.ItemIcon.MouseDown += new MouseEventHandler(moveIcon_MouseDown);
/// event handler for updating icons and quantity based on selected item
p.Child_ItemIndexChanged += c_SelectedIndexChanged;
PanelTest.Controls.Add(p);
}
private void btnFindByCustom_Click(object sender, EventArgs e)
{
ih_FindByCustom findCust = new ih_FindByCustom();
var result = findCust.ShowDialog();
if (result == DialogResult.OK)
{
List<int> workspaceidstohighlight = findCust.workspaceIdsToHighlight;
//highlihgt the panels in the workspaceidtohighlight
Panel p = (Panel)((Button)sender).Parent.Parent;
}
}
我需要访问面板p上的所有workspaceControl。
我解决了它,但它很丑陋
private void btnFindByCustom_Click(object sender, EventArgs e)
{
ih_FindByCustom findCust = new ih_FindByCustom();
var result = findCust.ShowDialog();
if(result == DialogResult.OK)
{
List<int> workspaceidstohighlight = findCust.workspaceIdsToHighlight;
//highlihgt the panels in the workspaceidtohighlight
if (workspaceidstohighlight.Count != 0)
{
foreach (WorkSpaceControl list in workSpaceList)
{
foreach (int idtohighlight in workspaceidstohighlight)
{
if (list.WorkspaceID == idtohighlight)
{
list.backPanel.BackColor = Color.Yellow;
}
}
}
}
}
}
我希望不要将workSpaceList变为全局变量,但我看不到任何其他方式。
有没有人采用“更清洁”的方式做到这一点?我在某处看到使用linq并创建连接将是一个可能的解决方案,我将要研究。