Clicking the same button but different action - c#
up vote
0
down vote
favorite
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
textBox2.Focus();
c# button click focus
add a comment |
up vote
0
down vote
favorite
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
textBox2.Focus();
c# button click focus
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
textBox2.Focus();
c# button click focus
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
textBox2.Focus();
c# button click focus
c# button click focus
asked Nov 9 at 20:40
milorad sivcevic
32
32
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44
add a comment |
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
add a comment |
up vote
0
down vote
accepted
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
edited Nov 10 at 17:00
answered Nov 9 at 20:54
Rufus L
16.6k31529
16.6k31529
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
add a comment |
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Yup its good,but how to do it with 4 textboxes? (textbox1,textbox2,textbox3,textbox4)
– milorad sivcevic
Nov 10 at 12:06
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Put them in a list, access them by index, and increment an integer to track the next index. I can post an example later
– Rufus L
Nov 10 at 15:56
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
Ok i added a sample, but i did this from my phone so it's not completely tested...
– Rufus L
Nov 10 at 16:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232980%2fclicking-the-same-button-but-different-action-c-sharp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use a boolean field to store and track in which "mode" the button is (or perhaps instead of using a boolean, better create an enum for that purpose)...
– elgonzo
Nov 9 at 20:44
When you click the button your textbox will lose focus - so you can't use the focus to determine which textbox to interact with. Use a variable to keep track of the next textbox to interact with.
– Ryan Pierce Williams
Nov 9 at 20:44