I am developing a WPF project in vb.net and have multiple windows in it. When user selects a menu item a new windows opens and the problem is when the user clicks on other menu item the current window should close by itself.
How do i achieve it?
Thanks!
I think you mean this based upon your comment:
Class MainWindow
Public Win3 As Window3 = New Window3()
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Win3.Show()
Me.Close()
End Sub
End Class
There are two ways to do this:
If the menu items share the same form then in the form create a subroutine, that you call instead of new, that checks if it is already shown/created and if not opens (as you already have). If it is open reload with the new information.
Otherwise before opening the new form go though the open forms (shown below from this website in C#):
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc) {
//iterate through
}
For each form check if it's name is equal to one of the menu items and if it is close it (after saving if required). Then after you exit the for loop you open the new menu item.
Related
I have a WPF application written in VB with several windows. These windows have a several controls in them for the users to enter data. This data is then saved to a database. I want the users to be a able to edit a given set of data and it would be a lot more convenient to use the same window the data was entered in. Depending on whether the user clicks "Add" or "Edit", I want to run different code behind the window.
My issue is that I can't figure out how to differentiate between these two events. The MainWindow class has buttons "Add" and "Edit". When clicked, they create a new tab that contains a new instance of "Data.xaml". "Data.xaml" has "Data.vb" behind it. How can "Data.vb" tell whether it should execute "Edit" or "Add" code?
Simple solution is to add some property to the Data class which will tell what should be done:
Public Partial Class Data Inherits Window
// ...
Public Property Mode As Mode
// ...
End Class
where Mode is enum with two fields: Add and Edit.
In click handler of Add button set Mode to Mode.Add, in click handler of Edit button set Mode to Mode.Edit.
If you want to prevent changing of Mode after window constructed you can create new constructor that will take mode as an argument:
Public Partial Class Data Inherits Window
// ...
Public Sub New(mode As Mode)
Me.New()
Mode = mode
End Sub
Public ReadOnly Property Mode As Mode
// ...
End Class
Then in your logic in Data.vb look at the Mode and do appropriate actions.
Private Sub AddButton_Click(sender As Object, e As RoutedEventArgs)
Dim dataWindow = New Data(Mode.Add)
dataWindow.ShowDialog()
End Sub
Private Sub EditButton_Click(sender As Object, e As RoutedEventArgs)
Dim dataWindow = New Data(Mode.Edit)
dataWindow.ShowDialog()
End Sub
here is my problem: i have created a main window called mainWindow in XAML (vb.net) and inside i have 2 buttons (valid and stop) and a grid in the center.
I have two others little windows (valid window and stop window) written in XAML (vb.net) which have buttons, textbox...
I want, when i click on valid button or stop button, display the valid window or the stop window inside the grid of my mainWindow, so i have this code in my mainWindow.vb:
enter code here
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Private Sub stop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles stop.Click
Dim content As Object = stop_.Content
stop_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
So when i click on button valid in my mainWindow, it's ok it displays the valid window in my grid.
First problem: then when i click on button stop in my mainWindow, the stop window is placed just above the valid window, it is not nice, is there a way to clear the grid before display this second window?
And finally, the biggest problem: i need to click many times on valid button or stop button but when i click the second time i have a null reference exception: Me.Grid.Children.Add(content) content is null after the first call so i am only able to click one time on my button.
How can i fix it in order to click many times on my buttons please?
I give you thanks.
Yet again, someone writes some invalid code and then says why isn't this code working? If you look at your code, you should see something:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
Me.Grid.Children.Add(content)
End Sub
I'm guessing that you have a private variable for your Valid Window as you call it: valid. On this line, what are you doing to your variable?:
valid_.Content = Nothing
That's right! You're setting its Content property to null. Therefore, I'm not really sure why you were surprised that it was null after the first attempt.
How can i fix it in order to click many times on my buttons please?
Try removing the line that sets the Content property to null.
UPDATE >>>
Your problem is really caused by the fact that you cannot display any UI element in two places at once. Your whole idea of copying the Window.Content to your DataGrid is entirely wrong, but in the name of brevity, your fix is simply to move your content back to the Window, rather than setting it to null each time. Try something like this:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
If Me.Grid.Children.Count > 0 Then 'Put content back where it belongs
Dim oldContent = Me.Grid.Children(0)
stop.Content = oldContent
Me.Grid.Children.Remove(oldContent)
EndIf
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Please forgive any code mistakes, as I don't write VB.NET. Also, you'll need to update your other method likewise.
I'm stuck on this issue, please help.
I created an user control named "CreateNewCase_uc" within which I created a button Close named "btnClose"
In my MainWindow, I created a Grid named "grid1" and a button Open named "btnCreateNewCase" with this code
Private Sub btnCreateNewCase_Click(sender As Object, e As RoutedEventArgs)
Dim cnc As CreateNewCase_uc = New CreateNewCase_uc
grid1.Children.Clear()
grid1.Children.Add(cnc)
End Sub
My question : which Code I need to Write for my bntClose button which is inside the user control to close or make disappeared the user control in VB.NET
Thanks in advance for your help.
You need to get the Parent control then remove it.
' from inside your close button on UC.
Dim parent = TryCast(Me.Parent, Grid))
If Not parent Is Nothing Then
parent.Children.Remove(Me)
End If
You may simply set the UserControl's Visibility to Collapsed:
Private Sub btnCreateNewCase_Click(sender As Object, e As RoutedEventArgs)
Me.Visibility = System.Windows.Visibility.Collapsed
End Sub
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?
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.
I have a dialog window that I open from a record in a datagrid, this datagrid happens to be inside a Page that is navigated to inside a frame.
I have a button in this window, that when clicked, I would like to navigate the main window (which happens to be in a frame), to a different page that the datagrid is on.
How can I do this?
Here's the click code for the button:
Private Sub btnPlaceOrder_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnPlaceOrder.Click
Dim _M As New Main
_M.InnerFrame.Navigate(New ManualOrder())
Me.Close()
End Sub
figured it out for myself.
I needed to find the frame from the main application window in order to do this:
_F = Application.Current.Windows.Item(0).FindName("InnerFrame")