• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 686
  • Last Modified:

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????
0
udir
Asked:
udir
  • 3
  • 2
1 Solution
 
anyoneisCommented:
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
0
 
anyoneisCommented:
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
0
 
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
anyoneisCommented:
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
0
 
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);
   }

0
 
udirAuthor Commented:
Hi sumix,
great!!! it works geate with the AfterSelect
Thanks
(and David, thank you also for the effort).
Udir
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now