I am developing a C# Windows Forms application. I would like to have a single instance of all the forms.
So, when the user clicks on the Contact button, for instance, twice, instead of having two contact forms, I would to bring the single instance contact form to the front.
How can I achieve this?
Check if the form exists in collection of open forms before creating and showing the form using Application.OpenForms
if (System.Windows.Forms.Application.OpenForms["Form1"] as Form1 != null)
MessageBox.Show("Form1 is opened");
else
MessageBox.Show("Form1 is not opened");
public static Form GetOpenedForm<T>() where T: Form {
foreach (Form openForm in Application.OpenForms) {
if (openForm.GetType() == typeof(T)) {
return openForm;
}
}
return null;
}
And in your code, where you create the ContactForm:
ContactForm form = (ContactForm) GetOpenedForm<ContactForm>();
if (form == null) {
form = new ContactForm();
form.Show();
} else {
form.Select();
}
Simple as that:
Form fc = Application.OpenForms["Form1"];
if (fc != null)
{
fc.Focus();
}
else
{
Form1 f1 = new Form1();
f1.Show();
}
You can disable the contactButton when it is clicked and open the contactForm-
private void contactButton_Click(object sender, EventArgs e)
{
contactButton.Enabled=false;
//code to open the contactForm
}
When the contactForm is closed, you can re-enable the button-
contactButton.Enabled=true;
Try this combo
First make contact form a global object
private ContactForm contactForm;
Then your contact button handler:
private void contactButton_Click(object sender, EventArgs e)
{
if (contactForm == null)
{
contactForm = new ContactForm();
contactForm.FormClosing += new FormClosingEventHandler(contactForm_FormClosing);
}
contactForm.Show();
}
Then handle the FormClosing event of the ContactForm to hide it rather than close it:
private void contactForm_FormClosing(object sender, FormClosingEventArgs e)
{
contactForm.Hide();
e.Cancel = true;
}
Or if you want the contact form to close, and open as new next time, handle the FormClosed instead:
private void contactForm_FormClosed(object sender, FormClosedEventArgs e)
{
contactForm = null;
}
Then next time the button is clicked, the null if clause will be caught and the form will be set to a new instance and opened.
Form2 form2 = null;
private void button1_Click(object sender, EventArgs e)
{
bool isFormExists = false;
foreach (Form openForm in Application.OpenForms)
{
if (openForm == form2 && openForm!=null)
{
openForm.Focus();
isFormExists = true;
break;
}
}
if (!isFormExists)
{
form2 = new Form2();
form2.Show();
}
}
I'd go with Otiel's answer. Also you can add a WindowState, because if the form is minimized it won't be shown. this answer can be used to get the restore method.
ContactForm form = (ContactForm) GetOpenedForm<ContactForm>();
if (form == null) {
form = new ContactForm();
form.Show();
} else {
//use this (if you want it to be restored to normal and focused)
//no need of extension method
//form.WindowState = FormWindowState.Normal;
//form.Select();
//or use this if you want it to be restored as previous state and focused
//You need a Restore extension method that can be found in the link above
form.Focus();
form.Restore();
}
Related
I have 2 windows forms.Both Form1 and form2 have a button. When I click the button in form1 it shows form2 and vice versa. But it create a duplicate form when I click button on each time. How to avoid it. please tell me. My code is given below.
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
}
just use this method common and pass parameter like 'this' when button click
public bool formIsExist(Form frmOpen)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm.Name == frmOpen.Name)
{
return true;
}
}
return false;
}
The problem is that you are creating a new form each time you click the button.
To solve this problem You could make your form static.
static Form1 form = new Form1();
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
My solution, you can try
Form1
private void button1_Click(object sender, EventArgs e)
{
FormCollection fc = Application.OpenForms;
bool present = false;
foreach (Form frm in fc)
{
if (frm.Name == "Form2")
{
present = true;
}
}
if (!present)
{
Form2 f2 = new Form2();
f2.Show();
}
}
Form2
private void button1_Click(object sender, EventArgs e)
{
FormCollection fc = Application.OpenForms;
bool present = false;
foreach (Form frm in fc)
{
if (frm.Name == "Form1")
{
present = true;
}
}
if (!present)
{
Form1 f1 = new Form1();
f1.Show();
}
}
The easiest way is to determine whether the target form is already there and in that case, open it.
For example, in Form1 do this:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = null;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
if (Application.OpenForms[i] is Form2)
{
f2 = Application.OpenForms[i];
break;
}
}
if (f2 == null)
f2 = new Form2();
f2.Show();
}
To make a common method for all forms, you could try this:
public static T GetForm<T>() where T : Form
{
for (int i = 0; i < Application.OpenForms.Count; i++)
{
if (Application.OpenForms[i] is T)
return (T)Application.OpenForms[i];
}
return null;
}
And call it like:
Form2 f2 = GetForm<Form2>();
if (f2 == null) f2 = new Form2();
#Mostafiz' answer is almost correct. It just misses the code to show the found present form. (Sorry, do not yet have 50 reputation to write this as a comment)
Try this code:
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form form = null;
//search all opened forms for one with name "Form2"
foreach( Form frm in Application.OpenForms )
if( frm.Name == "Form2" ) //this requires Form2 to be named "Form2"
{
form = frm;
break;
}
//if no opened Form2 was found, create a new one
if( form == null )
form = new Form2();
form.Show();
}
Form2:
private void button1_Click(object sender, EventArgs e)
{
Form form = null;
//search all opened forms for one with name "Form1"
foreach( Form frm in Application.OpenForms )
if( frm.Name == "Form1" ) //this requires Form1 to be named "Form1"
{
form = frm;
break;
}
//if no opened Form1 was found, create a new one
if( form == null )
form = new Form1();
form.Show();
}
I have a method that I use the open a Mid Child.
Lets say I am on a form called "InventoryAdd" on this form I have a drop down menu. When a user selects a value "-1" from the menu, I want to open another (ie. DepartmentsAdd().) Then after DepartmentAdd open I want to close InventoryAdd form.
this is the code behind the menu on InventoryAdd form
private void InputDepartment_SelectedValueChanged(object sender, EventArgs e) {
//InputDepartment.ValueMember = "ID";
int selectedDept = Convert.ToInt32(InputDepartment.SelectedValue);
if (selectedDept == -1) {
Form myForm = this.ActiveMdiChild;
Common.OpenMyForm("Vendors", new string[] { "add" }, new DepartmentsAdd());
if (myForm != null) {
myForm.Close();
}
}
}
This method what opens the new Mid form
public static void OpenMyForm(string sectionName, string[] keys, Form myform) {
//make sure there are no other forms of the ame type open
foreach (Form form in Application.OpenForms) {
if (form.GetType() == myform.GetType()) {
form.Activate();
return;
}
}
if (Settings._AuthenticationMode == "Thumbprint") {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;
// Display the new form.
newMDIChild.Show();
}
if (Settings._AuthenticationMode == "Single" && UserInfo.Autherized == true) {
var role = new Roles();
if (role.hasAccess(sectionName, keys)) {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;
// Display the new form.
newMDIChild.Show();
}
else {
Common.Alert("You do not have a permissions to perform this action!");
}
}
}
}
My code open the Mid form with no issues. However, the "InventoryAdd" form never close. "myForm" is always null to it never close it.
How can I properly close the Mid form?
Thanks
If you set an event listener for shown on the newly opened form in the calling context you can have the existing form close itself once its child is opened.
Form myForm=new Form();
myForm.shown += closeForm;
...
void closeForm(object sender, EventArgs e){
this.Dispose(); // as long as within the previous forms context.
}
myForm.Close();
is closing the program try to use
myForm.Hide();
On button click, I open a new form (lets say Form2), but I don't want that Form2 to open more than 1time. And I don't want to use .ShowDialog(), because it wont allow me to go to the Previous Form. How can I do that ?
You can use Application.OpenForms property to check if form already opened:
if (!Application.OpenForms.OfType<Form2>().Any())
{
Form2 form2 = new Form2();
form2.Show();
}
You can show existing form instead of creating new:
Form2 _form2 = null;
void Button1_Click(object sender, EventArgs e)
{
if (_form2 == null)
{
_form2 = new Form2();
_form2.Closed += Form2_Closed;
}
_form2.Show();
_form2.BringToFront();
}
private void Form2_Closed(object sender, System.EventArgs e)
{
_form2 = null;
}
You can issue the Show method, which will show the form and allow the users to get back to the form, but then you can also override the OnClosing event of the form:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
and that will keep the users from being able to literally close the form. Finally, if you wanted, you could Hide the form when the user closes it:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
and then you'll need to hold on to that instance in the first form so that you can re-show it when the button is clicked. So in the first form you might have a class field like this:
private Form2 _form2 = new Form2();
and then in the button click it would look like this:
_form2.Show();
and since they can't actually close the form this will always work.
You can try something like
bool windowIsNotOpen;
Mutex mutext = new Mutex(true, "Form2", out windowIsNotOpen);
if (!windowIsNotOpen)
{
// Form2 is already open
return;
}
You can do a static property that tells you whether it's open or not. You'd set it when the form is opened, and turn it off when the form is closed.
class Form2 {
public static bool IsOpen { get;set; }
public Form2() {
Load += (sender, e) => { Form2.IsOpen = true; };
Closed += (sender, e) => { Form2.IsOpen = false; };
}
}
Whenever you want to open it, check the flag first.
Form2 f;
bool isOpen = false;
private void button1_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form2(); ;
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
}
if (!isOpen)
{
isOpen = true;
f.Show();
}
}
void f_FormClosed(object sender, FormClosedEventArgs e)
{
isOpen = false;
}
Try this or use Application.OpenForms and check which one opened
I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}
I have a 'loader app' that loads a menu and when user clicks the menu image button a list view opens based on the text
(if text = employee)
(Go to class A)
(Go to class B)
...
...
(Show List View Window)
if he clicks again on the same button it opens again, I would like to prevent this.
i.e but this for a WPF application
If you want a list of the open forms, that is Application.OpenForms. You could iterate over this, using GetType() and checking the .Assembly to find those from a different assembly. Beyond that, I'm not entire clear on the question...
Assembly currentAssembly = Assembly.GetExecutingAssembly();
List<Form> formsFromOtherAssemblies = new List<Form>();
foreach (Form form in Application.OpenForms) {
if (form.GetType().Assembly != currentAssembly) {
formsFromOtherAssemblies.Add(form);
}
}
If you just want to track forms you have opened yourself, then cache that instance. Or if you use "owned forms", you can just check by name:
private void button1_Click(object sender, EventArgs e) {
foreach (Form form in OwnedForms) {
if (form.Name == "Whatever") {
form.Activate();
return;
}
}
Form child = new Form();
child.Name = "Whatever";
child.Owner = this;
child.Show(this);
}
NewProduct newproduct;
private void button1_Click(object sender, EventArgs e)
{
if(!isOpened())
{
newproduct = new NewProduct();
newproduct.Show();
}
}
private bool isOpened()
{
foreach (Form f in Application.OpenForms)
{
if (f == newproduct)
{
return true;
}
}
return false;
}
Another simple example
private Boolean FindForm(String formName)
{
foreach (Form f in Application.OpenForms)
{
if (f.Name.Equals(formName))
{
f.Location = new Point(POINT.X, POINT.Y + 22);
return true;
}
}
return false;
}
You can use a Command pattern. The loader assembly will search for commands in loaded assemblies.
For every command the loader create menu item ( or anything else, you want ), and click event will run the concrete command.
The command must know if should be created new form or used some already existing.
Mark Garvell's answer helped me to figure out what I should do, but it needed adjusting for WPF.
(In my case I wanted to close any windows not owned by the main one when it closes, but the principle is the same.)
private void EmployeeMenuItemClick(object sender, RoutedEventArgs e)
{
bool found = false;
foreach(Window w in Application.Current.Windows)
{
if(w.GetType() == typeof(EmployeeListViewWindow)
{
found = true;
break;
}
}
if(!found)
{
EmployeeListViewWindow ew = new EmployeeListViewWindow();
ew.Show();
}
}