Dynamic Form
-
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
-
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
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.
-
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
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";
}
}