|
-
February 28th, 2006, 01:02 PM
#1
Dynamically change control type
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
-
February 28th, 2006, 01:51 PM
#2
Re: Dynamically change control type
Something like this should work:
Code:
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
-
February 28th, 2006, 02:31 PM
#3
Re: Dynamically change control type
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:
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:
Code:
using System.Reflection;
-
March 3rd, 2006, 06:53 PM
#4
Re: Dynamically change control type
Thanks for your help guys. Will look into it asap and post back if I have any questions
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|