Link to home
Start Free TrialLog in
Avatar of udir
udir

asked on

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????
Avatar of anyoneis
anyoneis
Flag of United States of America image

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
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
Avatar of udir
udir

ASKER

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.

https://www.experts-exchange.com/questions/21815524/TreeView-problem.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
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
ASKER CERTIFIED SOLUTION
Avatar of sumix
sumix

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of udir

ASKER

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