I readed manu articles about this, but – still confused.
from Form1 I open Form2:
new Form2().Show();
On Form2.ClosingEvent I need:
Form1.TextBox1.Visible = false;
What code and exactly where should I put – to achieve this ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that, the way your code is currently structured, your instance of the
Form2class knows nothing about your instance of theForm1class. Therefore, it cannot access properties or call methods on the other object. Remember thatForm1andForm2are the names of classes, not objects.The hacky solution would be to add a public field to your
Form2class that holds a reference to yourForm1object. You would fill in that field after you create an instance of yourForm2class, but before you call theShowmethod.The next problem you’ll run into is that, by default, controls on a form are
private, which means that only code inside of the class that defines the form can access them. They cannot be accessed or manipulated from code inside of a different class.The design you have is fundamentally broken from an object-oriented perspective. One class should not be manipulating or accessing private members of another class.
If anything, you should be handling this all in
Form1. Modify theForm2class to raise an event when it gets closed, and then subscribe to that event fromForm1. Inside of theForm1event handler method, hide the textbox.The quick and dirty solution would be to switch to the
ShowDialogmethod, which shows another form and blocks execution until that form gets closed. Then you could write:However, the disadvantage of modality is that the user cannot interact with
Form1whileForm2is open. It’s not clear from your question whether or not that is workable. If not, I recommend the previous solution, which involves raising an event. Either way, I highly recommend that you pick up a book on object-oriented programming in C#. Design like this is hard to fix later if you get it wrong.