PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Get child controls in a control-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Get child controls in a control

  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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: Get child controls in a control

    You're setting the Location of three of the controls to the same value, so you'll only see one of them. You should probably be using a TableLayoutPanel or FlowLayoutPanel to contain those controls so there'd be no need to set the Location.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Get child controls in a control

    Hi, I edited my post. The problem isn't location, I get 2 dialogs in my loop instead of 4.
    I'm not a man of too many faces
    The mask I wear is one

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: Get child controls in a control

    Firstly, your code is ridiculously convoluted. Why would you make it so crazy?

    Secondly, I think I see the reason for the issue but I'm actually surprised that it doesn't throw an exception. To test my theory, comment out this line:
    csharp Code:
    1. Options_Pnl.Controls.Add(c);
    and run the code again and I think you'll see four messages.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: Get child controls in a control

    A control can only have one parent at a time so the line I suggested commenting out is actually removing that control from the Controls collection it's already in. What I think is happening is that, after two iterations of your loop, you have removed two controls from the original Controls collection and that leaves two. There's two controls and you've done two iterations so the loop thinks it's finished. Normally, modifying a collection while enumerating it causes an exception to be thrown but maybe it's not in this case because of the indirect way you're making that modification.

    Like I said, your code is rather crazy anyway. If the aim is to create four controls in a method and add them to Options_Pnl then you can simplify that a lot:
    csharp Code:
    1. public Control[] GetOption()
    2. {
    3.     Size size = new Size
    4.     {
    5.         Width = 90,
    6.         Height = 12
    7.     };
    8.  
    9.     var controls = new Control[]
    10.     {
    11.         new CheckBox
    12.         {
    13.             Name = "SomeCheckBox",
    14.             Checked = true,
    15.             Location = new Point(p.X, p.Y),
    16.             Size = size
    17.         };
    18.         new ComboBox
    19.         {
    20.             Name = "SomeComboBox",
    21.             Text = "Some text in CB",
    22.             Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
    23.             Size = size
    24.         },
    25.         new TextBox
    26.         {
    27.             Name = "CountTextBox",
    28.             Text = "20",
    29.             Size = size,
    30.             Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
    31.         },
    32.         new TextBox
    33.         {
    34.             Name = "DateNow",
    35.             Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
    36.             Size = size,
    37.             Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
    38.         }
    39.     };
    40.  
    41.     // Next position
    42.     p.X = 25;
    43.     p.Y = p.Y + 14;
    44.  
    45.     return controls;
    46. }
    csharp Code:
    1. Options_Pnl.Controls.AddRange(GetOption());
    That said, you really ought to be designing a user control that contains those four children and simply adding instances of that to a TableLayoutPanel or FlowLayoutPanel.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Get child controls in a control

    Hi thanks for helping. I did it like this

    C# Code:
    1. public Object GetOption()
    2. {
    3.     Object o = new Object();
    4.  
    5.     /*
    6.     TextBox1 = new TextBox
    7.     {
    8.         Location = p,
    9.         Size = new Size(110, 40),
    10.         Text = "Static"
    11.     };
    12.     */
    13.  
    14.     Size size = new Size
    15.     {
    16.         Width = 30,
    17.         Height = 12
    18.     };
    19.  
    20.  
    21.     CheckBox checkBox = new CheckBox
    22.     {
    23.         Name = "SomeCheckBox",
    24.         Checked = true,
    25.         Location = new Point(p.X, p.Y),
    26.         Size = size
    27.     };
    28.     ComboBox comboBox = new ComboBox
    29.     {
    30.         Name = "SomeComboBox",
    31.         Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
    32.         Size = size
    33.     };
    34.     TextBox count = new TextBox
    35.     {
    36.         Name = "CountTextBox",
    37.         Text = "20",
    38.         Size = size,
    39.         Location = new Point(Convert.ToInt32(p.X + 45), p.Y)
    40.     };
    41.     TextBox date = new TextBox
    42.     {
    43.         Name = "DateNow",
    44.         Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
    45.         Size = size,
    46.         Location = new Point(Convert.ToInt32(p.X + 75), p.Y)
    47.     };
    48.  
    49.     comboBox.Items.Add("test 1");
    50.     comboBox.Items.Add("test 2");
    51.  
    52.    
    53.     o = new Control[] { checkBox, comboBox, count, date };
    54.  
    55.     // Next position
    56.     p.X = 25;
    57.     p.Y = p.Y + 24;
    58.  
    59.     return o;
    60. }
    61.  
    62. private void AddOption_Btn_Click(object sender, EventArgs e)
    63. {
    64.     Control[] controls = (Control[])GetOption();
    65.  
    66.     for (int i = 0; i <= controls.Count() - 1; i++)
    67.     {
    68.         controls[i].Parent = Options_Pnl;
    69.         Options_Pnl.Controls.Add(controls[i]);
    70.     }
    71.  
    72. }

    I don't want to simplify it there's to much things to do I just hope I'm not doing it wrong.
    I'm not a man of too many faces
    The mask I wear is one

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: [RESOLVED] Get child controls in a control

    That will produce the desired result but you are doing some rather silly things.

    1.
    csharp Code:
    1. public Object GetOption()
    Why on Earth would you declare the return type of a method Object when you know for a fact that it will be returning a Control array and you are then going to cast that return value as a Control array when you call the method? Why would you not declare it as the type it is and then use it as that type without a cast?

    2.
    csharp Code:
    1. Object o = new Object();
    What is the point of that Object array? You create it, assign it to that variable and never use it, then finally assign a different object to the same variable here:
    csharp Code:
    1. o = new Control[] { checkBox, comboBox, count, date };
    Why would you create an object of any type that you never use?

    3.
    csharp Code:
    1. for (int i = 0; i <= controls.Count() - 1; i++)
    Why use a 'for' loop and only ever use the loop counter as an index into a list when you could use a 'foreach' loop over that list?

    4.
    csharp Code:
    1. controls[i].Parent = Options_Pnl;
    2. Options_Pnl.Controls.Add(controls[i]);
    Those two lines do effectively the exact same thing. Why use them both?

    5. Why use a loop and either of the lines you've got in it when you could just make one call to AddRange, as I've already demonstrated.

    6.
    I don't want to simplify it
    Why would you ever not want to simplify code? It's part of the process of learning if nothing else, and it makes maintenance of that code much easier.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: [RESOLVED] Get child controls in a control

    I've already fixed them except for 3 and 4
    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
  •  



Featured


Click Here to Expand Forum to Full Width