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 = ...;
}
谢谢。
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);
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 ...
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 ...
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. ...
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#?
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 ...
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 ...
When I m trying to change the default Image of a Control on Windows Forms in Form Designer (no matter where on which control) I get this error: Error message: An item with the same key has ...
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.