Results 1 to 8 of 8

Thread: [RESOLVED] Get child controls in a control

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Get child controls in a control

    Hi I'm trying to iterate through controls from main form. I'm getting 2 controls while I'm expecting 4. Here's my code so far

    C# Code:
    1. public Object GetOption()
    2. {
    3.     Object o = new Object();
    4.  
    5.     Control con = new Control();
    6.  
    7.     Size size = new Size
    8.     {
    9.         Width = 90,
    10.         Height = 12
    11.     };
    12.  
    13.  
    14.     CheckBox checkBox = new CheckBox
    15.     {
    16.         Name = "SomeCheckBox",
    17.         Checked = true,
    18.         Location = new Point(p.X, p.Y),
    19.         Size = size
    20.     };
    21.     ComboBox comboBox = new ComboBox
    22.     {
    23.         Name = "SomeComboBox",
    24.         Text = "Some text in CB",
    25.         Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
    26.         Size = size
    27.     };
    28.     TextBox count = new TextBox
    29.     {
    30.         Name = "CountTextBox",
    31.         Text = "20",
    32.         Size = size,
    33.         Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
    34.     };
    35.     TextBox date = new TextBox
    36.     {
    37.         Name = "DateNow",
    38.         Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
    39.         Size = size,
    40.         Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
    41.     };
    42.     con.Controls.AddRange(new Control[] { checkBox, comboBox, count, date });
    43.  
    44.     // Next position
    45.     p.X = 25;
    46.     p.Y = p.Y + 14;
    47.  
    48.     o = con;
    49.     return o;
    50. }

    EDIT: I forgot to give the loop

    C# Code:
    1. private void Button1_Click(object sender, EventArgs e)
    2. {
    3.     Control control = new Control();
    4.     control = (Control)GetOption();
    5.  
    6.     foreach (Control c in control.Controls)
    7.     {
    8.         MessageBox.Show("Type of control: " + c.GetType().ToString());
    9.         Options_Pnl.Controls.Add(c);
    10.     }
    11. }
    Last edited by nikel; Jul 3rd, 2018 at 01:25 AM. Reason: I changed highlight value
    I'm not a man of too many faces
    The mask I wear is one

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width