KeyPress event using textbox C# winform [duplicate]












0
















This question already has an answer here:




  • Assigning event TextChanged to all textboxes in Form

    3 answers




I have lot of textbox in my winform. maybe 20 to 30 textbox.
and I need all texbox have numerical only when typing.



I use keypress event per textbox to handle the numerical only(code below)



 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}


is there possible way to inherit all textboxes in just 1 keypress event?










share|improve this question















marked as duplicate by mjwills, Community Nov 16 '18 at 3:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    You alreay have the event, just assign it to all textboxes' 'keypress' event.

    – Poul Bak
    Nov 16 '18 at 3:22











  • In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

    – Poul Bak
    Nov 16 '18 at 3:23











  • @PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

    – mr.jm
    Nov 16 '18 at 3:31











  • Do you create the textboxes dynamically?

    – Poul Bak
    Nov 16 '18 at 3:35
















0
















This question already has an answer here:




  • Assigning event TextChanged to all textboxes in Form

    3 answers




I have lot of textbox in my winform. maybe 20 to 30 textbox.
and I need all texbox have numerical only when typing.



I use keypress event per textbox to handle the numerical only(code below)



 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}


is there possible way to inherit all textboxes in just 1 keypress event?










share|improve this question















marked as duplicate by mjwills, Community Nov 16 '18 at 3:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    You alreay have the event, just assign it to all textboxes' 'keypress' event.

    – Poul Bak
    Nov 16 '18 at 3:22











  • In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

    – Poul Bak
    Nov 16 '18 at 3:23











  • @PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

    – mr.jm
    Nov 16 '18 at 3:31











  • Do you create the textboxes dynamically?

    – Poul Bak
    Nov 16 '18 at 3:35














0












0








0









This question already has an answer here:




  • Assigning event TextChanged to all textboxes in Form

    3 answers




I have lot of textbox in my winform. maybe 20 to 30 textbox.
and I need all texbox have numerical only when typing.



I use keypress event per textbox to handle the numerical only(code below)



 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}


is there possible way to inherit all textboxes in just 1 keypress event?










share|improve this question

















This question already has an answer here:




  • Assigning event TextChanged to all textboxes in Form

    3 answers




I have lot of textbox in my winform. maybe 20 to 30 textbox.
and I need all texbox have numerical only when typing.



I use keypress event per textbox to handle the numerical only(code below)



 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}


is there possible way to inherit all textboxes in just 1 keypress event?





This question already has an answer here:




  • Assigning event TextChanged to all textboxes in Form

    3 answers








c# visual-studio winforms keypress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:20









JohnB

1,78311117




1,78311117










asked Nov 16 '18 at 3:17









mr.jmmr.jm

206




206




marked as duplicate by mjwills, Community Nov 16 '18 at 3:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by mjwills, Community Nov 16 '18 at 3:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    You alreay have the event, just assign it to all textboxes' 'keypress' event.

    – Poul Bak
    Nov 16 '18 at 3:22











  • In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

    – Poul Bak
    Nov 16 '18 at 3:23











  • @PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

    – mr.jm
    Nov 16 '18 at 3:31











  • Do you create the textboxes dynamically?

    – Poul Bak
    Nov 16 '18 at 3:35














  • 3





    You alreay have the event, just assign it to all textboxes' 'keypress' event.

    – Poul Bak
    Nov 16 '18 at 3:22











  • In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

    – Poul Bak
    Nov 16 '18 at 3:23











  • @PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

    – mr.jm
    Nov 16 '18 at 3:31











  • Do you create the textboxes dynamically?

    – Poul Bak
    Nov 16 '18 at 3:35








3




3





You alreay have the event, just assign it to all textboxes' 'keypress' event.

– Poul Bak
Nov 16 '18 at 3:22





You alreay have the event, just assign it to all textboxes' 'keypress' event.

– Poul Bak
Nov 16 '18 at 3:22













In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

– Poul Bak
Nov 16 '18 at 3:23





In VS, select all textboxes, then in property inspector, go to events and select your eventhandler.

– Poul Bak
Nov 16 '18 at 3:23













@PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

– mr.jm
Nov 16 '18 at 3:31





@PoulBak I already did it all textbox had keypress event. I'm asking if there's possible way for simple coding not apply keypress event to all textbox.

– mr.jm
Nov 16 '18 at 3:31













Do you create the textboxes dynamically?

– Poul Bak
Nov 16 '18 at 3:35





Do you create the textboxes dynamically?

– Poul Bak
Nov 16 '18 at 3:35












1 Answer
1






active

oldest

votes


















3














Implement the keypress event for only one textbox.



Then select another text box and go to keypress event and select the method you already implemented.



enter image description here






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Implement the keypress event for only one textbox.



    Then select another text box and go to keypress event and select the method you already implemented.



    enter image description here






    share|improve this answer




























      3














      Implement the keypress event for only one textbox.



      Then select another text box and go to keypress event and select the method you already implemented.



      enter image description here






      share|improve this answer


























        3












        3








        3







        Implement the keypress event for only one textbox.



        Then select another text box and go to keypress event and select the method you already implemented.



        enter image description here






        share|improve this answer













        Implement the keypress event for only one textbox.



        Then select another text box and go to keypress event and select the method you already implemented.



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 3:44









        Gihan Saranga SiriwardhanaGihan Saranga Siriwardhana

        604424




        604424















            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini