0

I have a button in my main form. When pressed, the child form appears. The child form acts as a notification.

Generally when any Window(of any application) is open, and if we click on another app the open window gets deactivated and goes behind the app(on which we clicked).

But notification(of any app) does not go off the screen(or go behind) when something is pressed. I want something like that.

Example

The "Magnifier" app in windows, does not go behind the screen when another app is clicked while the "Magnifier" app is on.

I want to code the child form so as it does not go off-screen even after deactivating.

My Code(of opening the child form)-

 private void button2_Click(object sender, EventArgs e) {
            this.Visible = false; // to make the main form invisible

            for (int x = 0; x <= x++; x++) { //infinite loop
                notificationForm n = new();
                n.StartPosition = FormStartPosition.Manual;
                n.Location = new Point(0, 0);
                n.ShowDialog();

                Task.Delay(3000).Wait(); // I made a for loop and included this line of code to open the notification(child form) every 3 seconds after closing it
            }
        }
Naitik
  • 55
  • 1
  • 8
  • Use a [Toast Notification](https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-ux-guidance) instead? See [Send a local toast notification from C# apps](https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop) -- So you don't need to fight other applications, or the System. – Jimi Aug 16 '21 at 14:14
  • @Jimi For that, I need to handle TFM which I don't really know about – Naitik Aug 16 '21 at 14:34
  • 1
    If you simply want your windows to stay on top of other windows. See [how to make a windows always stay on top](https://stackoverflow.com/questions/683330/how-to-make-a-window-always-stay-on-top-in-net) – JonasH Aug 16 '21 at 14:34
  • Does this answer your question? [How to make a window always stay on top in .Net?](https://stackoverflow.com/questions/683330/how-to-make-a-window-always-stay-on-top-in-net) – JonasH Aug 16 '21 at 14:51
  • If I read [how should duplicate questions be handled](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) correctly, it should be closed as duplicate. – JonasH Aug 16 '21 at 14:55
  • Trying to make you app *stay on top* is not exactly a good choice. See the examples here: [Toast Notifications in Win Forms .NET 4.5](https://stackoverflow.com/q/51374101/7444103), which also points to these notes: [Windows.UI.Notifications is missing](https://stackoverflow.com/a/39141010/7444103). You'll find out that is simpler than it may look like and the problem is actually solved. -- If you didn't, you should read Raymond Chen's notes here: [What if two programs did this?](https://devblogs.microsoft.com/oldnewthing/20050607-00/?p=35413) – Jimi Aug 16 '21 at 14:58

1 Answers1

1

Try setting the form's TopMost property to true, like this:

private void button2_Click(object sender, EventArgs e) {
        this.Visible = false; // to make the main form invisible

        for (int x = 0; x <= x++; x++) { //infinite loop
            notificationForm n = new();
            n.StartPosition = FormStartPosition.Manual;
            n.Location = new Point(0, 0);
            n.TopMost = true;
            n.ShowDialog();

            Task.Delay(3000).Wait(); // I made a for loop and included this line of code to open the notification(child form) every 3 seconds after closing it
        }
    }