none
How to do KeyPress in WPF ? RRS feed

  • Question

  • I want to translate a project from C# to WPF

    I have the function  

      private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
            {
                if (CheckNum(e.SuppressKeyPress.ToString()) == false)
                    e.Handled = true;
                else
                {
                    if (Logic.redError == true)
                    {
                        
                        Brd.CheckLineAndRow(I, J, e.KeyValue.ToString());
                        SimpleCell SC = (SimpleCell)this.Brd.MatCell[I, J];
    
                        if (SC.value.ToString() == e.KeyValue.ToString())
                            LibLogic.ClsPoint.points = LibLogic.ClsPoint.points + 10;
                        else LibLogic.ClsPoint.points = LibLogic.ClsPoint.points - 5;
                    }
                    if (Brd.CheckWin())
                    {
    
                        frmWin f = new frmWin();
                        f.ShowDialog();
                       
                    }
                }
                pictureBox1.BringToFront();
                textBox1.CaretBrush = System.Windows.Media.Brushes.Transparent;
              
            }

    How can I do this in WPF ?

    Tuesday, March 3, 2015 11:37 AM

Answers

  • There is no SuppressKeyPress property in WPF but since this is just a boolean property that is false by default in Windows Forms you could just pass the string "False" to the CheckNum method:

    if (CheckNum(false.ToString()) == false)
                    e.Handled = true;

    And you should handle the KeyDown event. Then the event handler will get a KeyEventArgs argument and this one has a Key property:


     private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
    ...
            
    Brd.CheckLineAndRow(I, J, e.Key.ToString());
    ...
        }


     <TextBox  KeyDown="TextBox_KeyDown"/>


    Please remember to close your threads by marking posts that provide a solution to your issue as answer.

    • Marked as answer by black94 Tuesday, March 3, 2015 3:04 PM
    Tuesday, March 3, 2015 2:20 PM
  • >>For Key it doesn't work. I'm in MouseDown (not in KeyDown) ...

    Since there is no Key pressed when the MouseDown event occurs you will never be able to detect which key is pressed...this just doesn't make any sense at all.

    Since you handled the KeyPress event in your Windows Forms application you should handle the KeyDown event in your WPF application and not the MouseDown event.

    The MouseDown event does not correspond to the KeyPress event so you cannot convert your Windows Forms KeyPress event handler to a WPF MouseDown event handler. This makes no sense and your code will not work because there is no way to detect which key that was pressed unless there actually was some key pressed...

     

    Please remember to close your threads by marking posts that provide a solution to your issue as answer.

    • Marked as answer by black94 Tuesday, March 3, 2015 3:57 PM
    Tuesday, March 3, 2015 3:13 PM

All replies

  • You could handle the TextChanged event of the TextBox.

    Or the KeyDown event:


     private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
    }
    
    
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
    
        }
    

    Please remember to close your threads by marking helpful posts as answer.


    Tuesday, March 3, 2015 11:43 AM
  • Thank you

    I choose KeyDown. But there is 2 elements that WPF doesn't accept (Key and SuppressKeyPress:

       private void textBox1_MouseDown(object sender, MouseButtonEventArgs e)
            {
    
                if (e.RightButton == MouseButtonState.Pressed)
                {
                    textBox2.Visibility = Visibility.Hidden;
                    textBox2.Focus();
    
                }
                else
                {
                    timer1.IsEnabled = true;
                    textBox2.Visibility = Visibility.Visible;
                    textBox1.CaretBrush = System.Windows.Media.Brushes.Transparent;
    
              }
                if (CheckNum(e.SuppressKeyPress.ToString()) == false)
                    e.Handled = true;
                else
                {
                    if (Logic.redError == true)
                    {
    
                        Brd.CheckLineAndRow(I, J, e.Key.ToString());
                        SimpleCell SC = (SimpleCell)this.Brd.MatCell[I, J];
    
                        if (SC.value.ToString() == e.Key.ToString())
                            LibLogic.ClsPoint.points = LibLogic.ClsPoint.points + 10;
                        else LibLogic.ClsPoint.points = LibLogic.ClsPoint.points - 5;
                    }
                    if (Brd.CheckWin())
                    {
    
                        frmWin f = new frmWin();
                        f.ShowDialog();
    
                    }
                }
                pictureBox1.BringToFront();
                textBox1.CaretBrush = System.Windows.Media.Brushes.Transparent;
              
            }

    Here the errors :

    Error1 'System.Windows.Input.MouseButtonEventArgs' does not contain a definition for 'SuppressKeyPress' and no extension method 'SuppressKeyPress' accepting a first argument of type 'System.Windows.Input.MouseButtonEventArgs' could be found (are you missing a using directive or an assembly reference?)

    Error 2 'System.Windows.Input.MouseButtonEventArgs' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'System.Windows.Input.MouseButtonEventArgs' could be found (are you missing a using directive or an assembly reference?)


    Do you know how can I write it ok in WPF ?

    Tuesday, March 3, 2015 1:22 PM
  • You seem to be checking Mousebutton events in that and textbox keydown in your first post.

    Which are you doing?

    I guess textbox and your last bit of code is completely wrong.

    https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.keydown%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            textBlock1.Text = "You Entered: " + textBox1.Text;
        }
    }

    I think this has been said before.

    But

    Directly translating winforms code to wpf will be a rocky road.


    Hope that helps.
    Recent Technet articles: Property List Editing;   Dynamic XAML

    Tuesday, March 3, 2015 2:02 PM
    Moderator
  • There is no SuppressKeyPress property in WPF but since this is just a boolean property that is false by default in Windows Forms you could just pass the string "False" to the CheckNum method:

    if (CheckNum(false.ToString()) == false)
                    e.Handled = true;

    And you should handle the KeyDown event. Then the event handler will get a KeyEventArgs argument and this one has a Key property:


     private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
    ...
            
    Brd.CheckLineAndRow(I, J, e.Key.ToString());
    ...
        }


     <TextBox  KeyDown="TextBox_KeyDown"/>


    Please remember to close your threads by marking posts that provide a solution to your issue as answer.

    • Marked as answer by black94 Tuesday, March 3, 2015 3:04 PM
    Tuesday, March 3, 2015 2:20 PM
  • For SuppressKeyPress, it's OK. Thanks.

    For Key it doesn't work. I'm in MouseDown (not in KeyDown) and I already did what you've told me but it still not work lke you can see : 

     <TextBox  Name="textBox1" Width="{Binding ElementName=grid, Path=ActualWidth}" Grid.Row="0" Grid.RowSpan="2" BorderThickness="0" MouseDown="textBox1_MouseDown" MouseLeave="textBox1_MouseLeave" KeyDown="textBox1_KeyDown" />
    Have you another idea why WPF doesn't allow me to write e.Key ?
    Tuesday, March 3, 2015 3:04 PM
  • >>For Key it doesn't work. I'm in MouseDown (not in KeyDown) ...

    Since there is no Key pressed when the MouseDown event occurs you will never be able to detect which key is pressed...this just doesn't make any sense at all.

    Since you handled the KeyPress event in your Windows Forms application you should handle the KeyDown event in your WPF application and not the MouseDown event.

    The MouseDown event does not correspond to the KeyPress event so you cannot convert your Windows Forms KeyPress event handler to a WPF MouseDown event handler. This makes no sense and your code will not work because there is no way to detect which key that was pressed unless there actually was some key pressed...

     

    Please remember to close your threads by marking posts that provide a solution to your issue as answer.

    • Marked as answer by black94 Tuesday, March 3, 2015 3:57 PM
    Tuesday, March 3, 2015 3:13 PM
  • Ok

    But if I write what Written in TextBox1_MouseDown in the function textBox1_KeyDown, is it ok ?

    Tuesday, March 3, 2015 3:59 PM