访问动态创建的面板上的动态创建的控件
Accessing dynamically created controls that are on a dynamically created panel
我正在创建这个面板
Label PrinterName = new Label();
Label Format = new Label();
Label Count = new Label();
Label FormatLabel = new Label();
Label PrintedLabel = new Label();
Label ResourceLabel = new Label();
Button Detailsbutton = new Button();
Button PrintButton = new Button();
PictureBox LabelImageBox = new PictureBox();
Panel panel1 = new Panel();
ComboBox PrinterComboBox = new ComboBox();
PrinterName.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrinterName.Location = new System.Drawing.Point(11, 9);
PrinterName.Name = "PrinterName";
PrinterName.Size = new System.Drawing.Size(169, 28);
PrinterName.TabIndex = 5;
PrinterName.Text = printers[pc * 3];
//
// Format
//
Format.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Format.Location = new System.Drawing.Point(195, 51);
Format.Name = "Format" +pc.ToString();
Format.Size = new System.Drawing.Size(169, 28);
Format.TabIndex = 7;
Format.Text = "Label Format";
Count.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Count.Location = new System.Drawing.Point(370, 51);
Count.Name = "Count";
Count.Size = new System.Drawing.Size(109, 28);
Count.TabIndex = 8;
Count.Text = "Count";
//
// Detailsbutton
//
Detailsbutton.Location = new System.Drawing.Point(485, 60);
Detailsbutton.Name = "Detailsbutton";
Detailsbutton.Size = new System.Drawing.Size(91, 45);
Detailsbutton.TabIndex = 11;
Detailsbutton.Text = "Datails";
Detailsbutton.UseVisualStyleBackColor = true;
//
// PrinterComboBox
//
PrinterComboBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrinterComboBox.FormattingEnabled = true;
PrinterComboBox.Location = new System.Drawing.Point(16, 66);
PrinterComboBox.Name = "PrinterComboBox";
PrinterComboBox.Size = new System.Drawing.Size(157, 33);
PrinterComboBox.TabIndex = 10;
PrinterComboBox.Items.AddRange(resourcenumbers());
PrinterComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
PrinterComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
PrinterComboBox.SelectedIndexChanged += (s, e) => { selectedindexchanged(pc); };
//
// PrintButton
//
PrintButton.Location = new System.Drawing.Point(485, 9);
PrintButton.Name = "PrintButton";
PrintButton.Size = new System.Drawing.Size(91, 45);
PrintButton.TabIndex = 9;
PrintButton.Text = "Print";
PrintButton.UseVisualStyleBackColor = true;
//
// FormatLabel
//
FormatLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
FormatLabel.Location = new System.Drawing.Point(195, 23);
FormatLabel.Name = "FormatLabel";
FormatLabel.Size = new System.Drawing.Size(169, 28);
FormatLabel.TabIndex = 12;
FormatLabel.Text = "Format:";
//
// PrintedLabel
//
PrintedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrintedLabel.Location = new System.Drawing.Point(370, 23);
PrintedLabel.Name = "PrintedLabel";
PrintedLabel.Size = new System.Drawing.Size(109, 28);
PrintedLabel.TabIndex = 13;
PrintedLabel.Text = "Printed:";
//
// ResourceLabel
//
ResourceLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
ResourceLabel.Location = new System.Drawing.Point(20, 37);
ResourceLabel.Name = "ResourceLabel";
ResourceLabel.Size = new System.Drawing.Size(169, 28);
ResourceLabel.TabIndex = 14;
ResourceLabel.Text = "Resoruce:";
//
// LabelImageBox
//
LabelImageBox.Location = new System.Drawing.Point(589, 8);
LabelImageBox.Name = "LabelImageBox" + pc.ToString();
LabelImageBox.Size = new System.Drawing.Size(206, 107);
LabelImageBox.TabIndex = 15;
LabelImageBox.TabStop = false;
//
// panel1
//
panel1.BackColor = System.Drawing.SystemColors.ActiveBorder;
panel1.Controls.Add(LabelImageBox);
panel1.Controls.Add(ResourceLabel);
panel1.Controls.Add(PrintedLabel);
panel1.Controls.Add(FormatLabel);
panel1.Controls.Add(Detailsbutton);
panel1.Controls.Add(PrinterComboBox);
panel1.Controls.Add(PrintButton);
panel1.Controls.Add(Format);
panel1.Controls.Add(Count);
panel1.Controls.Add(PrinterName);
panel1.Location = new System.Drawing.Point(106, 91 + 150*pc);
panel1.Name = "panel" + pc.ToString();
panel1.Size = new System.Drawing.Size(815, 126);
panel1.TabIndex = 9;
this.Controls.Add(panel1);
创建面板后如何访问面板上的控件。这些面板不止一个,它们可以随时移除和重新绘制,这就是为什么需要动态创建它的原因。
我尝试过类似的方法,但我认为我还差得远。 p 是相关的面板编号。
this.Controls["panel" + p.ToString()].Controls["Format" + p.ToString()].Text = "the thing";
我还需要能够访问诸如组合框的选定索引之类的内容,例如
int index = this.Controls["panel" + p.ToString()].Controls["PrinterComboBox"].SelectedIndex;
您需要为每个子控件添加一个标识符或其他内容以引用该控件。
您可以使用名称、设置标签等
下面是删除面板中动态创建的特定按钮的示例。
foreach (Control p in this.Controls)
If (p is Panel)
{
foreach (Control PanelControl in p.Controls)
If (PanelControl.Tag.Contains("RemoveThisButton"))
{
PanelControl.Dispose();
}
}
修改数组时要小心,因为这会破坏循环。
我正在创建这个面板
Label PrinterName = new Label();
Label Format = new Label();
Label Count = new Label();
Label FormatLabel = new Label();
Label PrintedLabel = new Label();
Label ResourceLabel = new Label();
Button Detailsbutton = new Button();
Button PrintButton = new Button();
PictureBox LabelImageBox = new PictureBox();
Panel panel1 = new Panel();
ComboBox PrinterComboBox = new ComboBox();
PrinterName.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrinterName.Location = new System.Drawing.Point(11, 9);
PrinterName.Name = "PrinterName";
PrinterName.Size = new System.Drawing.Size(169, 28);
PrinterName.TabIndex = 5;
PrinterName.Text = printers[pc * 3];
//
// Format
//
Format.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Format.Location = new System.Drawing.Point(195, 51);
Format.Name = "Format" +pc.ToString();
Format.Size = new System.Drawing.Size(169, 28);
Format.TabIndex = 7;
Format.Text = "Label Format";
Count.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Count.Location = new System.Drawing.Point(370, 51);
Count.Name = "Count";
Count.Size = new System.Drawing.Size(109, 28);
Count.TabIndex = 8;
Count.Text = "Count";
//
// Detailsbutton
//
Detailsbutton.Location = new System.Drawing.Point(485, 60);
Detailsbutton.Name = "Detailsbutton";
Detailsbutton.Size = new System.Drawing.Size(91, 45);
Detailsbutton.TabIndex = 11;
Detailsbutton.Text = "Datails";
Detailsbutton.UseVisualStyleBackColor = true;
//
// PrinterComboBox
//
PrinterComboBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrinterComboBox.FormattingEnabled = true;
PrinterComboBox.Location = new System.Drawing.Point(16, 66);
PrinterComboBox.Name = "PrinterComboBox";
PrinterComboBox.Size = new System.Drawing.Size(157, 33);
PrinterComboBox.TabIndex = 10;
PrinterComboBox.Items.AddRange(resourcenumbers());
PrinterComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
PrinterComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
PrinterComboBox.SelectedIndexChanged += (s, e) => { selectedindexchanged(pc); };
//
// PrintButton
//
PrintButton.Location = new System.Drawing.Point(485, 9);
PrintButton.Name = "PrintButton";
PrintButton.Size = new System.Drawing.Size(91, 45);
PrintButton.TabIndex = 9;
PrintButton.Text = "Print";
PrintButton.UseVisualStyleBackColor = true;
//
// FormatLabel
//
FormatLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
FormatLabel.Location = new System.Drawing.Point(195, 23);
FormatLabel.Name = "FormatLabel";
FormatLabel.Size = new System.Drawing.Size(169, 28);
FormatLabel.TabIndex = 12;
FormatLabel.Text = "Format:";
//
// PrintedLabel
//
PrintedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PrintedLabel.Location = new System.Drawing.Point(370, 23);
PrintedLabel.Name = "PrintedLabel";
PrintedLabel.Size = new System.Drawing.Size(109, 28);
PrintedLabel.TabIndex = 13;
PrintedLabel.Text = "Printed:";
//
// ResourceLabel
//
ResourceLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
ResourceLabel.Location = new System.Drawing.Point(20, 37);
ResourceLabel.Name = "ResourceLabel";
ResourceLabel.Size = new System.Drawing.Size(169, 28);
ResourceLabel.TabIndex = 14;
ResourceLabel.Text = "Resoruce:";
//
// LabelImageBox
//
LabelImageBox.Location = new System.Drawing.Point(589, 8);
LabelImageBox.Name = "LabelImageBox" + pc.ToString();
LabelImageBox.Size = new System.Drawing.Size(206, 107);
LabelImageBox.TabIndex = 15;
LabelImageBox.TabStop = false;
//
// panel1
//
panel1.BackColor = System.Drawing.SystemColors.ActiveBorder;
panel1.Controls.Add(LabelImageBox);
panel1.Controls.Add(ResourceLabel);
panel1.Controls.Add(PrintedLabel);
panel1.Controls.Add(FormatLabel);
panel1.Controls.Add(Detailsbutton);
panel1.Controls.Add(PrinterComboBox);
panel1.Controls.Add(PrintButton);
panel1.Controls.Add(Format);
panel1.Controls.Add(Count);
panel1.Controls.Add(PrinterName);
panel1.Location = new System.Drawing.Point(106, 91 + 150*pc);
panel1.Name = "panel" + pc.ToString();
panel1.Size = new System.Drawing.Size(815, 126);
panel1.TabIndex = 9;
this.Controls.Add(panel1);
创建面板后如何访问面板上的控件。这些面板不止一个,它们可以随时移除和重新绘制,这就是为什么需要动态创建它的原因。
我尝试过类似的方法,但我认为我还差得远。 p 是相关的面板编号。
this.Controls["panel" + p.ToString()].Controls["Format" + p.ToString()].Text = "the thing";
我还需要能够访问诸如组合框的选定索引之类的内容,例如
int index = this.Controls["panel" + p.ToString()].Controls["PrinterComboBox"].SelectedIndex;
您需要为每个子控件添加一个标识符或其他内容以引用该控件。 您可以使用名称、设置标签等
下面是删除面板中动态创建的特定按钮的示例。
foreach (Control p in this.Controls)
If (p is Panel)
{
foreach (Control PanelControl in p.Controls)
If (PanelControl.Tag.Contains("RemoveThisButton"))
{
PanelControl.Dispose();
}
}
修改数组时要小心,因为这会破坏循环。