المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Dynamic Form



C# Programming
03-06-2010, 02:12 AM
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 add items to it? And is this whole approach just not right?