【问题标题】:C# WPF - The longer I press on a button the faster will be countedC# WPF - 我按下按钮的时间越长,计数越快
【发布时间】:2017-02-28 09:03:55
【问题描述】:

我只是“构建”了一个数字向上向下按钮。现在我有一个问题,我希望如果我按下按钮的时间越长,按下的时间就越快。 但仅在 1-2 秒后。以及普通的数字向上向下按钮。如果你点击了按钮,它被计数的速度越快。

我该怎么做?

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>

【问题讨论】:

    标签: c# wpf button numeric toolbox


    【解决方案1】:

    您可以添加一个计时器并在鼠标按下时启动计时器,在鼠标按下时停止它。

    计时器,如果鼠标按下后的时间大于 2 秒,则可以 +1 递增,因此计数会随着递增而增加。所以每次迭代它都会越来越快,或者只是使用计时器来 +1如果

    【讨论】:

    • 我也更喜欢他的解决方案。我认为 mousebutton down 事件应该在 control_click 事件之前触发,不是吗?
    • pastebin.combosa.com/23 这是第一个版本,但不如Windows窗体好,如何使它更漂亮?数字不会连续下降。
    • “数字不会连续下降”是什么意思?
    • 是的,我知道。但是我要如何才能使它不是如此呢?
    • 您清楚地表明您不知道自己在做什么。或者理解你为什么要添加你的代码。我建议你弄清楚代码为什么会做它所做的事情,然后你就会明白如何让它做你想做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2018-01-23
    • 1970-01-01
    • 2022-11-12
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多