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.
Linq, I need to declare something in the shaman, how do we do with the Click events? Thus, gives error.
– pnet
I was editing Agpra
– Jéf Bueno
It is giving Violation of the Nomenclature Rule error for txtTara, as if there were already two such guys.
– pnet
Of course, young man. You put the same name for the component and for the event
– Jéf Bueno
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?
– pnet
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.– Jéf Bueno
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.
– pnet