VB .Net WPF changing windows - wpf

I am trying to create a WPF application with a number of windows and am getting a bit confused with the correct way to do this. I understand that I need to keep the main window open for the program to run, so I hide this for the sub windows. However, the problem occurs when I want to change from one sub window to another and can be narrowed down to the following: how to catch the event when the user presses the close button in the top right (there seems to be no way to reference when this button is pressed in VB .net)?
Currently, I am closing the current window and then creating a new instance of the next window to change from one to the other, like the following:
Private Sub btnContinue_Click(sender As Object, e As RoutedEventArgs) Handles btnContinue.Click
Dim wd As New NextWindow
Me.Close()
wd.ShowDialog()
End Sub
There is an exit button:
Private Sub btnExit_Click(sender As Object, e As RoutedEventArgs) Handles btnExit.Click
Me.Close()
End Sub
and I have the following in the window closing event:
Application.Current.MainWindow.Show()
But how do I differentiate if the exit button is pressed or the continue button or the default close button in the top right of every window? if the exit button or close button is pressed, I would like to close the current window and then show the main window. If the continue button is pressed, I would like to close the current window and then show the next window.
I hope this makes sense.

I am in the belief that this old question holds your answer:
How to catch the event of the window close button(red X button on window right top corner) in wpf form?
This shows an event on Window_Closing that may be what you are looking for.
Hope that helps!

Related

How to detect a drop, regardless of where it occurred?

I am creating a WPF application with a drag and drop functionality.
When a drag is initiated, the colors of the rectangles in my window will change from white to green or red to indicate which ones are valid drop targets.
I want to revert the colors of the rectangles back to white whenever a drop was performed, regardless of where the drop occured.
I already managed to revert the colors back when the drop occured on the drop targets. But now I can't find a way to do the same thing when the drop is performed elsewhere.
I've tried something like this, but no luck for me:
Private Sub Meh(sender As Object, e As QueryContinueDragEventArgs) Handles DragSource.QueryContinueDrag()
If e.Action = DragAction.Cancel Or e.Action = DragAction.Drop Then
For Each R as Rectangle In Rectangles.Children
R.Fill = Brushes.White
Next
End If
End Sub
I even made a handler of the MouseUp event of the window to do it. Still no good.
Can anybody help me out here?

Stopping an Unloaded event in WPF

I think this isn't the best way to go about doing this but this is the only way I can think of.
I have this WPF UserControl and it is called via a menu item (click on the menu and open the UserControl).
There are a number of things the user can do on the UserControl and I keep track of the changes made (via EF and a variable that is set to true if the user makes any changes).
Now comes the part where I know I'm doing wrongly. I want the UserControl to check if there have been changes or additions made.
I have placed this in the Unloaded event trigger.
Private Sub TheUserControl_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
If (changesMade) Then
Dim answer = MessageBox.Show("Changes have been.....", "Alert", MessageBoxButton.OKCancel)
If (answer = MessageBoxResult.OK) Then
If (saveChanges() = False) Then
'stop unloading???
End If
Else
MessageBox.Show("Discarding changes.")
End if
End if
End Sub
I realised that I have placed this in the wrong EventControl but I have no idea which Event Control to place it in.
So now I'm asking, where can I put this "check" to ensure that if the user navigates away from this page (by clicking on another item in the menu) they will be asked to save the changes made before the UserControl closes and if an error occurs in the saving, it would stop the UserControl from Unloading.
Thanks.

On Click (text box,) Open Window

I am trying to build a basic front-end to link information together that would be accessible by clicking a text box (or a button) on the front page of the GUI. Basically, I have a number of text boxes for the different "functions" I want to show information on.
How do I make it so that on click, that box will open up a new window that I've already designeD? Within the solution (WPApplication,) I have all of the .xaml's set up (that are the seperate windows) in the solution already- I just want to call the on-click to open them. I figured it'd just be Show('Window Name') after private/as/handles, but it gives me an expression. Here's what I have:
Private Sub PO_info_MouseDown(Sendar As Object, e As MouseButtonEventArgs) Handles PO_Info.Mousedown
Show('PO Information')
End Sub
End Class
To flesh out my comment, the new window objects that you created are classes, you need to create an instance of the class before the Show Method will work. Lets assume that your PO Information forms class is POInfo you would need to do something like this.
POInfo POInformation = new POInfo;
POinformation.Show(); //This is where your show method is
Since your code looks like vb.net it would look like this in VB
Dim POInformation as New PoInfo
POInformation.Show()

Ookii.Dialogs - making sure another dialog appears above Progress dialog

If you're unfamiliar with Ookii.Dialogs, I suggest you look at this web page first. It is open source and you can find source code, compiled binary, documentation and sample app as a download there.
In my application, I'm using Ookii.Dialogs.Wpf.ProgressDialog to ShowDialog(this) a progress dialog that does some processing on files (this is always the application's main window). As expected, the progress dialog takes about a second before it actually becomes visible (even if it is already doing the processing of my files).
In the DoWork thread of the progress dialog, I'm also checking whether the output files already exist and asking the user whether to overwrite each file or skip the output. I use Ookii.Dialogs.Wpf.TaskDialog to ShowDialog(this) a "Task Dialog with Command Links" (looks like this) and ask the user the overwrite question -- except when the OS doesn't support it, I fall back to a regular MessageBox (the problem applies to the message box as well).
The problem occurs when my application finds an existing file right at the beginning of the progress dialog's DoWork thread. When the task dialog appears asking user whether to overwrite:
Expected behavior: The task dialog must stay on top. When the progress dialog appears (after 1s delay), it must appear behind the task dialog.
Actual behavior: The task dialog does not stay on top. When the progress dialog appears after 1s delay), it appears on top of the task dialog.
The actual behavior does not occur in subsequent overwrite requests when the progress dialog is already visible. The task dialog appears correctly on top of the progress dialog for the subsequent ones, although the user can switch back and forth the two dialogs (just can't switch to the main window from either of them).
I'm looking for this:
Best solution: Make the task dialog appear modal on the progress dialog. If the user tries to switch to progress dialog, user must not be allowed to do so.
Second best: Make the first appearance of task dialog remain on top even when the progress dialog appears after the 1s delay.
I'm not looking for the following:
Set the always-on-top flag of task dialog. I don't want the task dialog to appear always-on-top of every window on user's computer.
Add a delay before task dialog's appearance. I have tried Thread.Sleep() even in a loop, it simply hangs the execution of everything and doesn't solve the problem.
Wait till the progress dialog appears before showing the task dialog. Could work in theory, except I didn't find a way to know if the progress dialog has appeared or not.
Make the dialogs modeless. I want them both to be modal. (Besides I have tried making them modeless; it doesn't help solve the problem.)
A solution that works only for the task dialog. It must work for the regular message box as well.
I know this may a little late coming but I just pre-empted a similar problem in my own program and it reminded me of this question.
I used the windows FindWindowEx API function against the task dialogues window title property to find the dialogues window handle, then used my own class to make that an Iwin32Window which can then be used as a parent for a message box or task-dialogue.
the appropriate code is below (VB.NET)
'allows us to use the handle as a window
class window
Implements IWin32Window
private _handle
public sub new(handle as intptr)
_handle = handle
end sub
Public ReadOnly Property Handle As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
Get
Return _h
End Get
End Property
end class
'Declare the needed DLL import
class NativeMethods
<DllImport("user32.dll", SetLastError:=True, ThrowOnUnmappableChar:=True, CharSet:=CharSet.Unicode, bestFitMapping:=False)>
Public Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
end class
'Make sure you've set a window title when you run your task
sub runTask()
dim myDlg as Ookii.Dialogs.ProgressDialog
'Do what you need to here
myDlg.WindowTitle = "make sure you set a title"
end sub
sub myTask(sender as object, e As System.ComponentModel.DoWorkEventArgs)
'Checks and balances here
if fileExists then
'this is the dialog that's running our task
Dim dlg As Ookii.Dialogs.ProgressDialog = DirectCast(sender, Ookii.Dialogs.ProgressDialog)
'Find the window handle
Dim dlgHandle As IntPtr = NativeMethods.FindWindowEx(IntPtr.Zero, IntPtr.Zero, Nothing, dlg.WindowTitle)
'make it an Iwin32Window
dim dlgWindow as new window(dlgHandle)
'That can then be used as a parent for the message box or the task dialog
MessageBox.Show(dlgWindow,"The file exists, overwrite?")
end if
end sub
I'm not really very good at commenting my code or making my explanations understood so If you have any questions about what's going on I'll try and help.
Today I've read the question and found a solution for me. Tried it for TaskDialog, and this ist working. Didn't try with other dialogs.
I wrote a DialogControler to deal with MVVM.
This DialogControlar has a property called 'Owner' which contains the WPF-MainWindow.
Then I use this for invoking (in my case I've been coming from a backgroundthread in the viewmodel) and also set this in ShowDialog.
My Code:
using (var dialog = new Ookii.Dialogs.Wpf.TaskDialog())
{
dialog.WindowTitle = "My title";
dialog.MainInstruction = "My text";
var okButton = new Ookii.Dialogs.Wpf.TaskDialogButton(Ookii.Dialogs.Wpf.ButtonType.Ok);
dialog.Buttons.Add(okButton);
Owner.Dispatcher.Invoke(new Action(() => dialog.ShowDialog(Owner)));
}
With this I got a topmost window.

Blacklight Expander problem while expanding programmatically

I am doing a silverlight project in Silverlight 4 and I included the BlackLight project in my project so that I could use their new controls, especialy the dockpanel and the autoexpander, which is causing me at the moment some little problems.
What I would like to do is to have several auto-expander that will expand or collapsed when I click on button. In my case more specifically, each auto-expander have a set of parameter to fill, which in turn will fill the other expander and the current one would collaps and the just filled one would expand.
The idea is simple, yet when I use a button which is on one of my expander, it only works once... It would expand/collapse the first time, then after that, nothing. I trace the code and it seems to go through just fine, but the property value won't change
Here is my code
Private Sub BtnExpand_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BtnExpand.Click
ClientExpander.IsExpanded = False
ProjetExpander.IsExpanded = True
End Sub
Could it be a known bug, or must I reset some flags to make it work?
Edit : Forgot to mention, if it makes any difference, but the IsExpanded property seems to be a dependancy property.
Thanks.

Resources