1

I'm trying to have 2 forms on start up, one is a log in form that can be pushed aside or closed, I just need it to open ontop of my main menu. I've tried this:

public partial class MainMenu : Form
{
    public MainMenu()
    {
        InitializeComponent();
        Log_In login = new Log_In();
        login.Show();
        login.Activate();
    }

But that open the Log_In form underneith my main menu. How would I go about making it pop up on top of my main menu as soon as the program starts? Thanks!

Nathan
  • 1,287
  • 6
  • 15
  • 32
  • 1
    @Brian, that will cause the program to terminate when the log in form closes. I just saw the `Load` event, maybe I'll try using that – Nathan Nov 21 '13 at 18:50
  • @Nathan - You are 100% correct (it has been forever since I have used Winforms). Deleting the comment. – Brian Nov 21 '13 at 18:51
  • maybe you need to look into MdiParent, MdiChild. [See this](http://msdn.microsoft.com/en-us/library/aa984329%28v=vs.71%29.aspx) – Manish Mishra Nov 21 '13 at 18:52
  • Load event still showed the form underneith :/ – Nathan Nov 21 '13 at 18:54
  • try [this](http://stackoverflow.com/questions/14061746/can-not-focus-onto-a-form/14065365#14065365) one. – spajce Nov 21 '13 at 18:58

5 Answers5

2

You can Show the form after the previous form has been displayed. Then it will be shown in front and non-modal so you can move it about while continuing to use the background form..

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        Login login = new Login();
        login.Show();
    }
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Login login;

        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= MainWindow_Loaded;

            login = new Login();

            login.Show();
        }
    }
}
Paul Sinnema
  • 2,534
  • 2
  • 20
  • 33
1

The problem that you're having is that you're showing the login form in the main form constructor. The main form hasn't yet been shown, but it will be shown at some point in the future. You need to wait until after the main form has finished being shown to show the login form:

public Form1()
{
    EventHandler shownHandler = null;
    shownHandler = (s, args) =>
    {
        Log_In login = new Log_In();
        login.Show();
        Shown -= shownHandler;
    };
    Shown += shownHandler;
}

Note that, to prevent the login form from being shown multiple times in the event that you hide and then re-show the login form, you can remove the handler the first time it's called.

Servy
  • 202,030
  • 26
  • 332
  • 449
0
public partial class MainMenu : Form
{
    public MainMenu()
    {
        InitializeComponent();
        this.Shown += new System.EventHandler(this.MainMenu_Shown);
    }
    private void MainMenu_Shown(object sender, EventArgs e)
    {
        Log_In login = new Log_In();
        login.Show();
        login.Activate();
    }
}
Kemo Sabe
  • 99
  • 4
-1

Try setting the owner for Form 2 as so:

public Form1()
{
    InitializeComponent();

    Form2 login = new Form2();
    login.Show(this);
}

How do you make a non-modal topmost dialog that is only topmost in regards to the parent form in WinForms?

Community
  • 1
  • 1
Sourav 'Abhi' Mitra
  • 2,390
  • 16
  • 15