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();
}
Related
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();
}
}
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
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();
}
I am showing a window on a button click like this:
private void showWindow(object obj)
{
var dialog = new AddItemView();
dialog.Show();
}
If the button is clicked again, while this window is still open, how do I bring this window to the front and not create a new one?
Just store the dialog object and check whether it's already been created in showWindow.
Used the windows Closed event to clear the reference to the dialog object.
AddItemView dialog;
private void showWindow(object obj)
{
if ( dialog == null )
{
dialog = new AddItemView();
dialog.Show();
dialog.Owner = this;
dialog.Closed += new EventHandler(AddItemView_Closed);
}
else
dialog.Activate();
}
void AddItemView_Closed(object sender, EventArgs e)
{
dialog = null;
}
Just a quick sketch but this should do what you want:
Window1 W = new Window1();
private void Button_Click(object sender, RoutedEventArgs e)
{
if (W.IsVisible)
W.Activate();
else
W.Show();
}
If this does not do it, maybe I have misread your question.
Edited to correct a bug.
Add this on the class constructor where you are instantiating the window. A window cannot be closed after is opened.
W.Closing += (s, e) =>
{
e.Cancel = true;
((Window)s).Hide();
};
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.