TreeView problem...

Please!!! need help!!!

I have 2 problems (i think from the same reason)
i built a treeView (with the great help from vo1d - here in this site) which opens every time the same form (this is the first problem), and also i get  an error massage when i try to close the form (the only form) that has been opened with the treeView.
the error massage is :

 "Forms that are already visible cannot be displayed as a modal dialog. Set the form's visible property to false before calling showDialog."

the code for the treeView is:

in the constractor:
          frmWorkersRcrmnt varWorkersRcrmnt;
          frmTeachrsPeriodEval varfrmTeachrsPeriodEval;
          frmTeachersEvaluation varfrmTeachersEvaluation;
          frmMainEmployee varfrmMainEmployee;
          frmMainCandidates varfrmMainCandidates;

in the InitializeComponent part:

 public frmMainForm()
  {
    InitializeComponent();
               
     this.varWorkersRcrmnt  = new frmWorkersRcrmnt();
     this.varfrmTeachrsPeriodEval = new frmTeachrsPeriodEval();
     this.varfrmTeachersEvaluation = new frmTeachersEvaluation();
     this.varfrmMainEmployee = new frmMainEmployee();
     this.varfrmMainCandidates = new frmMainCandidates();

     this.treeView1.Nodes[0].Tag=this.varWorkersRcrmnt;
                                                   
     this.treeView1.Nodes[0].Nodes[0].Tag=this.varfrmTeachrsPeriodEval;
                                           
     this.treeView1.Nodes[1].Nodes[0].Tag = this.varfrmTeachersEvaluation;  
     this.treeView1.Nodes[3].Tag = this.varfrmMainEmployee;                                            
     this.treeView1.Nodes[4].Tag = this.varfrmMainCandidates;                                            
     
     this.treeView1.BeforeSelect += new TreeViewCancelEventHandler    
                                                                              (treeView1_BeforeSelect);
    }

and also in the treeview BeforeSelect:

     void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
     {
       if(e.Node.Tag != null && e.Node.Tag is Form)
          {
             (e.Node.Tag as Form).ShowDialog();
          }
     }

in the form "frmWorkersRcrmnt" in the closing event :

private void frmWorkersRcrmnt_Closing(object _      
                                                        _ sender,System.ComponentModel.CancelEventArgs e)
     {
          e.Cancel = true;
          (sender as Form).Hide();
     }

All the forms that opened are Modal Forms! because i wrote:  "ShowDialog" (with "show" it works fine) but i need them to be Modal!
Whats wrong????
LVL 1
udirAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anyoneisSoftware DeveloperCommented:
You call a form with ShowDialog(), making it modal. But then you cancel the Close? Go ahead and let the close happen - and lose the Hide() - that's already done for you.

"When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application."

David
anyoneisSoftware DeveloperCommented:
I said "Let the close happen" which is not correct. The whole point is that the modal form is not closed, but remains alive.

I didn't know that!

David
udirAuthor Commented:
Hi David,
Thanks for the reply.
I took off the Closing Event from the form "frmWorkersRcrmnt", but still got the same error massage.
I understand from your comment the logic of the Modal forms and the DialogResult but anyway - still got the problem.
If you can look at my first question (when i asked how to creat TreeView) it will be great.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21815524.html

vo1d wrote :

in your forms which shall be shown, you will have to go into the FormClosing event to avoid the forms being disposed, if the are closed.
that should look like this:

private void FormClosingEventHandler(object sender, FormClosingEventArgs e)
{
  e.Cancel = true;
  (sender as Form).Hide();
}

so, what does it mean?
i got the error massage if i put the closing event or if i take it off.
or maybe the problem is somewhere else?
Thanks
Udir
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

anyoneisSoftware DeveloperCommented:
I didn't expect that the Closing event would be called given that this is a modal form, but...

The mere presence of the closing event handler appears to alter the normal dialog processing. So either leave out the closing event handler or take vo1d's suggestion of a closing handler with the cancel and the hide - either seems to work for my test program. (I wonder what the difference is?)

Meanwhile, I have no clue why/how you are getting that error, Udir.  I think it must be somewhere else....

Was that a typo in the top message (object_sender -vs- sender)?

private void frmWorkersRcrmnt_Closing(object _      
                                                        _ sender,System.ComponentModel.CancelEventArgs e)
     {
          e.Cancel = true;
          (sender as Form).Hide();
     }

David
sumixCommented:

  when the treeview form is shown for the first time, the root node is selected and BeforeSelect event is raised. When you close the second form (varWorkersRcrmnt), BeforeSelect event is raised again, treeView1_BeforeSelect method tries to show once more the varWorkersRcrmnt form but this hadn't been closed. Hence the error you get...

 What you should do is to put the code that displays the form in AfterSelect event handler (instead of BeforeSelect).

 Another option (in case you don't want to have varWorkersRcrmnt automatically displayed when the treeview form is loaded) is to assign the handler for BeforeSelect (or AfterSelect) event when the treeview form is Activated first time, not when the instance is created:

   public frmMainForm()
  {
     InitializeComponent();
      ....
      this.Activated+= new System.EventHandler(this.frmMainForm_Activated);              
     
  }

   private void frmMainForm_Activated(object sender, System.EventArgs e)
   {
        // this needs to happen only the first time
        this.Activated-= new System.EventHandler(this.frmMainForm_Activated);

        this.treeView1.BeforeSelect += new TreeViewCancelEventHandler  (treeView1_BeforeSelect);
   }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
udirAuthor Commented:
Hi sumix,
great!!! it works geate with the AfterSelect
Thanks
(and David, thank you also for the effort).
Udir
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.