opening same form again from button click in winform - c#

I have a button in my dashboard form opening another form called entry, but when entry is minimised and if i click again on the open button in dashboard another window is opening instead of maximising the same minimised form. i am very new to this visual studio 2019 & c#.
private void Btngotoentryform_Click(object sender, EventArgs e)
{
FrmDataEntry f = new FrmDataEntry();
f.Show();
}

Create a global instance of the form on which you can perform nullcheck
private FrmDataEntry _instance = null;
private void Btngotoentryform_Click(object sender, EventArgs e)
{
if(_instance == null)
{
_instance = new FrmDataEntry();
_instance.Show();
}
else
{
_instance.WindowState = WindowState.Maximized;
_instance.Activate();
}
}

Related

Navigating through different forms in C#

I recently started working on an application with windows forms using C#. To navigate through the different forms I am using:
Form Form_name = new Form ();
Form_name .TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Form_name.Show();
The current setup is that I have the main menu form where you then navigate to a upload photo form. But after uploading an image in the upload photo I want to show the next form. But because we now are working in another form we cant use:
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Because panel1 is referenced in the first form. I tried to create a public class to clear it but had no success.
My questions come down to:
Is there a better method to navigate through different forms/windows?
Can we create a public function to clear the panel in the first form?
MainMenu.cs
private void mainMenu1_Click(object sender, EventArgs e)
{
ME_Upload ME_UploadConstruct = new ME_Upload();
ME_UploadConstruct.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(ME_UploadConstruct);
ME_UploadConstruct.Show();
}
private void mainMenu2_Click(object sender, EventArgs e)
{
IndividualEditor individualEditor = new IndividualEditor();
individualEditor.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(individualEditor);
individualEditor.Show();
}
}
ME_Upload.cs
private void ME_UploadButton_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
ME_ImageEditor ME_ImageEditorConstruct = new ME_ImageEditor();
ME_ImageEditorConstruct.TopLevel = false;
panel1.Controls.Clear(); ///Not Working
panel1.Controls.Add(ME_ImageEditorConstruct); ///Not Working
ME_ImageEditorConstruct.Show();
}
}

How to close the previous child form on clicking another child form in mdi using c#

I have a mdi parent form and I open my other forms in run time as mdi child form by this code:
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
{
Upload objWA = new Upload();
objWA.MdiParent = this;
objWA.Show();
//objWA.WindowState = FormWindowState.Maximized;
}
private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
Reports objUI = new Reports();
objUI.MdiParent = this;
objUI.Show();
//objUI.WindowState = FormWindowState.Maximized;
}
but the problem is: When current form is open, user can open another form and it can be repeated several times so that each form is opened what's code for closing the previous child form before user open a new child form??
Screen shot for reference
if we see the image my upload and reports forms both are getting opened on one another but it should only show the currently opened form
private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
{
Upload objWA = new Upload();
objWA.MdiParent = this;
objWA.Show();
//objWA.WindowState = FormWindowState.Maximized;
}
private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
Reports objUI = new Reports();
objUI.MdiParent = this;
objUI.Show();
DisposeAllButThis(Form objUI);
//objUI.WindowState = FormWindowState.Maximized;
}
public void DisposeAllButThis(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType()
&& frm != form)
{
frm.Dispose();
frm.Close();
}
}
}
let me know if it worked because i just made it up
public void DisposeAllButThis(Form form){
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType()
&& frm != form)
{
frm.Dispose();
frm.Close();
}
}}
got this from:
C# loop through all MDI childer and close all other forms except the current

open a window(wpf) on the foreground from a page(wpf)

I am working at a WPF desktop application, in this application I open a wpf page which contains a listbox of contacts on a frame.
When I double-click on a contact, a window opens to modify it. The problem is that the window is opened in the back and I want it on the foreground , I used this code
private void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
if (listBox.SelectedItem != null)
{
modify_contact window = new modify_contact(list[listBox.SelectedIndex]);
window.Owner=this;
window.Show();
}
}
It didn't work because it's and not a window
So what can I do to make my opened on the foreground ?
private void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
if (listBox.SelectedItem != null)
{
modify_contact window = new modify_contact(list[listBox.SelectedIndex]);
window.Owner = MainWindow.GetWindow(this);
window.Show();
}
}
You can always create a Window and set it's Content like this
private void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
modify_contact window = new modify_contact(list[listBox.SelectedIndex]);
Window windowHost = new Window
{
Title = "I'm the title",
Content = window
};
windowHost.ShowDialog();
}

Closing an active mdi child

I have this code where i can close and open a child form using a menu strip. My question is how do i close a specific active child form if i have multiple child forms that is active?
private void fileMenu_Click(object sender, EventArgs e)
{
frmtview tv = new frmtview();
if (ActiveMdiChild != null)
{
ActiveMdiChild.Close();
}
else
{
tv.MdiParent = this;
tv.Dock = DockStyle.Left;
tv.Show();
}
}
private void Home_Load(object sender, EventArgs e)
{
frmtview tv = new frmtview();
tv.MdiParent = this;
tv.Dock = DockStyle.Left;
tv.Show();
}
Do you mean all opened child windows? If so, when open/create child window, add the object to the list<>(member variable). when click over the close menu, just iterate all items in the list and call close method.
List childControls = new List();
void Closeclick(.......)
{
foreach(UserControl uc in childControls)
{
uc.Close();
}
}
void ActivateClick(.......)
{
HomeForm home = new HomeForm();
childControls.Add(home);
home.Show();
}

I need to close the previous active window when navigating a windows form with menustrip

I'm putting together a simple UI that interacts with a SQL database. My problem is a UI problem, ever time a menustrip item is selected, it opens a new active window. How do I set this up to close the previous active window? I've tried using Form.Close();, but that just closes everything.
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_campus go = new if_add_campus();
go.Show();
}
private void addDepartmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_dept go = new if_add_dept();
go.Show();
}
private void addEmployeToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_employee go = new if_add_employee();
go.Show();
}
Just keep track of the last form you created in a variable:
private Form lastForm;
private void showForm(Form frm) {
frm.FormClosed += (sender, ea) => {
if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
};
frm.Show();
if (lastForm != null) lastForm.Close();
lastForm = frm;
}
And use showForm() to display your forms:
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new if_add_campus());
}
Not tested, should be close.

Categories