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. Dynamic Form

Dynamic Form

Scheduled Pinned Locked Moved C#
csharpasp-netvisual-studiographicsdata-structures
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.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I have a button on a form that triggers a pop up (This is a Windows Form App, not an ASP.Net App) to collect additional data based on what the related combobox value was when the button was pressed. This form can have anywhere from 2 to 10 inputs (Radio Buttons, Combo Boxes) on the screen. The issue is this: I don't want to make 15 forms to accommodate all of the possibilities, so I decided (Or am still deciding) to make a dynamic form that accepts the combobox value as an argument. So parent form has combobox A and sends that value to child form on button press. First of all I would like to know what you think of the idea of having a dynamic form vs. 15 forms. Secondly, on the dynamic form idea I decided to create a form with all of the possible elements (5 groupboxes with radio buttons, 5 comboboxes, 1 multiline textbox and 1 save button) and then resize and show / hide / position the controls as necessary based on the input. I can handle this well enough, though it seems laborious, but I am having an issue with dynamically loading information into my comboboxes, see this code:

    switch (theId)
    {
    case 24:
    this.Size = new System.Drawing.Size(348, 619);
    foreach (Control ctrl in Controls)
    {
    ctrl.Visible = true;
    }
    break;
    case 25:
    string[] myControls = new string[4] { "groupBox1", "label1", "comboBox1", "btnSave" };

                    foreach (Control ctrl in Controls)
                    {
                        if (Array.IndexOf(myControls, ctrl.Name) == -1)
                        {
                            ctrl.Visible = false;
                        }
                        else
                        {
                            ctrl.Visible = true;
                            switch (Array.IndexOf(myControls, ctrl.Name))
                            {
                                case 0:
                                    ctrl.Text = "Blah";
                                    break;
                                case 1:
                                    ctrl.Text = "Foo";
                                    break;
                                case 2:
                                    if (ctrl is ComboBox)
                                    {
    

    My breakdown is here at the end, the Control ctrl won't accept the Combobox.Items.Add, so how do I

    B M 2 Replies Last reply
    0
    • E eddieangel

      I have a button on a form that triggers a pop up (This is a Windows Form App, not an ASP.Net App) to collect additional data based on what the related combobox value was when the button was pressed. This form can have anywhere from 2 to 10 inputs (Radio Buttons, Combo Boxes) on the screen. The issue is this: I don't want to make 15 forms to accommodate all of the possibilities, so I decided (Or am still deciding) to make a dynamic form that accepts the combobox value as an argument. So parent form has combobox A and sends that value to child form on button press. First of all I would like to know what you think of the idea of having a dynamic form vs. 15 forms. Secondly, on the dynamic form idea I decided to create a form with all of the possible elements (5 groupboxes with radio buttons, 5 comboboxes, 1 multiline textbox and 1 save button) and then resize and show / hide / position the controls as necessary based on the input. I can handle this well enough, though it seems laborious, but I am having an issue with dynamically loading information into my comboboxes, see this code:

      switch (theId)
      {
      case 24:
      this.Size = new System.Drawing.Size(348, 619);
      foreach (Control ctrl in Controls)
      {
      ctrl.Visible = true;
      }
      break;
      case 25:
      string[] myControls = new string[4] { "groupBox1", "label1", "comboBox1", "btnSave" };

                      foreach (Control ctrl in Controls)
                      {
                          if (Array.IndexOf(myControls, ctrl.Name) == -1)
                          {
                              ctrl.Visible = false;
                          }
                          else
                          {
                              ctrl.Visible = true;
                              switch (Array.IndexOf(myControls, ctrl.Name))
                              {
                                  case 0:
                                      ctrl.Text = "Blah";
                                      break;
                                  case 1:
                                      ctrl.Text = "Foo";
                                      break;
                                  case 2:
                                      if (ctrl is ComboBox)
                                      {
      

      My breakdown is here at the end, the Control ctrl won't accept the Combobox.Items.Add, so how do I

      B Offline
      B Offline
      BryanWilkins
      wrote on last edited by
      #2

      Well, you didn't show the line where you are calling ComboBox.Items.Add But have you tried this?

      try
      {
      (ctrl as ComboBox).Items.Add(whatevertoaddhere);
      }
      catch(Exception ex)
      {
      //fail here gracefully...
      }

      or something of the like.

      My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.

      1 Reply Last reply
      0
      • E eddieangel

        I have a button on a form that triggers a pop up (This is a Windows Form App, not an ASP.Net App) to collect additional data based on what the related combobox value was when the button was pressed. This form can have anywhere from 2 to 10 inputs (Radio Buttons, Combo Boxes) on the screen. The issue is this: I don't want to make 15 forms to accommodate all of the possibilities, so I decided (Or am still deciding) to make a dynamic form that accepts the combobox value as an argument. So parent form has combobox A and sends that value to child form on button press. First of all I would like to know what you think of the idea of having a dynamic form vs. 15 forms. Secondly, on the dynamic form idea I decided to create a form with all of the possible elements (5 groupboxes with radio buttons, 5 comboboxes, 1 multiline textbox and 1 save button) and then resize and show / hide / position the controls as necessary based on the input. I can handle this well enough, though it seems laborious, but I am having an issue with dynamically loading information into my comboboxes, see this code:

        switch (theId)
        {
        case 24:
        this.Size = new System.Drawing.Size(348, 619);
        foreach (Control ctrl in Controls)
        {
        ctrl.Visible = true;
        }
        break;
        case 25:
        string[] myControls = new string[4] { "groupBox1", "label1", "comboBox1", "btnSave" };

                        foreach (Control ctrl in Controls)
                        {
                            if (Array.IndexOf(myControls, ctrl.Name) == -1)
                            {
                                ctrl.Visible = false;
                            }
                            else
                            {
                                ctrl.Visible = true;
                                switch (Array.IndexOf(myControls, ctrl.Name))
                                {
                                    case 0:
                                        ctrl.Text = "Blah";
                                        break;
                                    case 1:
                                        ctrl.Text = "Foo";
                                        break;
                                    case 2:
                                        if (ctrl is ComboBox)
                                        {
        

        My breakdown is here at the end, the Control ctrl won't accept the Combobox.Items.Add, so how do I

        M Offline
        M Offline
        MollyTheCoder
        wrote on last edited by
        #3

        It's hard to tell where your 15 choices come from, but I recently had a similar problem. If you're choices are based on a reasonably designed class or classes, you should look at using reflection to dynamically add the controls you need:

        foreach (Type mytype in Assembly.GetEntryAssembly().GetTypes())
        {
        if (type_matches_criteria)
        {
        (create new control)
        (adjust control details and location)
        Controls.Add(new_control)
        }
        }

        Then to use your controls:

        foreach (Control ctrl in (from Control c in Controls where c (meets criteria) select c))
        {
        if (ctrl.Checked) // or whatever
        {
        ctrl.Text = "Blah";
        }
        }

        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