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.