English 中文(简体)
如何控制?
原标题:How to point to control?
  • 时间:2011-10-22 11:18:11
  •  标签:
  • c#
  • winforms

i ve 6 label controls in a form: label1, label2...label6. How to refer to the control in a loop like this:

for (i=1;i<=6;i++) {
   label[i].text = ...;
}

谢谢。

最佳回答

让我们假设这种控制是WinForms,而你的“实验室”是控制——<>Form有Controls 财产,即与该集装箱有关的控制,因此,我们应当能够利用Linq来质疑这一点,掌握我们想要的那种控制,然后将其er为:

using System.Linq;

var labels = from control in Controls where control is Label select control;

for (i = 1; i <= controls.Count; i++)
{
   labels[i].text = i.ToString();
}

A little rough, but you aren t very specific - it should be a decent starting point if nothing else.

EDIT:

韩文,我认为我需要时间来研究,并读到<条码>。 表格:Controls 在林克(以这种直截了当的方式,至少是这样),作为一种替代办法,这应当有助于:

private List<Label> GetLabels()
{
    var result = new List<Label>();
    foreach (var control in Controls)
    {
        if (control is Label)
        {
            result.Add(control as Label);
        }
    }
    return result;
}

甚至可以笼统地考虑上述方法,但可以:

var labels = GetLabels();
for (int i = 0; i <= labels.Count; i++)
{
    labels[i].Text = i.ToString();
}
问题回答

Try,

Label []labels={Label1,Label2,Label3};

这里还有另一种方式:

for (int n = 1; n < 4; n++)
{
    Control[] Temp = Controls.Find("Label" + n, false);
    Temp[0].Text = n.ToString();
}

你们可以执行这样的行动:

    int y = 0;
    int index = 0;

    Label[] labels = new Label[6];

    foreach (Student std in StudentList)
    {
            labels[index] = new Label();

            labels[index].Text = std.Name;

            labels[index].ForeColor = Color.Red;

            labels[index].Location = new Point(0, y);

            labels[index].Size = new Size(50, 12);

            y = y + 10;
            ++index;
    }

    // Add the Label control to the form.
    mPanel.Controls.AddRange(labels);




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.