4
votes

Maybe first I will tell about my application. When for example an employee will log into my application, he will have loaded "Employee Menu":

    Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

This is from Window_Loaded event. Menu is User Control, and there I have few buttons to open and operate another user controls. When I press for example button "Question":

Public Class Employee
    Dim mw As New MainWindow
    Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
        Dim Que As New QuestionAdd
        mw.MainGrid.Children.Add(Que)
        Grid.SetRow(Que, 2)
        Grid.SetColumn(Que, 1)
    End Sub
End Class

I don't know why nothing is loading after button_click..... Is it so hard to navigate main windows grid from other controls?

1
Have you tried setting a breakpoint on the btnQuestionAdd_Click method to make sure it's being called in the first place? Perhaps your event handling is not correct. - JDB
Hmm, Que is Nothing. What I did as additional now is - I've put button on main window with the code for loading use control, and It works, but no in EMployee User COntrol... - user2678787

1 Answers

1
votes

This is just a guess as you've not provided much information, but I noticed the following issue which may be the culprit.

In your first code snippet, you seem to be creating the employee from the MainForm:

Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

Your following comment would seem to confirm that assumption:

This is from Window_Loaded event. Menu is User Control, and there I have few buttons to open and operate another user controls. When I press for example button "Question"

And yet, in your Employee class, you are creating a brand new instance of MainWindow and then adding data to it:

Public Class Employee
    Dim mw As New MainWindow
    Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
        Dim Que As New QuestionAdd
        mw.MainGrid.Children.Add(Que)
        Grid.SetRow(Que, 2)
        Grid.SetColumn(Que, 1)
    End Sub
End Class

If this observation is correct, then I think you need to go back to the books and understand the concept of classes and instances.

You essentially created a second form (which is hidden because you never explicitly show it) and then modify this second form rather than the original. To prove this hypothesis, try adding the following line of code:

Public Class Employee
    Dim mw As New MainWindow
    Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
        Dim Que As New QuestionAdd
        mw.MainGrid.Children.Add(Que)
        Grid.SetRow(Que, 2)
        Grid.SetColumn(Que, 1)
        mw.Show() ' <---
    End Sub
End Class

You'll likely see a second form pop up with all of the changes your were expecting on your first form.

As to how to fix this, the easiest path is going to be adding a parameter to your initializer ("Sub New") which accepts the MainForm as a value. You can then assign the value to a field or property (probably just your mw field) and continue on your merry way. This is going to give you headaches down the road, though, so it might be a good time to start learning more about software architecture, especially the concept of separation of concerns.