Here is a dynamic way -
public partial class Form1 : Form
{
Assembly currentAssembly;
Type formType;
Form form;
public Form1()
{
InitializeComponent();
this.listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
currentAssembly = Assembly.GetExecutingAssembly();
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
formType = currentAssembly.GetType("WindowsFormsApplication1.Form" + listBox1.SelectedItem.ToString());
if (formType != null)
{
form = (Form)Activator.CreateInstance(formType);
form.Show();
}
else
{
MessageBox.Show(String.Format("Form {0} doesn't exists",listBox1.SelectedItem.ToString()));
}
}
}
Hope this helps.
Update - Added an if condition to check if form exists or not.
Just to add on, if your listbox had items like -
Form1<br />
MainWindow<br />
HelpWinow<br />
instead of numbers, then using the above method would be much simpler.
just use the below way to define the formType -
formType = currentAssembly.GetType("WindowsFormsApplication1." + listBox1.SelectedItem.ToString());
And the rest will work absolutely perfect.