My Child window not closed in WPF - wpf

I’m working in WPF and I’m facing an issue regarding NDI container
In my MainWindow Xaml I have the MainMdiContainer
<mdi:MdiContainer Theme="Aero" mdi:MdiChild.Closed="MdiChild_Closed" Margin="0,0,0,80" Name="MainMdiContainer" Grid.Row="1" Background="{x:Null}">
</mdi:MdiContainer>
Of course I have another xaml page named “Registration” and I also have a button in mainWindow to open the mdiChild
Public Sub btnAddUser_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnAddUser.Click
MainMdiContainer.Children.Clear()
MainMdiContainer.Children.Add(New MdiChild() With { _
.Title = "Πρόγραμμα Εσωτερικής Διαχείρισης Διορισμός Επιτροπής και Χρηστών", _
.Height = (System.Windows.SystemParameters.PrimaryScreenHeight - 500), _
.Width = (System.Windows.SystemParameters.PrimaryScreenWidth - 430), _
.Style = Nothing, _
.Content = New Registration() _
})
End Sub
Public Sub MdiChild_Closed(sender As System.Object, e As System.Windows.RoutedEventArgs)
For Each child As MdiChild In MainMdiContainer.Children.ToList()
If (child.Name.ToString().Contains("mdiChildRegistration")) Then
MainMdiContainer.Children.Remove(child)
End If
Next
End Sub
Until this stage everything goes fine and the mdiChild opens
Now I want to close the child window by pushing a button
Private Sub btnClose_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnClose.Click
Dim mainWin As New MainWindow
Dim mdiCont As MdiContainer = mainWin.MainMdiContainer
mainWin.MdiChild_Closed(sender, e)
End Sub
But the mdiChild it not close… in fact the Childern in mdiCont is empty.
When I add a Childern
<mdi:MdiChild IsTabStop="False" />
In MainWindow then the Childern in mdiCont is not empty it has one Child which is closed when I'm try to open my child whindow by pressing the button.
I suspect that something in the add procedure has to do with but I don’t know what.
Is there someone to assist me?

Finally I've made it to clese the child window
And all comes from the child code behind
Private Sub btnClose_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnClose.Click
Dim mainWin As MainWindow = Economy.Application.Current.MainWindow
mainWin.MainMdiContainer.Children.Clear()
In the declaration of mainWin it has to be refered as
'Object.Application.Current.MainWindow'
in order to transfer the child window attributes and have the ability to clear it.
Not Removed because the Remove is not clear the child window.
Thank you very much for #dymanoid.

Related

Winform dialog show modal form wait for Dialogresult

I need to use "Show()" method for dialog winform using Ironpython
and i need to get result of button pressing and use it in another part of program
how can i wait for pressing OK in modal "Show()" dialog?
mform = Form3()
mform.Show()
# How to wait for button OK
Thx
Use ShowDialog() instead of Show().
Then check "if (mform.DialogResult == DialogResult.OK)"
Suppose you have a main form called Form1 and a secondary form called AddForm which presents some result when the [OK] button is pressed.
The main form then can subscribe to the FormClosed event of the secondary form and pull the result if the user clicked on the [OK] button.
This can all happen in the subroutine where the secondary form is shown
Private Sub Button1.Click(sender as Object, e as EventArgs) Handles Button1.Click
Dim dlg as New AddForm
AddHandler dlg.FormClosed, _
Sub(e,ev) TextBox1.Text = If(dlg.DialogResult = DialogResult.OK, dlg.Result, String.Empty)
dlg.Show()
End Sub
And in the secondary form make sure there is a property called Result which contains the results
The [OK] button has a handler like so
Private Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
Result = ...
DialogResult = DialogResult.OK
Me.Close()
End Sub
and the [Cancel] button has a handler as so
Private Sub Button2_Click(sender as Object, e as EventArgs) Handles Button2.Click
DialogResult = DialogResult.Cancel
Me.Close()
End Sub

Visual Basic Saving Form info on hide

Im using Visual Basic 2008
I have 2 forms
Main, EditCustomerInfo
Main form contains the following
Public Class Main
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EditCustomerInfo.ShowDialog()
End Sub
EditCustomerInfo contains a text box and the following
Public Class EditCustomerInfo
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
WHAT IT DOES:
So with this code alone when i debug the program it takes me to the main form and allows me to click a button to open the editcustomerinfo form
When im on the editcustomerinfo form i have a textbox and a button. If something is typed in the textbox and the button is clicked then the form hides, if nothing is typed in the textbox when the button is clicked then the form closes.
WHAT I WOULD LIKE IT TO DO:
If something is typed in the textbox i would like the button on the editcustomerinfoform to hide the editcustomerinfoform and also create a button on main form that allows the user to bring the editcustomerinfo form back up with what was typed in the text box.
Suggestions?
Automatic behavior, such as this, always worries me. How do you know when a user has completed their input without a lost focus event. If there are no other controls on the screen, then users won't readily know how to trigger the event. That being said, you can use a timer to delay the screen closing from a KeyPress event.
Public Class EditCustomerInfo
Private WithEvents userInputDelay As Timer = New Timer() With {.Interval = 1000} REM 1 second delay for next user input
Public ReadOnly Property CustomerId As String
Get
Return CustomerIDTextBox.Text
End Get
End Property
Private Sub CustomerIDTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CustomerIDTextBox.KeyPress
userInputDelay.Enabled = False
REM Reset the timer
userInputDelay.Enabled = True
End Sub
Private Sub userInputDelay_Tick(sender As Object, e As KeyPressEventArgs) Handles userInputDelay.Tick
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
End Class
Add a button (Button2) to your Main. The code below will hide Button1 when the EditCustomerInfo.Textbox1.Text value is null/blank/white space. Button2's visibility is always the inverse of Button1.
Private EditCustomerInfoInstance As New EditCustomerInfo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EditCustomerInfoInstance.ShowDialog()
Button1.Visible = String.IsNullOrWhiteSpace(EditCustomerInfoInstance.CustomerId)
End Sub
Private Sub Button1_VisibleChanged(sender As Object, e As EventArgs) Handles Button1.VisibleChanged
Button2.Visible = Not Button1.Visible
End Sub

Showing and closing multiple windows

Say I have two WPF Windows. WindowA and WindowB. Each Window has two buttons. A Open button which will SHOW the other Window and a Close button that will Close the other Window (vice versa).
Example WindowA I have the following click event and sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
WindowB.Show()
CloseWeAidWindow()
End Sub
Public Sub CloseWeAidWindow()
Dim CloseWindow = Window.GetWindow(Me)
If CloseWindow IsNot Nothing Then
CloseWindow.Close()
GC.Collect()
End If
End Sub
However it closes the entire application and not just WindowA. WindowB opens for about .5 seconds.
How can I open another window and close the current window?
Sounds like your Application.ShutdownMode is set to OnMainWindowClose. If you want to the app to remain open as long as any window is visible try OnLastWindowClose.

wpf forcing update UI window during a procedure

I need only to show a custom control (a clock with rotating hands) and with this to replace the mouse cursor, if I use a file .cur or .ani to replace the mouse cursor
Me.CUrsor = New Cursor("absolute path of the .ani file")
there is no problem: I can change the cursor during a procedure: but the quality of the animation is very bad, and, also for other reasons, I'd prefer to use my little user-control. The problem is that if I write:
Me.gridScreen.Visibility = Visibility.Visible
' some operations that takes about 1 second
Me.gridScreen.Visibility = Visibility.Hidden
(gridScreen is the grid that contains the user-control)
Obviously I can see nothing, because the update of the UI happens at the end of the procedure. I have tried Me.UpdateLayout(), but it doesn't work.
I have tryed to use the dispacker in many way but none that works :-(
This is my lost attempt:
(uCurClock is the usercontrol, gridScreen a Grid placed at the top-level in the window, with trasparent background, that contains the usercontrol)
Private Sub showClock()G
Dim thread = New System.Threading.Thread(AddressOf showClockIntermediate)
thread.Start()
End Sub
Private Sub hideClock()
Dim thread = New System.Threading.Thread(AddressOf hideClockIntermediate)
thread.Start()
End Sub
Private Sub showClockIntermediate()
Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New Action(AddressOf showClockFinale))
End Sub
Private Sub hideClockIntermediate()
Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New Action(AddressOf hideClockFinale))
End Sub
Private Sub showClockFinale()
Dim pt As Point = Mouse.GetPosition(Nothing)
Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)
Me.gridScreen.Visibility = Visibility.Visible
Me.Cursor = Cursors.None
Me.UpdateLayout()
End Sub
Private Sub hideClockFinale()
Me.gridScreen.Visibility = Visibility.Hidden
Me.Cursor = Cursors.Arrow
Me.UpdateLayout()
End Sub
Private Sub u_MouseMove(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles gridScreen.MouseMove
Dim pt As Point = e.GetPosition(Nothing)
Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)
e.Handled = True
End Sub
Private Sub u_MouseEnter(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles gridScreen.MouseEnter
Me.uCurClock.Visibility = Visibility.Visible
e.Handled = True
End Sub
Private Sub u_MouseLeave(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles gridScreen.MouseLeave
Me.uCurClock.Visibility = Visibility.Hidden
e.Handled = True
End Sub
PIleggi
While the following code will do what you ask for, I suspect it won't actually help you, since you've mentioned animation. You're going to need to use multiple threads. However, just to demonstrate why that is, here's something that answers the question you've asked:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
uc1.Visibility = Visibility.Visible
Cursor = Cursors.Wait
' Push visibility changes now.
' (Sort of like DoEvents - and a horrible idea for exactly the same
' reasons that DoEvents was a total train wreck. Never actually do
' this - use a background thread instead.)
Dim df As New DispatcherFrame(True)
Dispatcher.BeginInvoke(Sub() df.Continue = False, DispatcherPriority.ContextIdle)
Dispatcher.PushFrame(df)
Thread.Sleep(1000)
ClearValue(CursorProperty)
uc1.Visibility = Visibility.Hidden
End Sub
Assuming you have some usercontrol called uc1 on the page, this will force it to be visible while your slow procedure runs.
But no animations will run. The problem is, if you're doing something slow on the UI thread, the UI thread can't do anything else - it can't run animations, it can't respond to user input. Basically the UI is frozen out. The only reason the code shown here even makes the user control visible is that it basically says "do any outstanding UI thread work now", which has the side effect of processing your change to the Visible property.
But animations happen on the UI thread too.
If you want to do this properly, you need to do the work on a background thread, possibly by using the BackgroundWorker, or by writing your own threading code.
reference DispatcherFrame Class Reference
good ole DoEvents for WPF!!!
Public Sub DoEvents()
Dim frame As New DispatcherFrame()
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf ExitFrame), frame)
Dispatcher.PushFrame(frame)
End Sub
Public Function ExitFrame(ByVal f As Object) As Object
CType(f, DispatcherFrame).Continue = False
Return Nothing
End Function

Cannot update a treeview inside a usercontrol

I created a usercontrol with a treeview inside it. The treeview will be populated if I add nodes in the onload handler of the usercontrol. But after that(for example, I click a button in its parent form), the treeview will not refresh. I can see the nodes was updated in memory, but it just cannot display on the screen. I called refresh/update after adding nodes. Any suggestion is appreciated.
I put a quick test together based on your description and it seems to paint just fine.
UserControl1
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
Inherits System.Windows.Forms.UserControl
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.SuspendLayout()
'
'TreeView1
'
Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TreeView1.Location = New System.Drawing.Point(0, 0)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Size = New System.Drawing.Size(150, 150)
Me.TreeView1.TabIndex = 0
'
'UserControl1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Controls.Add(Me.TreeView1)
Me.Name = "UserControl1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
End Class
Public Class UserControl1
Public Sub AddNewNode(ByVal text As System.String)
TreeView1.Nodes.Add(text)
End Sub
End Class
Put the usercontrol on a form with a button
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UserControl11.AddNewNode(Now.ToString)
End Sub
End Class
If you are seeing proper painting as well then look at any graphics handling in the parent form then the usercontrol then the controls within the usercontrol. We really need more info.
Thank you, Dave. I figured it out. I put the usercontrol twice to my form by mistake(I cannot remember how I did it). And the one I operate is underneath the other one. That's why I cannot see it. Sorry for wasting your time.

Resources