Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. accessing controls through a collection

accessing controls through a collection

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rbledwards
    wrote on last edited by
    #1

    Hi, I am collaborating/taking over a project from another developer in my office. One part is a Windows form with a CheckBoxList controls on top and rows of ComboBoxes on bottom like this: cbo1_1 cbo1_2 cbo1_3 cbo1_4 cbo2_1 cbo2_2 cbo2_3 cbo2_4 etc... If there is anything selected in the CheckListBox then row 1 is enabled. More rows could be enabled depending on what exactly is checked. The problem is that so much code is duplicated. For instance, each cbo is bound to a column in a dataset. For each cbo this code exists: ------------------- private void Populatecbo1(System.Windows.Forms.ComboBox s) { s.Items.Clear(); int Count = _aColumn.Length; for(int i =0; i < Count; i++) { s.Items.Add(_aColumn[i].ToString()); } }--------------------- and one for cbo2 and so on. Theres also literally thousands of lines of switch statements for enabling/disabling cbo's. Isn't there a better way to do this without duplicating all of this code? Thanks, Robert

    F L 2 Replies Last reply
    0
    • R Rbledwards

      Hi, I am collaborating/taking over a project from another developer in my office. One part is a Windows form with a CheckBoxList controls on top and rows of ComboBoxes on bottom like this: cbo1_1 cbo1_2 cbo1_3 cbo1_4 cbo2_1 cbo2_2 cbo2_3 cbo2_4 etc... If there is anything selected in the CheckListBox then row 1 is enabled. More rows could be enabled depending on what exactly is checked. The problem is that so much code is duplicated. For instance, each cbo is bound to a column in a dataset. For each cbo this code exists: ------------------- private void Populatecbo1(System.Windows.Forms.ComboBox s) { s.Items.Clear(); int Count = _aColumn.Length; for(int i =0; i < Count; i++) { s.Items.Add(_aColumn[i].ToString()); } }--------------------- and one for cbo2 and so on. Theres also literally thousands of lines of switch statements for enabling/disabling cbo's. Isn't there a better way to do this without duplicating all of this code? Thanks, Robert

      F Offline
      F Offline
      Furty
      wrote on last edited by
      #2

      RblEdwards wrote: Isn't there a better way to do this without duplicating all of this code? Without seeing the full code it's not going to be possible to supply a working example, but the answer to your question given the information here is a resounding YES. I would suggest creating an array of ComboBox controls, and iterating over them to populate them and set the Enabled property, there's a good chance you will be able to reduce it down to 3 or so methods. e.g. int cols = 10; int rows = 10; System.Windows.Forms.ComboBox[,] comboArray; comboArray = new System.Windows.Forms.ComboBox[cols, rows]; Another possible option would be using a DataGrid to display the data and combo boxes (there's plenty of examples online for putting ComboBoxes in DataGrid cells), and simply manipulating the DataGrid properties to enable/disable item editing.

      1 Reply Last reply
      0
      • R Rbledwards

        Hi, I am collaborating/taking over a project from another developer in my office. One part is a Windows form with a CheckBoxList controls on top and rows of ComboBoxes on bottom like this: cbo1_1 cbo1_2 cbo1_3 cbo1_4 cbo2_1 cbo2_2 cbo2_3 cbo2_4 etc... If there is anything selected in the CheckListBox then row 1 is enabled. More rows could be enabled depending on what exactly is checked. The problem is that so much code is duplicated. For instance, each cbo is bound to a column in a dataset. For each cbo this code exists: ------------------- private void Populatecbo1(System.Windows.Forms.ComboBox s) { s.Items.Clear(); int Count = _aColumn.Length; for(int i =0; i < Count; i++) { s.Items.Add(_aColumn[i].ToString()); } }--------------------- and one for cbo2 and so on. Theres also literally thousands of lines of switch statements for enabling/disabling cbo's. Isn't there a better way to do this without duplicating all of this code? Thanks, Robert

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        In addition to Furty's response... If you have a collection of controls you can either populate them at design-time (which leads to code like you've got) or you can programmatically add them yourself. Try this: Create a new project and put a GroupBox on the form Add a single control to that GroupBox Look at the Windows Generated code. Now you have a template for what YOU would do, since the logic is adding a single control to the Control collection. Rip out the Windows generated code and make it a programmatic loop to add each control. Now --- if you have an event like having an item selected, point it to a single routine in your build code. If it is necessary to know which control from top to bottom was selected, you can interate through the Control collection until you find that control, and that becomes the index for populating the corresponding control in the other groupbox. The beauty of reusability! _____________________________________________ The world is a dangerous place.
        Not because of those that do evil,
            but because of those who look on and do nothing.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups