C#WPF - 我按下按钮的时间越长,计数就越快
问题描述:
我只是“建立”一个Numeric Up Down按钮。现在我有一个问题,我希望如果按下按钮的时间越长,按下的时间就越快。 但只有1-2秒后。以及正常的Numeric Up Down按钮。如果你点击了按钮,它的计数就越快。C#WPF - 我按下按钮的时间越长,计数就越快
我该怎么做?
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>
答
你可以添加上mousebutton定时器和下来,开始计时,在mousebutton起来,阻止它。
定时器,如果鼠标向下的时间大于2秒,可以+1加1,这样计数就会增加......所以每次迭代都会越来越快,或者只是使用定时器+ 1如果< 2s和+5或2s后的东西..选择是你的
我也宁愿他的解决方案。我认为mousebutton down事件应该在control_click事件之前触发,不是吗? –
http://pastebin.combosa.com/23这是第一个版本,但是 它不如Windows窗体那么好,我该如何让它更漂亮?这些数字不会连续下降。 – GabelUndMesser
你是什么意思“数字不连续”? – BugFinder