Declare textbox event in XAML

Asked

Viewed 89 times

0

How do I declare the Keypresseventargs event from a textbox to work with this event in Code Behind? The code to run the event I have, I just can’t declare the event, as I do with Click, for example. That I can. I thought for keypress, keydown, it was not necessary to declare, but when I create my event in Code Behind, gives me error in textbox. Look at my codes. XAML:

<TextBox x:Name="txtTara" HorizontalAlignment="Left" Height="23" Margin="161,130,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="120"/>

Code Behind:

private void txtTara(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '.' || e.KeyChar == ',')
    {
        //troca o . pela virgula
        e.KeyChar = ',';

        //Verifica se já existe alguma vírgula na string
        if (txtTara.Text.Contains(","))
        {
            e.Handled = true; // Caso exista, aborte 
        }
    }

    //aceita apenas números, tecla backspace.
    else if (!char.IsNumber(e.KeyChar) && !(e.KeyChar == (char)Keys.Back))
    {
        e.Handled = true;
    }
}

That code I copied here on Sopt.

1 answer

3


You’re making a huge mess.

First of all, KeyPressEventArgs is not a event is only one class used to pass parameters for the KeyPress.

So, this does not exist. This class comes from Windows Forms, it makes no sense to try to use it because its element is not of this namespace.

For WPF what is used is KeyEventArgs.

private void txtTara_KeyPress(object sender, KeyEventArgs e) { }

Second, there is no event KeyPress in WPF, you will have to use the KeyDown.

Third, change the method name to txtTara_KeyPress. Because: 1) there is already an element with this name (the component itself); and 2) this is the C nomenclature standard#.

And finally, it is necessary to link the event to the visual element. Somehow the visual needs to know that there is an event triggered, there is no magic to figure it out.

To link an event to a componenen, it is necessary to declare in XAML.

For example:

<TextBox x:Name="txtTara" 
         HorizontalAlignment="Left" Height="23" Margin="161,130,0,0"  
         TextWrapping="Wrap" Text="0" VerticalAlignment="Top" 
         Width="120" KeyDown="txtTara_KeyPress"/>
  • Linq, I need to declare something in the shaman, how do we do with the Click events? Thus, gives error.

  • I was editing Agpra

  • It is giving Violation of the Nomenclature Rule error for txtTara, as if there were already two such guys.

  • 1

    Of course, young man. You put the same name for the component and for the event

  • I switched to txtTara_KeyDown, as I do for Click, but added some using and does not recognize Keys or Keychar. And worse, there is no suggestion for this. I believe they’re Winform’s, correct?

  • 1

    It’s because they’re not part of the class KeyArgs. You’ll have to write the code using this class. It won’t do you much good to copy a Winforms code and hope it works.

  • I know, it’s just that this code is in a question with wpf. But I’m going to read it here and see how to do that. Anyway, the original question was answered and I’ve already marked it as an answer.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.