I have a check if a form is already open. If it’t not, it will open, if it is, it will activate the form + use a SwitchTab(int i) function. Here is some code:
public partial class Insert : Form {
public Insert() {
InitializeComponent();
}
public Insert(int tab) : this() {
SwitchTab(tab);
}
public void SwitchTab(int tab) {
tabControl1.SelectedIndex = tab;
}
}
private void OpenInsert(int tab) {
// Loop through all forms
foreach (Form f in Application.OpenForms) {
// Check if form of Insert type is found
if (f.GetType() == typeof(Insert)) {
f.Activate();
// Unknown function
f.SwitchTab(tab)
return;
}
}
// Not found, open form
Insert insert = new Insert(tab);
insert.Show();
}
The reason behind this is that the Insert form has multiple tabs and I want to switch tab if an other button in the menu is clicked. The problem is that where I want to call the SwitchTab() function, the compiler doesnt know that it’s a Insert class, but it only passes when it is. So is there some way to forcefully let it know it’s a instance of insert?
You would have to tell that it is, like so:
This will try to cast the form to it, but will throw an Exception if its not. But since you check it, there shouldn’t be a problem 🙂