Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF-我按下按钮的时间越长,计数的速度就越快_C#_Wpf_Button_Numeric_Toolbox - Fatal编程技术网

C#WPF-我按下按钮的时间越长,计数的速度就越快

C#WPF-我按下按钮的时间越长,计数的速度就越快,c#,wpf,button,numeric,toolbox,C#,Wpf,Button,Numeric,Toolbox,我只是“构建”一个数字上下按钮。现在我有一个问题,我想如果我按下按钮的时间越长,按下的次数就越快。 但只有在1-2秒之后。以及普通的数字上下按钮。如果你点击按钮,它计数的速度就越快 我该怎么做 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Wi

我只是“构建”一个数字上下按钮。现在我有一个问题,我想如果我按下按钮的时间越长,按下的次数就越快。 但只有在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)
            {

            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统诊断;
使用System.Text.RegularExpressions;
名称空间loeschen
{
/// 
///Interaktionslogik für MainWindow.xaml
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
private void NumberValidationTextBox(对象发送者,文本组合目标)
{
//正则表达式正则表达式=新正则表达式(“[^0-9-”);
//正则表达式正则表达式=新正则表达式(“[^0-9-”);
//e、 Handled=regex.IsMatch(e.Text);
//国际扎尔;
//e、 Handled=int.TryParse(e.Text,out-zahl);
正则表达式正则表达式=新正则表达式(@“-?\d+(?:\。\d+);
e、 Handled=!regex.IsMatch(e.Text);
}
私有无效文本框_GotFocus(对象发送方,路由目标)
{
textBox.SelectAll();
}
私有无效文本框1_GotFocus(对象发送方,路由目标)
{
}
私有无效按钮\u复制1\u单击(对象发送者,路由目标)
{
如果(!String.IsNullOrEmpty(tb.Text))
tb.Text=((Int32.Parse(tb.Text))+1.ToString();
}
私有无效按钮\u复制2\u单击(对象发送者,路由目标)
{
如果(!String.IsNullOrEmpty(tb.Text))
tb.Text=((Int32.Parse(tb.Text))-1.ToString();
}
私有void tb_TextChanged(对象发送方,textchangedventargs e)
{
尝试
{
如果(!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=“”;
}
}
}
捕获(例外)
{
}
}
}
}


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


如果鼠标按下后的时间>2秒,计时器可以+1增加一个增量,使计数增加一个增量。。所以每次迭代都会越来越快,或者如果我也喜欢他的解决方案,就使用+1的定时器。我认为mousebutton down事件应该在control_click事件之前触发,不是吗?这是第一个版本,但它没有Windows窗体好,我如何使它更漂亮?数字不是一行下来的。你说的“数字不是一行下来的”是什么意思?是的,我知道。但我该怎么做才能让事情变得不那么糟呢?你清楚地表明你根本不知道自己在做什么。或者理解为什么要添加代码。我建议你找出代码为什么会这样做,然后你就会明白如何让它做你想做的事情。
<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>