none
how to retrieve object data from a container that has more the one type of object in it RRS feed

  • Question

  • Hi I am working on a tool that can help keep track of an inventory of items. I am using a panel control to display checkBoxes and numericUpDown controls. Now the problem is I need to get the names of all the chechBoxes and the values of all the numericUpDown controls so I can save the data, but I don't know how to do that. A foreach loop wont work because you can only give it one type. How do I get the info from the controls? I do not have a preset amount of items you can add and remove as many items as you want. I need to be able to get the needed info from all the checkBoxes and numericUpDows so I can save the data.
    Tuesday, December 17, 2019 9:43 PM

Answers

  • Greetings LavaCreeperKing.

    You can, in fact, use a foreach. You just have to cast the objects to the type you want as you go.

    Foreach(control in Controls)
    {
       CheckBox cb = control as CheckBox;
       if(cb != null)
       {
          // Code for CheckBox goes here, using 'cb'.
       }
    
       // And similarly for NumericUpDown, or whatever.
    }

    Tuesday, December 17, 2019 10:08 PM

All replies

  • Greetings LavaCreeperKing.

    You can, in fact, use a foreach. You just have to cast the objects to the type you want as you go.

    Foreach(control in Controls)
    {
       CheckBox cb = control as CheckBox;
       if(cb != null)
       {
          // Code for CheckBox goes here, using 'cb'.
       }
    
       // And similarly for NumericUpDown, or whatever.
    }

    Tuesday, December 17, 2019 10:08 PM
  • Hello,

    The following may appear overboard yet when considering if this becomes part of a library it makes sense.

    https://raw.githubusercontent.com/karenpayneoregon/DescendantsVisalBasicWinForms/master/DescendantsLibrary1/ControlExtensions.cs

    private void button1_Click(object sender, EventArgs e)
    {
        var checkBoxes = panel1.CheckBoxList();
        var numericUpDown = panel1.NumericUpDownList();
    }

    Pretty clean code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 17, 2019 10:39 PM
    Moderator
  • Thanks. So as I understand it, all windows form controls inherit from a base class called control. This allows them to all be stored into one container. So when I wish to access one I just need to make sure I cast it to the appropriate object type. Is this correct?  
    Wednesday, December 18, 2019 5:18 PM
  • That's correct.

    Of course it helps if you know the type to start with (for example, that checkBox1 is a CheckBox), but for cases where the number and type of controls is variable and you need to search through them, you can do it by casting.

    And I just realised I made a couple of errors in the foreach line of my code snippet, but obviously you worked that out.


    • Edited by Ante Meridian Wednesday, December 18, 2019 10:05 PM Bit about the errors.
    Wednesday, December 18, 2019 10:02 PM