Freigeben über


How to: Use RepeatButton to Raise Events Repeatedly

This example describes how to increment and decrement a number using the RepeatButton control in Windows Presentation Foundation (WPF).

The following example generates two RepeatButton controls, Increase and Decrease, and a text element that initially contains the number 0.

When the user clicks the Increase button, the number increments by 1 in intervals of 100 milliseconds. This behavior repeats until the user releases the button. The Decrease button, which decrements the number by 1 on every repetition, generates similar behaviors.

The buttons and text are created using the Extensible Application Markup Language (XAML), and the event handlers that process the user input are C# or Microsoft Visual Basic.

Example

<RepeatButton Width="100" DockPanel.Dock="Top" Delay="500" Interval="100" 
    Click="Increase">Increase</RepeatButton>
<RepeatButton Width="100" DockPanel.Dock="Top" Delay="500" Interval="100" 
    Click="Decrease">Decrease</RepeatButton>
<Button Name="btn" Width="100" DockPanel.Dock="Top" FontSize="16">0</Button>
void Increase(object sender, RoutedEventArgs e)
{
    Int32 Num = Convert.ToInt32(btn.Content);

    btn.Content = ((Num + 1).ToString());
}
        
void Decrease(object sender, RoutedEventArgs e)
{
    Int32 Num = Convert.ToInt32(btn.Content);

    btn.Content = ((Num - 1).ToString());
}
Sub Increase(ByVal sender As Object, ByVal e As RoutedEventArgs)

    Num = CInt(btn.Content)

    btn.Content = ((Num + 1).ToString())
End Sub

Sub Decrease(ByVal sender As Object, ByVal e As RoutedEventArgs)

    Num = CInt(btn.Content)

    btn.Content = ((Num - 1).ToString())
End Sub

For the complete sample see RepeatButtons Sample.

See Also

Reference

RepeatButton