locked
Clear Textbox and RichTextbox when form is in focus again RRS feed

  • Question

  • Hi,

    I am currently working on a small directory searcher and at the moment I need my program to be able to clear the current data in the textbox and richtextbox when the main form comes back into focus. So the user would search for a user on the first form which would then display data in a richtextbox and if the data is correct the user can then continue by pressing the next button. When the user has closed form 2, I want the text in the current textbox and rich text box to be cleared. How could I do this? I have had a search and suggested to try the form in focus handler possibly ?

    This is how I am viewing form 2

    ResetPassword cp = new ResetPassword(adUser, adName);
    cp.Show();
    

    Then on form 2 I use this.Close() to return to the first form

    Regards

    Jordan

    • Moved by Kristin Xie Thursday, June 11, 2015 9:58 AM move to the right forum
    Wednesday, June 10, 2015 8:23 PM

Answers

  • If I understand you correctly, you could display the second form using ShowDialog instead of Show. In this case, execution will not return to the first form until the second form closes, so you can clear the textboxes immediately after the call.

    ResetPassword cp = new ResetPassword(adUser, adName);
    cp.ShowDialog();
    
    // Clear the textboxes here, because we won't get here until 
    // cp closes.

    It would also be a good idea to check the DialogResult that's returned from ShowDialog, and take different action based on whether cp was closed by OK or Cancel or whatever (so the user has a chance to cancel out if they don't want their changes to take effect).

    • Proposed as answer by BonnieBMVP Thursday, June 11, 2015 4:52 AM
    • Marked as answer by Youjun Tang Thursday, June 18, 2015 5:24 AM
    Thursday, June 11, 2015 1:36 AM

All replies

  • This sounds like a WinForms question, and those have a seperate subforum.

    I am also not 100% certain what you want as behavior. Could you give a example use case of these two forms?

    Wednesday, June 10, 2015 9:11 PM
  • Hi,

    Sorry if I have placed this under the wrong forum, on a side note, how do I get it moved to the correct location?

    The application is basically a directorysearcher which then stores the username and then goes to the next form to reset the users password. So the user would type in a username into the textbox into form 1 and then it would print data to the richtextbox and then if the data is correct the user then presses the change password button which then opens up the password reset form and this contains two textboxs, one is for new password and one is to confirm the new password, on submit button this would then reset the AD password for the user in form 1. On closure of form 2 I want the textbox and rich text box in form 1 to be cleared.

    Regards

    Wednesday, June 10, 2015 9:25 PM
  • If I understand you correctly, you could display the second form using ShowDialog instead of Show. In this case, execution will not return to the first form until the second form closes, so you can clear the textboxes immediately after the call.

    ResetPassword cp = new ResetPassword(adUser, adName);
    cp.ShowDialog();
    
    // Clear the textboxes here, because we won't get here until 
    // cp closes.

    It would also be a good idea to check the DialogResult that's returned from ShowDialog, and take different action based on whether cp was closed by OK or Cancel or whatever (so the user has a chance to cancel out if they don't want their changes to take effect).

    • Proposed as answer by BonnieBMVP Thursday, June 11, 2015 4:52 AM
    • Marked as answer by Youjun Tang Thursday, June 18, 2015 5:24 AM
    Thursday, June 11, 2015 1:36 AM