Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

Call a method in a parent from a child user control - VB.NET/WPF

Is it possible to call a method from a loaded user control?

MainWindow.xaml.vb loads user control Login.Xaml.vb.  Login.Xaml.vb has a button on it.  When that button is clicked, I want the method MainWindow.LoadUC(UserControl) to run.

This is my MainWindow.Xaml code:
Imports System.IO
Imports Microsoft.PointOfService
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Net.NetworkInformation

Class MainWindow
    Public Sub MainWindow()

        InitializeComponent()
    End Sub

    Public Sub Window_Loaded()
        LoadLogin()
    End Sub

    Public Sub Load_Login()

    End Sub

    Public Sub btnAdmin_Click()

    End Sub

    Public Sub btnLogOut_Click()

    End Sub

    Public Sub LoadUC(ByVal userControl As UserControl)
        mainPanel.Children.Clear()
        mainPanel.Children.Add(userControl)
    End Sub

    Private Sub LoadLogin()
        Dim LoginScreen As New Login
        'Content = LoginScreen

        mainPanel.Children.Add(LoginScreen)

    End Sub

End Class

Open in new window


And the Login User Control code is:
Public Class Login
    Public uc As UserControl


    Public Sub numberclick()
        MsgBox("Number Click")
    End Sub

    Public Sub Login()

    End Sub
    Public Sub enterclick() Handles btnZero_Copy9.Click
        MsgBox("Number Click")
    End Sub
    Public Sub clearclick()
        MsgBox("Number Click")
        uc = New UC_Text
        
        'Not sure how to send "uc" back to MainWindow.
        'This is what I want to do, but doesn't work:
        MainWindow.LoadUC(uc)

    End Sub
End Class

Open in new window


Maybe there's a better way to go about it.  Basically, I need to load various user controls, and put them in the MainWindow's area, replacing the User Control that was there before.

Thanks for your help!!!
SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightlyoff
slightlyoff

ASKER

Thanks - what would be the best way to do that?
 
In MainWindow code:
Dim LoginScreen As New Login
LoginScreen.mwindow = MainWindow

Open in new window


And then in Login
Public mwindow as Page

Open in new window


I'm new to WPF - I've been doing WinForms too long...  sorry if it's a silly question.

Thanks again for the quick reply!

Steve
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok - I'll give it a try.  I'm assuming, other thank vb.net markup, the winform and wpf function close to the same here?
I won't get to try it until later tonight, but I'll let you know.

Thanks for the help!
Appropriate answers for the questions- weighted due to the time taken to write example code.