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();










share|improve this question





















  • 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















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();










share|improve this question





















  • 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













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();










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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













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;






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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;






share|improve this answer






















  • 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














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;






share|improve this answer






















  • 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












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;






share|improve this answer














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;







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20