我目前有一个Windows窗体,有24个标签,需要定期更新,目前我这样做。
private void UpdateLabels()
{
// ------------------------------------------------------------------------
// Group 1 Team 1
// -------------------------------------------------------------------------
lblGroup1Team1Points.Text = Convert.ToString(theTeams[0].returnPoints());
lblGroup1Team1Points.Refresh();
lblGroup1Team1Wins.Text = Convert.ToString(theTeams[0].returnWins());
lblGroup1Team1Wins.Refresh();
lblGroup1Team1Draws.Text = Convert.ToString(theTeams[0].returnDraws());
lblGroup1Team1Draws.Refresh();
lblGroup1Team1Loses.Text = Convert.ToString(theTeams[0].returnLoses());
lblGroup1Team1Loses.Refresh();
lblGroup1Team1GoalDifference.Text = Convert.ToString(theTeams[0].returnGoalDifference());
lblGroup1Team1GoalDifference.Refresh();
lblGroup1Team1Played.Text = Convert.ToString(theTeams[0].returngamesPlayed());
lblGroup1Team1Played.Refresh();
// ------------------------------------------------------------------------
// Group 1 Team 2
// -------------------------------------------------------------------------
lblGroup1Team2Points.Text = Convert.ToString(theTeams[1].returnPoints());
lblGroup1Team2Points.Refresh();
lblGroup1Team2Wins.Text = Convert.ToString(theTeams[1].returnWins());
lblGroup1Team2Wins.Refresh();
lblGroup1Team2Draws.Text = Convert.ToString(theTeams[1].returnDraws());
lblGroup1Team2Draws.Refresh();
lblGroup1Team2Loses.Text = Convert.ToString(theTeams[1].returnLoses());
lblGroup1Team2Loses.Refresh();
lblGroup1Team2GoalDifference.Text = Convert.ToString(theTeams[1].returnGoalDifference());
lblGroup1Team2GoalDifference.Refresh();
lblGroup1Team2Played.Text = Convert.ToString(theTeams[1].returngamesPlayed());
lblGroup1Team2Played.Refresh();
// ------------------------------------------------------------------------
// Group 1 Team 3
// -------------------------------------------------------------------------
lblGroup1Team3Points.Text = Convert.ToString(theTeams[2].returnPoints());
lblGroup1Team3Points.Refresh();
lblGroup1Team3Wins.Text = Convert.ToString(theTeams[2].returnWins());
lblGroup1Team3Wins.Refresh();
lblGroup1Team3Draws.Text = Convert.ToString(theTeams[2].returnDraws());
lblGroup1Team3Draws.Refresh();
lblGroup1Team3Loses.Text = Convert.ToString(theTeams[2].returnLoses());
lblGroup1Team3Loses.Refresh();
lblGroup1Team3GoalDifference.Text = Convert.ToString(theTeams[2].returnGoalDifference());
lblGroup1Team3GoalDifference.Refresh();
lblGroup1Team3Played.Text = Convert.ToString(theTeams[2].returngamesPlayed());
lblGroup1Team3Played.Refresh();
// ------------------------------------------------------------------------
// Group 1 Team 4
// -------------------------------------------------------------------------
lblGroup1Team4Points.Text = Convert.ToString(theTeams[3].returnPoints());
lblGroup1Team4Points.Refresh();
lblGroup1Team4Wins.Text = Convert.ToString(theTeams[3].returnWins());
lblGroup1Team4Wins.Refresh();
lblGroup1Team4Draws.Text = Convert.ToString(theTeams[3].returnDraws());
lblGroup1Team4Draws.Refresh();
lblGroup1Team4Loses.Text = Convert.ToString(theTeams[3].returnLoses());
lblGroup1Team4Loses.Refresh();
lblGroup1Team4GoalDifference.Text = Convert.ToString(theTeams[3].returnGoalDifference());
lblGroup1Team4GoalDifference.Refresh();
lblGroup1Team4Played.Text = Convert.ToString(theTeams[3].returngamesPlayed());
lblGroup1Team4Played.Refresh();
}
这很好用,不会让我弄错,但是当它可以在某种循环中进行更新时它是一个相当漫长的过程 - 所以我基本上要问的是:是无论如何要将现有标签添加到标签数组中,以便我可以通过访问该标签数组中的元素来更新和刷新这些标签?我的想法就像这个一样简单
Label[,] group1Labels = new Label[5,7];
private void CreateLabelArray
{
group1Labels[0,0] = lblGroup1Team1Points;
}
但是我错了,因为group1Labels给了我错误"一个get或set accessor预期"什么是正确的方法?
答案 0 :(得分:1)
不确定这是多么漂亮或快,但你可以尝试类似的东西:
for (int i = 0; i < theTeams.Length; i++)
{
foreach(Control ctr in this.Controls)
{
if (ctr.Name == "lblGroup1Team" + (i + 1) + "Points")
{
((Label)ctr).Text = theTeams[i].returnPoints();
((Label)ctr).Refresh();
}
//other ifs for each label
}
}
答案 1 :(得分:1)
试试这个列表
foreach (Control c in group1.Controls)
{
if (c is Label)
{
string name = c.Name;
// add your code to add in list or array
}
}
答案 2 :(得分:0)
您可以使用此功能获取所有标签。 您可以在表单构造函数中调用它
var labels = GetType()
.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(x => x.FieldType == typeof(Label))
.Select(x => (Label)x.GetValue(this))
.ToArray();