Answered by:
Open another window in WPF

Question
-
Sir
I am new in WPF.
I have a login (frmLogin) WPF window and I want to open a main window (frmMan) when I click a button. But the show method is not appearing in the list. Please advice
Private Sub btnCancel_Click(sender As Object, e As RoutedEventArgs) Handles btnCancel.Click End End Sub Private Sub btnLogin_Click(sender As Object, e As RoutedEventArgs) Handles btnLogin.Click If txtUser.Text = "" Then MsgBox("Please enter user name..",MsgBoxStyle.OkOnly ,"Stop!") Exit Sub End If If txtPassword.Password = "" Then MsgBox("Please enter password..") Exit Sub End If Me.Close() frmMain.Show() End Sub Private Sub frmLogin_Loaded(sender As Object, e As RoutedEventArgs) Handles frmLogin.Loaded txtPassword.Password = "" txtUser.Text = "" txtUser.Focus() End Sub End Class
Here frmMain.show () is not working.
Here is the xaml page for frmMain
<Window x:Name="frmMain" x:Class="frmMain" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="" Height="300" Width="300" ShowInTaskbar="False"> </Window>
Regards
Manoj
Manoj
- Moved by KareninstructorMVP Wednesday, March 16, 2016 12:49 AM Moved from VB.NET
Tuesday, March 15, 2016 11:38 PM
Answers
-
You are using the class to try to open rather than an instance of the class.
What you need is something like :
Dim formInstance as frmMain = new frmMain() formInstance.Show()
Lloyd Sheen
- Proposed as answer by Magnus (MM8)MVP Wednesday, March 16, 2016 8:37 PM
- Marked as answer by Manoj Joseph Friday, March 18, 2016 3:57 AM
Wednesday, March 16, 2016 10:12 AM
All replies
-
Hello Manoj Joseph, beings this is a WPF question I am moving this to the WPF forum for better visibility.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Wednesday, March 16, 2016 12:49 AM -
You are using the class to try to open rather than an instance of the class.
What you need is something like :
Dim formInstance as frmMain = new frmMain() formInstance.Show()
Lloyd Sheen
- Proposed as answer by Magnus (MM8)MVP Wednesday, March 16, 2016 8:37 PM
- Marked as answer by Manoj Joseph Friday, March 18, 2016 3:57 AM
Wednesday, March 16, 2016 10:12 AM -
Sir
Thank you for the help.
But I could open the window in VB just using the code
frmMain.show() then why I should declare the instance in WPF?
Regards
Manoj
Manoj
Friday, March 18, 2016 4:00 AM -
Remember that VB is the programming language. It works with both WinForms and WPF. Using WinForms you can do that but in WPF you can't. It is always safer to create your own instance and as you have seen in WPF you have to.
Lloyd Sheen
Friday, March 18, 2016 10:50 AM