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 BurneyPosted Sep 1, 2023, 9:00 PM
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:In the
App.xaml.csfile, set the startup window to be theLoginWindow: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.Abhishek YadavPosted Sep 1, 2023, 6:19 AM
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:
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.
Tahir AnsariPosted Sep 1, 2023, 5:24 AM
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.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: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.