Search code examples
c#wpfmahapps.metro

KeyUp event is doubling using "mahapps.metro"


Complete project: https://uploadfiles.io/wz8ji

Follow code:

Code C#:

public partial class MainWindow : MetroWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Textbox_KeyUp(object sender, KeyEventArgs e)
    {
        if (textbox.Text != string.Empty)
        {
            this.ShowMessageAsync("This is the title", "Some message");
        }
    }
}

Code xaml:

<Controls:MetroWindow x:Class="Wpf.MainWindow"
        xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
        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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl HorizontalAlignment="Left" Height="324" Margin="88,31,0,0" VerticalAlignment="Top" Width="605">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <TextBox 
                        x:Name="textbox"
                        HorizontalAlignment="Left"
                        Height="23"
                        Margin="10,10,0,0"
                        TextWrapping="Wrap"
                        Text="TextBox"
                        VerticalAlignment="Top"
                        Width="120"
                        KeyUp="Textbox_KeyUp"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </Grid>
</Controls:MetroWindow>

In my text box, when I quickly erase and type, the event is duplicated. When I delete and type "1" normally, everything is fine.The problem is when I delete and type any very fast character. When I do this quickly, the event is duplicated.

I also use this: http://mahapps.com/

I created another project without using "mahapps", it works right.

Here's how it's happening:

Press the backspace and release + 1 keys quickly.

enter image description here

Any Solution ?


Solution

  • Well, I have submitted your code to different tests and the method is not duplicated...

    int i = 0;
    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if(((TextBox)sender).Text != string.Empty)
        {
            System.Diagnostics.Debug.WriteLine("Hello " + ++i);
        }
     }
    

    Verify that another active control has no associated keyboard event, that may be the problem.

    Your code works correctly

    I have tested your code and it works well

    As you told me in the chat, you quickly press backspace + 1, well, the error is that your code detects the pulsation of any key as long as the TextBox is not empty, so, what you must do is restrict the use of Back

    private void Textbox_KeyUp(object sender, KeyEventArgs e)
    {
        if (textbox.Text != string.Empty && e.Key != Key.Back)
         {
             this.ShowMessageAsync("This is the title", "Some message");
         }
    }