Hi Sir, Mam,
I would like to ask how can I call WPF form from another WPF form. I have a login WPF form and once I click login button
it will go to Main Admin form. how can get this? I am not much familiar/new on WPF (.Net).
Thank you.
Regards,
Rodel Barbin

Tasadduq BurneyRank #1,427 · 1.1K reputationPosted Sep 1, 2023, 5:00 PM EDT
In WPF, you can navigate from one Window (WPF form) to another using various methods. One common approach is to use the
NavigationWindoworFramecontrol for navigation. However, if you have a simple application with just two windows (Login and Main Admin), you can achieve this by creating and displaying the Main Admin window when the Login button is clicked. Here's how you can do it:Assuming you have two WPF windows:
LoginWindow.xamlandMainAdminWindow.xaml.In the code-behind for
LoginWindow.xaml.cs, handle the button click event to open the Main Admin window:using System; using System.Windows; namespace YourNamespace { public partial class LoginWindow: Window { public LoginWindow() { InitializeComponent(); } private void LoginButton_Click(object sender, RoutedEventArgs e) { // Instantiate and show the MainAdminWindow MainAdminWindow mainAdminWindow = new MainAdminWindow(); mainAdminWindow.Show(); // Close the current LoginWindow if needed this.Close(); } } }In the
App.xaml.csfile, set the startup window to be theLoginWindow:using System; using System.Windows; namespace YourNamespace { public partial class App: Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // Set the startup window to LoginWindow LoginWindow loginWindow = new LoginWindow(); loginWindow.Show(); } } }Now, when you run the application, it will start with the
LoginWindow. When you click the "Login" button, theMainAdminWindowwill be displayed, and you can navigate between these two windows as needed.Remember to replace
YourNamespacewith the actual namespace of your application in the code above.Tahir AnsariRank #366 · 6.8K reputationPosted Sep 1, 2023, 1:24 AM EDT
Create your WPF Application:
First, create a new WPF application project in Visual Studio.
Design Your Windows:
Design your login window and main admin window using XAML. You can create these windows using the WPF designer in Visual Studio.
Code-Behind for Login Window:
In the code-behind of your login window (e.g.,
LoginWindow.xaml.cs), you should have code that handles the login button click event. When the login button is clicked and the credentials are valid, you can open the main admin window.private void LoginButton_Click(object sender, RoutedEventArgs e) { // Check if the login credentials are valid. if (IsValidCredentials()) { // Create an instance of the MainAdminWindow. MainAdminWindow mainAdminWindow = new MainAdminWindow(); // Show the MainAdminWindow. mainAdminWindow.Show(); // Close the login window (optional). this.Close(); } else { // Display an error message to the user. MessageBox.Show("Invalid username or password"); } }Code-Behind for Main Admin Window (optional):
In the code-behind of your main admin window (e.g.,
MainAdminWindow.xaml.cs), you can add code to initialize the main admin window and perform any necessary tasks when it loads.Set Startup Window:
In the App.xaml.cs file, set the startup window of your application to be the login window in the
OnStartupmethod:protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); LoginWindow loginWindow = new LoginWindow(); loginWindow.Show(); }This code ensures that the login window is the first window displayed when your application starts.
Build and Run:
Build your application and run it. When you click the login button with valid credentials, it should open the main admin window.
Abhishek YadavRank #217 · 11.3K reputationPosted Sep 1, 2023, 2:19 AM EDT
To open a second WPF form from the first form, you can follow these steps:
Create a new WPF Window by right-clicking on your project and selecting "Add" -> "Window" from the context menu. Name it as your second form, for example, "MainWindow.xaml".
In the first form (login form), handle the click event of the login button. You can do this in the code-behind file, for example, "LoginWindow.xaml.cs". Here is an example code snippet:
private void LoginButton_Click(object sender, RoutedEventArgs e) { // Create an instance of the second form MainWindow mainWindow = new MainWindow(); // Show the second form mainWindow.Show(); // Close the current form (login form) this.Close(); }In the second form, you can add any additional controls or functionality you need.
Note that you may need to adjust the code according to your specific requirements. For example, you might want to pass data between forms or handle form close events differently.
Also, make sure to set the "StartupUri" in your App.xaml file to your login form, so it becomes the entry point of your application:
With these steps, clicking the login button on the first form will open the second form and close the first form.