访问动态创建的面板上的动态创建的控件
Posted
技术标签:
【中文标题】访问动态创建的面板上的动态创建的控件【英文标题】:Accessing dynamically created controls that are on a dynamically created panel 【发布时间】:2021-08-31 19:13:30 【问题描述】:我正在创建这个面板
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;
【问题讨论】:
我看不出 ""Format" + p.ToString()" 的意义,因为每个面板只有其中一个。你有 this.Controls["panel" + p.ToString] 来识别面板。之后,您只需要 Controls["Format"] 即可获得相关标签 this.Controls["panel" + p.ToString()].Controls["Format"].Text = "the thing"; 【参考方案1】:您需要为每个子控件添加一个标识符或引用该控件的东西。 您可以使用名称,设置标签等。
以下是删除面板中动态创建的特定按钮的示例。
foreach (Control p in this.Controls)
If (p is Panel)
foreach (Control PanelControl in p.Controls)
If (PanelControl.Tag.Contains("RemoveThisButton"))
PanelControl.Dispose();
修改数组时要小心,因为这会破坏循环。
【讨论】:
以上是关于访问动态创建的面板上的动态创建的控件的主要内容,如果未能解决你的问题,请参考以下文章