how to make a wpf window appear after the login

Melek KHMIRI 0 Reputation points
2024-07-18T08:02:10.0833333+00:00
public partial class CustomDialog : Window

{

    private mydbcontext _dbContext;

    public CustomDialog()

    {

        InitializeComponent();

        _dbContext = new mydbcontext();

    }

    private void OKButton_Click(object sender, RoutedEventArgs e)

    {

        string login = LoginTextBox.Text;

        string password = PasswordBox.Password;

        // Search for the user in the database

        user user = _dbContext.users.FirstOrDefault(u => u.login == login && u.motdepasse == password);

        if (user != null)

        {

            // Authentication successful

            mylocaldata.mycurrentuser = user;

           

            DialogResult = true; // Close the window with a positive result (OK)

            MessageBox.Show("Login successful. Closing dialog.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);

           

           // this.Close(); // Close the window

            

        }

        else

        {

            // Display an error message if the user is not found

            MessageBox.Show("Login or password incorrect.", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error);

            //this.Close();

        }

    }

    protected override void OnClosed(EventArgs e)

    {

        _dbContext.Dispose();

        base.OnClosed(e);

    }

}

}

public partial class MainWindow : Window

{

 public MainWindow(user mycurrentuser)

 {

     try

     {

         MessageBox.Show("Initializing MainWindow...", "Debug", MessageBoxButton.OK, MessageBoxImage.Information);

         try

         {

             InitializeComponent();

             MessageBox.Show("Initialized Component", "Debug", MessageBoxButton.OK, MessageBoxImage.Information);

         }

         catch (Exception ex)

         {

             var innerexception = ex.InnerException != null ? ex.InnerException.Message : "no innerexception";

             MessageBox.Show($"Error initializing component: {ex.Message}", "InnerException, {innerexception}", MessageBoxButton.OK, MessageBoxImage.Error);

         }

         if (mycurrentuser != null) {

              MessageBox.Show("User is not null", "Debug", MessageBoxButton.OK, MessageBoxImage.Information);

              DataContext = new MainWindowViewModel(notificationcontainer, mycurrentuser);

              MessageBox.Show("DataContext set", "Debug", MessageBoxButton.OK, MessageBoxImage.Information);

              this.Show(); // Make the main window visible

              MessageBox.Show("MainWindow initialized successfully.", "Debug", MessageBoxButton.OK, MessageBoxImage.Information);

         }

         else

          {

              MessageBox.Show("Login failed. Please try again.", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error);

              this.Close(); // Close the main window if login failed

          }     catch (Exception ex)

 {

     MessageBox.Show($"Une erreur est survenue lors de l'initialisation de la fenêtre principale : {ex.Message}", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);

     this.Close(); // Close the main window if login failed

 }

}

public void Button_Click(object sender, RoutedEventArgs e)

{

} public partial class App : Application

{

    private void ApplicationStart(object sender, StartupEventArgs e)

    {

        CustomDialog customDialog = new CustomDialog();

        bool? dialogResult = customDialog.ShowDialog();

       

        if (dialogResult == true  && mylocaldata.mycurrentuser != null)

        {

            // User successfully logged in

            

                MainWindow mainWindow = new MainWindow(mylocaldata.mycurrentuser);

                mainWindow.Show();

            

        }

        else

        {

            // Handle failed login or user canceled the login

            MessageBox.Show("Login failed or canceled. Application will shut down.", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error);

            Shutdown(); // Close the application

        }

    }

}

}

Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.