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

TreeView problem...

Hi,
I'm new at vb.net (c#).
I need to open form from a LinkLables (Parent and chiled)
that has been created from a TreeView.
How do i do it?
Udir
0
udir
Asked:
udir
  • 9
  • 8
1 Solution
 
vo1dCommented:
could you please explain it a little bit?
do you wann create linked lable of a treenode, which is clicked?
0
 
udirAuthor Commented:
Yes!
how can i get to the items (linked lables of a tree? (like i get to a button for example)
Thanks
0
 
vo1dCommented:
the treeview has a property for all added nodes.
yourtreeview.Nodes;
thats a treenodecollection.
if you wanna get the selected node, use the yourtreeview.SelectedNode property.
if a node is selected, you will get a TreeNode, otherwise you will get null.


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
udirAuthor Commented:
Hi vo1d,
so if i have a treeview called 'treeview1'
which has 2 lables : parent called 'first' and a chiled called 'second'
and i want 'first' will open 'mainForm' and 'second will open 'lastform'
i write (for lets say the first Node : treeview1.selectedNode...........???? (need help here)


(Another little question: can i make the treeview be shown from right to left?
because when i choose this property nothing happaned!)
Thanks
Udir
0
 
vo1dCommented:
what is the name of your mainform?
mainForm?what's the name of the form which has the treeview control?
0
 
udirAuthor Commented:
The name of Main form is : 'frmMainForm', the treeview is on the main form.
The treeview has parent label called : 'workers' and a child called : 'workersSalary'
the parent  labele - 'workers' opens a form called - 'Workers' and the child opens a form called - 'workersSalary'

0
 
vo1dCommented:
ok, then you should create your form instances in the constructor of frmMainForm after the
InitializeComponent() call, put the formreferences in your nodes tag object and attach a beforeselect eventhandler for your treeview, so that instances can be called to show the forms.
it should look similar to this:

mainForm yourMainForm;
lastForm  yourLastForm;

public frmMainForm()
{
  InitializeComponent()
  this.yourmainform = new mainForm();
  this.yourlastForm  = new lastForm();
  this.treeview1.Nodes[0].Tag = this.yourMainForm;                                                              //thats your workers node
  this.treeView1.Nodes[0].Nodes[0].Tag = this.yourLastForm;                                                //thats your workerssalary childe node
  this.treeView1.BeforeSelect += new TreeViewCancelEventHandler(treeView1_BeforeSelect); //this method is called if a node
                                                                                                                                        //is going to be selected
}

the eventhandler for your tree should be as this:
void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
  if(e.Node.Tag != null && e.Node.Tag is Form)
  {
    (e.Node.Tag as Form).Show();                                                                                      //show the specific form for the node.
  }
}

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();
}
0
 
udirAuthor Commented:
Hi vo1d,
Thanks for the reply.

I get a massage :
C:\Mashavit\\UserScreens\frmMainForm.cs(40): 'frmMainForm': member names cannot be the same as their enclosing type

frmMainForm    frmMainForm;
                              ^
                               |
this is where the error point to.
(in your code is : mainForm yourMainForm;)

What this lines do?

mainForm yourMainForm;
lastForm  yourLastForm;

anyway , don't i have to put  each line in it's costractor?
like: this.treeview1.Nodes[0].Tag = this.yourMainForm;
isn't it should be in the TreeView constractor?


Thanks
Udir


0
 
vo1dCommented:
oh i think i confused you with the form names.
this were only placeholder for your fieldnames.

the 2 lines,
_____
mainForm yourMainForm;
lastForm  yourLastForm;
_____
should be your forms declarations, which shall be shown.

am i right, that you have:
- 1 form called 'frmMainForm' with the treeview on it
- 1 form called mainForm, which shall be shown if the TreeNode 'workers' is clicked
- 1 form called lastForm, which shall be shown if the TreeNode ''workersSalary'' is clicked. this node is child of 'workers'

maybe you can post your frmMainForm constructor?
0
 
udirAuthor Commented:
frmMainForm has the treeview on it, and there isn't another main form.
the treeView on frmMainForm looks like that:

         --Workers
            |
            ---WorkersSalery
 

click on Workers will open form called 'workers'
and click on WorkersSalery will open 'WorkersSalery'
(if you need the constractor i can sand it to you, but i think that example explain it).
Thanks
Udir
0
 
gbzhhuCommented:
It is only confusion with form names.  Change vo1ds code (only the constructor code) like this

Workers workers;
WorkersSalery workersSalery;

public frmMainForm()
{
  InitializeComponent()
  this.workers= new Workers();
  this.workersSalery = new WorkersSalery ();
  this.treeview1.Nodes[0].Tag = this.workers;                                                              //thats your workers node
  this.treeView1.Nodes[0].Nodes[0].Tag = this.workersSalery;                                                //thats your workerssalary childe node
  this.treeView1.BeforeSelect += new TreeViewCancelEventHandler(treeView1_BeforeSelect); //this method is called if a node
                                                                                                                                        //is going to be selected
}
0
 
udirAuthor Commented:
OK, i works but somthing strange happaned:
if i put this lines her :

public frmMainForm()
{
  InitializeComponent()
  this.yourmainform = new mainForm();
  this.yourlastForm  = new lastForm();
  this.treeview1.Nodes[0].Tag = this.yourMainForm;                                                              //thats your workers node
  this.treeView1.Nodes[0].Nodes[0].Tag = this.yourLastForm;                                                //thats your workerssalary childe node
  this.treeView1.BeforeSelect += new TreeViewCancelEventHandler(treeView1_BeforeSelect); //this method is called if a node
                                                                                                                                        //is going to be selected
}


then i get a massage " out of memory" and the application dosn't run at all.
if i put the lines in :       private void treeView1_Click(object sender, System.EventArgs e)
            {
               this.yourmainform = new mainForm();
                                    this.yourlastForm  = new lastForm();
    this.treeview1.Nodes[0].Tag = this.yourMainForm;                                                              //thats your workers node
  this.treeView1.Nodes[0].Nodes[0].Tag = this.yourLastForm;                                                //thats your workerssalary childe node
  this.treeView1.BeforeSelect += new TreeViewCancelEventHandler(treeView1_BeforeSelect);
            }

then after i click on any label (even the +) the frmMainForm open again and only
after i click another time the form "Workers" or WorkersSalery" opened.
sorry for that, but i'm new in c#.
any idea?
Udir


0
 
vo1dCommented:
you dont have to use the clickevent of the treeview. use the trees beforeselect event and put the following lines in the method:

if(e.Node.Tag != null && e.Node.Tag is Form)
{
    (e.Node.Tag as Form).Show();
}

the code, gbzhhu posted, should be named correctly with your formnames.use that code in your frmMainForm constructor.

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

private void FormClosingEventHandler(object sender, FormClosingEventArgs e)
{
  e.Cancel = true;
  (sender as Form).Hide();
}
0
 
udirAuthor Commented:
but if i put it in the constractor (after the line : InitializeComponent()),
 i get an error massage of 'out of memory'!! (and i have a lot of memory)
and if i put this lines somewhere else it works but then the same window(frmMainForm)
opened again and only after i click again, the form 'Workers' opened (behined!!!! main form,
so i have to close mainForm in order to see 'WorkersForm'
(i hope i explained myself)
0
 
vo1dCommented:
and if you put that code in the frmMainForm Load event, does the out of memory message still appear?
do you want to make an mdi application?
shall the workers form and the workerssalery form appear in the frmMainForm?
0
 
udirAuthor Commented:
if you put that code in the frmMainForm Load event, i get the same strange thing:
if i click on the 'Workers' label the form 'frmMainForm' opened again and again and
and also if i click 'WorkersSalary' frmMainForm opened again until the wanted form
('Workers' and 'WorkersSalary' opened but BEHIND frmMainForm).
(what is mdi application?)
i just want simply (like i use buttons) to use TreeView Labels
Udir
0
 
vo1dCommented:
could you post your code please?
0
 
udirAuthor Commented:
ooooops sorry!!!!!
i wrote somthing wrong in the code.
it works great!!!
sorry again.
Thanks
Udir
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now