user7597016
user7597016

Reputation:

C# WPF - The longer I press on a button the faster will be counted

I just "build" a Numeric Up Down button. Now I have a question, I would like that if I press the button longer the longer the presses are counted faster. But only after 1-2 seconds. As well as the normal Numeric Up Down Button. If you clicked on the button, the faster it was counted up.

How do I do that ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace loeschen
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            //Regex regex = new Regex("[^0-9-)]");
            // Regex regex = new Regex("[^0-9-)]");
            //e.Handled = regex.IsMatch(e.Text);
            //int zahl;
            //e.Handled = int.TryParse(e.Text, out zahl);

            Regex regex = new Regex(@"-?\d+(?:\.\d+)?");
            e.Handled = !regex.IsMatch(e.Text);


        }

        private void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox.SelectAll();
        }

        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
        }

        private void button_Copy1_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(tb.Text))
                tb.Text = ((Int32.Parse(tb.Text)) + 1).ToString();


        }

        private void button_Copy2_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(tb.Text))
                tb.Text = ((Int32.Parse(tb.Text)) - 1).ToString();

        }

        private void tb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {

                if (!String.IsNullOrEmpty(tb.Text))
                {

                    if (Int32.Parse(tb.Text) > 100)
                    {
                        label.Content = "Limit erreicht";
                        tb.Text = (Int32.Parse(tb.Text) - 1).ToString();
                    }
                    else if (Int32.Parse(tb.Text) < -100)
                    {
                        label.Content = "Limit erreicht";
                        tb.Text = (Int32.Parse(tb.Text) + 1).ToString();
                    }
                    else if(Int32.Parse(tb.Text) < 100 && Int32.Parse(tb.Text) > -100)
                    {
                        label.Content = "";
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}

<Window x:Class="loeschen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:local="clr-namespace:loeschen"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="#FFA8EE9D">
        <Canvas HorizontalAlignment="Left" Height="46" Margin="75,25,0,0" VerticalAlignment="Top" Width="132" Background="White">
            <Button x:Name="button" Content="▲" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="116" Canvas.Top="2"/>
            <Button x:Name="button_Copy" Content="▼" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="116" Canvas.Top="24" RenderTransformOrigin="2.069,0.293"/>
            <TextBox x:Name="textBox" HorizontalAlignment="Left" BorderThickness="0" Height="22" BorderBrush="Transparent" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="79" Canvas.Left="37" Canvas.Top="12" SelectionBrush="Transparent" TextAlignment="Center" Background="{x:Null}" GotFocus="textBox_GotFocus"/>



        </Canvas>
        <Canvas HorizontalAlignment="Left" Height="45" Margin="290,201,0,0" VerticalAlignment="Top" Width="99" Background="White">
            <TextBox x:Name="tb" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" BorderThickness="0" Height="22" BorderBrush="Transparent" TextWrapping="Wrap" Text="99" VerticalAlignment="Top" Width="79" Canvas.Top="13" SelectionBrush="Transparent" TextAlignment="Center" Background="{x:Null}" GotFocus="textBox_GotFocus" TextChanged="tb_TextChanged"/>
        </Canvas>
        <Canvas HorizontalAlignment="Left" Height="45" Margin="370,201,0,0" VerticalAlignment="Top" Width="19" Background="Gainsboro">
            <Button x:Name="button_Copy1" Content="▲" HorizontalAlignment="Left" VerticalAlignment="Top" Height="19" Width="19" FontSize="9" Click="button_Copy1_Click"/>
            <Button x:Name="button_Copy2" Content="▼" HorizontalAlignment="Left" VerticalAlignment="Top" Height="19" Width="19" FontSize="9" Canvas.Top="26" Click="button_Copy2_Click"/>
        </Canvas>
        <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="329,154,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>

Upvotes: 1

Views: 388

Answers (1)

BugFinder
BugFinder

Reputation: 17858

You could add a timer and in on mousebutton down, start the timer, in the mousebutton up, stop it.

The timer, if time since mouse down is >2s could +1 to an increment, so that the count goes up by the increment.. so each iteration it goes up faster and faster, or just uses the timer to +1 if <2s and +5 or something after 2s .. choices are yours

Upvotes: 2

Related Questions