简体   繁体   中英

Accessing dynamically created controls that are on a dynamically created panel

I am creating this 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);

How would I access the controls on the panel once it is created. There are more than one of these panels and they can be removed and redrawn at any point so that is why it needs to be dynamically created.

I tried something like this but I think I am far off. p being the relevant panel number.

this.Controls["panel" + p.ToString()].Controls["Format" + p.ToString()].Text = "the thing";

I also need to be able to get to things like the combobox's selected index such as

int index = this.Controls["panel" + p.ToString()].Controls["PrinterComboBox"].SelectedIndex;

You will need to add an identifyer to each of the child controls or something to reference the control. You can use the name, set a tag, etc.

Below is an example of removing a specific button dynmically created within a panel.

foreach (Control p in this.Controls)
If (p is Panel)
{ 
    foreach (Control PanelControl in p.Controls)
    If (PanelControl.Tag.Contains("RemoveThisButton"))
    { 
        PanelControl.Dispose();
    }
}

Be careful when modifying an Array as this will break the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM