locked
Accessing controls of one form in another RRS feed

  • Question

  • I have placed a notification icon control in main form.... i will place it in system tray if user selects some option in form1!!! and i should be able to remove the notification icon from system tray if user selects another option which is in form5.... i shud remove when he selects an option instantly........ is there any way i can remove notification icon which is a control of form1 from form5????

     

    I used  below statements in form5  and i also changed the notification icon property from private to public

     

    form1 f = new form1();

    f.notificationicon1.visible=false;   

     

    but above code dosent seems to work is there any way i can do it????

     

    Thanks and Regards,

     

    Tuesday, June 5, 2007 3:51 PM

Answers

  • Hmya, you are creating a new instance of Form1 instead of using the existing one.  The best way to do this is to do it way Windows Forms does it: raise an event on Form5 and write an event handler in Form1 to catch the event.  For example:

    In Form5:
        public event EventHandler MyEvent;
        private void button1_Click(object sender, EventArgs e) {
          if (MyEvent != null) MyEvent.Invoke(this, EventArgs.Empty);
        }

    In Form1:
        private void button1_Click(object sender, EventArgs e) {
          Form5 f5 = new Form5();
          f5.MyEvent += HandleEvent;
          f5.Show();
        }
        private void HandleEvent(object sender, EventArgs e) {
          this.notifyIcon1.Visible = !this.notifyIcon1.Visible;
        }

    With this setup, Form5 knows nothing about Form1, that's a Good Thing.  Do try to come up with better names...
    Tuesday, June 5, 2007 5:07 PM