简体   繁体   中英

How do you create an event to undo the previous event in WPF with VB.NET

I was wondering if there is any relatively simple way to essentially create an 'undo' button that would undo whatever event happened right before it. The problem is, I can't do it explicitly (for example, if the background was white, and then it became red, I can't just have the undo button reset the background as white). I can't do it this way because I won't know which event happened last, there could be a number of events that could have happened, and I don't want an individual undo button for every single event.

To provide an example, I have a few labels in a grid, and when I mouse over any label it changes to a bigger size, and all the other labels become a standard (smaller) size. However, sometimes one of the labels will already be a bigger size (from a button, or such) -- lets call this label1. So when I mouse over a different label -- lets call this label2 -- then label2 becomes bigger whereas label1 is small now. But when I move the mouse off of label2, I want label1 to be big again whereas label2 should be small again. Thanks for any help/ strategies in advance!!

PS I'm pretty new to WPF so the simpler the solution the better :) But anything is appreciated!

EDIT: I think an easy way of asking this is: Is there any way to create a MouseLeave event that undoes whatever was done by a MouseEnter event?

I don't do VB so sorry for conventions, but this should work for you as a quick example:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel x:Name="stackPanel">
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >1</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >2</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >3</Label>
        <Label FontSize="10" Margin="10" MouseEnter="OnMouseEnter" >4</Label>
     </StackPanel>
 </Window>

code behind:

Class MainWindow

Dim _mouseLeaveSize As Double = 10
Dim _mouseEnterSize As Double = 20

Private Sub OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)

    For Each child As Visual In stackPanel.Children
        SetLabelLeaveProperties(child)
    Next

    Dim label = CType(sender, Label)
    label.FontSize = _mouseEnterSize

End Sub

Private Sub SetLabelLeaveProperties(ByVal myVisual As Visual)

    Dim label = TryCast(myVisual, Label)

    If label Is Nothing Then
        'iterate thru children to see if anymore labels 
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(myVisual) - 1
            Dim child = VisualTreeHelper.GetChild(myVisual, i)
            Dim l = TryCast(child, Label)
            If l Is Nothing Then
                SetLabelLeaveProperties(child)  'Enumerate children of the child visual object.
            Else
                l.FontSize = _mouseLeaveSize
            End If
        Next i
    Else
        label.FontSize = _mouseLeaveSize
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM