open a window(wpf) on the foreground from a page(wpf) - c#

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();
}

Related

opening same form again from button click in winform

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();
}
}

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();
}

Fail minimizing to system tray in C#

I want to minimize my program to the system tray by clicking an entry in the system menu of the form.
So first I created a notify icon and a context menu:
private void InitializeComponent()
{
this.components = new Container();
...
this.notifyIcon = new NotifyIcon();
this.contextMenu = new ContextMenu();
this.contextMenuItem1 = new MenuItem();
this.contextMenuItem2 = new MenuItem();
this.SuspendLayout();
this.notifyIcon.ContextMenu = this.contextMenu;
this.notifyIcon.Text = "Test";
this.contextMenu.Name = "contextMenu";
this.contextMenu.MenuItems.AddRange(new MenuItem[]
{
this.contextMenuItem1,
this.contextMenuItem2
});
this.contextMenuItem1.Name = "contextMenuItem1";
this.contextMenuItem1.Text = "&Show";
this.contextMenuItem1.Click += new EventHandler(this.contextMenuItem1_Click);
this.contextMenuItem2.Name = "contextMenuItem2";
this.contextMenuItem2.Text = "&Exit";
this.contextMenuItem2.Click += new EventHandler(this.contextMenuItem2_Click);
}
Then I extended the system menu:
private void Form_Load(object sender, EventArgs e)
{
int hmenu = GetSystemMenu(Handle, 0);
AppendMenu(hmenu, 0xA00, 0, null);
AppendMenu(hmenu, 0, 111, "M&inimize to system tray");
}
A click on this menu item should fade out the main window:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x112)
{
if (m.WParam.ToInt32() == 111)
{
Visible = false;
Hide();
notifyIcon.Visible = true;
}
}
}
A click in the context menu have to reshow the program window or close the whole application:
private void contextMenuItem1_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
Show();
Visible = true;
}
private void contextMenuItem2_Click(object sender, EventArgs e)
{
Close();
}
My problem is now the following:
If I click the new entry to minimize then the WndProc method is executed successfully and the form will be hidden but no item with the caption "Test" is displayed in the system tray.
And then there's also another window visible. I think that comes from .NET but the window is completely empty so I'm not sure. Normally I should fallback to the exe file in windows explorer which starts my program, isn't it?
Thanks in advance!
+++ EDIT +++
I found out that the empty window behind my application was the console window. I only forgot to compile my project with the winexe parameter.

Bring dialog window to front

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 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