0

I am writing a bit of code that uses the AfterSelect behaviour from TreeView to then open a form using the selected Nodes name and tag. It works perfectly, however the flaw is that whenever the form is closed using either this.Close(); or this.Hide(); or even using the built in Windows Close Button I cannot reopen the same form without opening a different one first.

I have tried using different Behaviour methods, such as NodeMouseClick and NodeMouseDoubleClick, this still results in Object Errors so I presumed this was not the method I needed

Here is my code within the treeview:

public void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    string formName = treeView1.SelectedNode.Name.ToString();
    string namespaceName = treeView1.SelectedNode.Tag.ToString();
    var type = Type.GetType(namespaceName + "." + formName);
    var newForm = Activator.CreateInstance(type) as Form;
    if (newForm != null)
    {
        newForm.ShowDialog();
    }
}

And here is my code within the form:

private void BtnSave_Click(object sender, EventArgs e)
{
    AccessDBConnection.sql = "INSERT INTO Users (FirstName, Surname, Username) values(@FirstName, @Surname, @Username)";
    AccessDBConnection.cmd.Parameters.Clear();
    AccessDBConnection.cmd.CommandType = CommandType.Text;
    AccessDBConnection.cmd.CommandText = AccessDBConnection.sql;
    AccessDBConnection.cmd.Parameters.AddWithValue("@FirstName", firstNameBox.Text);
    AccessDBConnection.cmd.Parameters.AddWithValue("@Surname", surnameBox.Text);
    AccessDBConnection.cmd.Parameters.AddWithValue("@Username", usernameBox.Text);
    AccessDBConnection.openConnection();

    AccessDBConnection.cmd.ExecuteNonQuery();
    AccessDBConnection.closedConnection();
    MyMessageBox.ShowMessage("User Created.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    //this.Hide();
    //this.Close();
    //Commented out as using Windows Close Button for testing.
}

I should be able to open form Add User and then finish the details, press submit/close and then if need-be access the same form again, however I cannot re-open the same form until I have opened another first such as User Maintenance.

There are no Error Messages.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Snap
  • 1
  • 1
  • Since the form is opened as a dialog box, closing the dialog will not automatically dispose of the form. You probably want to `newForm.Dispose()` after the `newForm.ShowDialog()` – Lennart Stoop Jun 13 '19 at 11:16
  • Unfortunately this didn't help, it keeps the form on screen after I click submit but I still have the same issue. After closing the form completely I cannot start it again without starting another one. – Snap Jun 13 '19 at 11:40
  • 1
    It is just a basic logic flaw, you can't get the AfterSelect event again because the node is still selected. You could, say, add a dummy node at the top that you re-select when the form closes. – Hans Passant Jun 13 '19 at 13:36
  • @HansPassant idea will work. I could get it to select its parent node as those are folders for the nodes that will open forms. – Snap Jun 14 '19 at 13:48

1 Answers1

0

Do you really need this part of the Code:

var type = Type.GetType(namespaceName + "." + formName);
var newForm = Activator.CreateInstance(type) as Form;

If not, you need to think about, if you declare the var in every afterselect as a new Form, you can't get the Form back in his old state when you hide it, because you create a new instance.

If you always want a new Instance of this window do:

public void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    string formName = treeView1.SelectedNode.Name.ToString();
    string namespaceName = treeView1.SelectedNode.Tag.ToString();
    var newForm = new FORMNAME();
    newForm.ShowDialog();
}

and in the other form:

Close(); // this. is redundant

If you just want to hide it and don't declare it always as a new Instance: Declare

var newForm = new FORMNAME();

elsewhere and use in the other form:

Hide();
LoaStaub
  • 63
  • 14
  • I use that part of the code because the form name is defined by what node is clicked on, and where the forms are in different folders in the solution explorer the easiest way to get their name-space is by putting it in the nodes tag and reading it. I am new to coding in C# and this was the only solution I could come up with. Maybe I am misunderstanding what you mean, but your advice from reading through it doesn't seem like it will do what I want to achieve. I will try it though, when I am at a computer. – Snap Jun 14 '19 at 13:47