Activating form with string (Changeable values)

38 views Asked by At

I send name of opened form to datagridview in formload. Then when I double click row of this datagridview, I want to activate the form again. But I cannot use this method. What do you prefer?

`  private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        int i = dataGridView1.CurrentRow.Index;
        string fname = dataGridView1.Rows[i].Cells[1].Value.ToString();
        var type = Type.GetType("ProMod." + fname);
        var openthisform = Activator.CreateInstance(type) as Form;
        Application.OpenForms.OfType<Form>().First(f => f is openthisform).Activate();
    }`
1

There are 1 answers

0
Steve On BEST ANSWER

If you have already opened the form and know its name then you just need to retrieve it from the Application.OpenForms using the name string

var form = Application.OpenForms.OfType<Form>().FirstOrDefault(x => x.Name == fname);
if(form != null) form.Activate();