Click to See Complete Forum and Search --> : Dynamically change control type


draghuna
February 28th, 2006, 12:02 PM
I have a dropdownlist (ddl) with a list of Controls I would like to Display, say suppose my ddl has a textBox, Combobox and a Listbox and when I make a selection from the ddl it should draw the Control for me in the page. How do I do this? Everytime I pick something up from the ddl it should remove the existing control and add the selected control from the ddl. Any help is certainly appreciated.

Thanks,

DD

wildfrog
February 28th, 2006, 12:51 PM
Something like this should work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DynamicControl
{
public partial class Form1 : Form
{
protected System.Windows.Forms.ListBox listBox1;
protected System.Windows.Forms.Control control;

public Form1()
{
InitializeComponent();

// create listbox
listBox1 = new System.Windows.Forms.ListBox();

// add items to listbox
listBox1.Items.AddRange(
new object[] {"TextBox", "ComboBox","ListBox"});

listBox1.Location = new System.Drawing.Point(25, 30);
listBox1.Size = new System.Drawing.Size(120, 56);
listBox1.TabIndex = 0;

// add listbox to form
Controls.Add(this.listBox1);

// add listbox event handler
listBox1.SelectedIndexChanged += new System.EventHandler(this.OnSelectionIndexChanged);
}

// handle selection chenages
private void OnSelectionIndexChanged(object sender, EventArgs e)
{
// remove current control (if it exists)
if (control != null)
Controls.Remove(control);

// get new selection and create new control
switch (listBox1.SelectedIndex)
{
case 0: // create textbox
control = new TextBox();
((TextBox)control).Text = "Hello";
break;
case 1: // create combobox
control = new ComboBox();
((ComboBox)control).Items.AddRange(new object[] { "Item1", "Item2" });
break; // create listbox
case 2:
control = new ListBox();
((ListBox)control).Items.AddRange(new object[] { "Item1", "Item2" });
break;
}

// add new control
control.SetBounds(100, 100, 200, 200);
Controls.Add(control);
}
}
}

- petter

hspc
February 28th, 2006, 01:31 PM
using relection you can avoid using a switch case
and load any type you add to the listbox using its name:
add some items to the listbox like this:
TextBox
ComboBox
ListBox
Label
ProgressBar
TreeView
RadioButton

and use this code:
public partial class Form1 : Form
{
Control ctl=null;
public Form1()
{
InitializeComponent();

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ctl!=null)
{
ctl.Visible = false;
this.Controls.Remove(ctl);
}

string assemblyPath = Assembly.GetAssembly(typeof(Control)).CodeBase;

ctl = (Control)Activator.CreateInstanceFrom(assemblyPath, "System.Windows.Forms." + listBox1.SelectedItem.ToString()).Unwrap();

ctl.SetBounds(100, 100, 200, 200);
ctl.Text = "My text";
this.Controls.Add(ctl);

}
}
don't forget to add:
using System.Reflection;

draghuna
March 3rd, 2006, 05:53 PM
Thanks for your help guys. Will look into it asap and post back if I have any questions