d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# | Insert 1-9 Buttons Text In A Text Box
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Mar 5 2013 10:18pm
I have 9 buttons from 1 to 9 (text) I have to insert their text value in a text box and in red. Already have the following code:

Code
       private void btnChiffre_Click(object sender, EventArgs e)
       {
           Button b = (Button)sender;
           txtch.Text = b.Text;
       }    

       private void btn1_Click(object sender, EventArgs e)
       {
           
       }

       private void btn2_Click(object sender, EventArgs e)
       {
           
       }

       private void btn3_Click(object sender, EventArgs e)
       {

       }

       private void btn4_Click(object sender, EventArgs e)
       {

       }

       private void btn5_Click(object sender, EventArgs e)
       {

       }

       private void btn6_Click(object sender, EventArgs e)
       {

       }

       private void btn7_Click(object sender, EventArgs e)
       {

       }

       private void btn8_Click(object sender, EventArgs e)
       {

       }

       private void btn9_Click(object sender, EventArgs e)
       {

       }


This post was edited by eric838 on Mar 5 2013 10:18pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 5 2013 10:57pm
That's all you need:

Button b = (Button)sender;
txtch.Text = b.Text;


since the code's the same, i recommend getting rid of the other 9 methods. for the 9 buttons, just attach the same method to all their click events.
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Mar 5 2013 11:09pm
Quote (carteblanche @ 5 Mar 2013 23:57)
That's all you need:

Button b = (Button)sender;
          txtch.Text = b.Text;



since the code's the same, i recommend getting rid of the other 9 methods. for the 9 buttons, just attach the same method to all their click events.


for each button?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 5 2013 11:33pm
Quote (eric838 @ Mar 6 2013 12:09am)
for each button?


i think you missed the important part of my reply:

Quote (carteblanche @ Mar 5 2013 11:57pm)
That's all you need:

Button b = (Button)sender;
          txtch.Text = b.Text;


since the code's the same, i recommend getting rid of the other 9 methods. for the 9 buttons, just attach the same method to all their click events.


/edit: i assume you want to set the text based on the button clicked

This post was edited by carteblanche on Mar 5 2013 11:47pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Mar 6 2013 12:01am
Quote (carteblanche @ 6 Mar 2013 00:33)
i think you missed the important part of my reply:



/edit: i assume you want to set the text based on the button clicked


like what
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Mar 6 2013 09:45am
Quote (eric838 @ 6 Mar 2013 01:01)
like what


Quote (carteblanche @ 5 Mar 2013 23:57)
That's all you need:

Button b = (Button)sender;
          txtch.Text = b.Text;


since the code's the same, i recommend getting rid of the other 9 methods. for the 9 buttons, just attach the same method to all their click events.


That's the problem

Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Mar 6 2013 09:58am
Is that what u mean?

Code
this.btn1.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn2.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn3.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn4.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn5.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn6.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn7.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn8.Click += new System.EventHandler(this.btnChiffre_Click);
this.btn9.Click += new System.EventHandler(this.btnChiffre_Click);


Code

       private void btnChiffre_Click(object sender, EventArgs e)
       {
           Button b = (Button)sender;
           txtch.Text = b.Text;
       }

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll