sábado, 12 de noviembre de 2011

TREEVIEW C#

 public partial class Form1 : Form
    {
        private Interes listaInteres;

        internal Interes ListaInteres

        {
            get { return listaInteres; }
            set { listaInteres = value; }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)

        {
            this.listaInteres = new Interes();
            this.listaInteres.Id = 1;
            this.listaInteres.Nombre = "Interes";
            
            llenarArbol();
            
        }
        private void llenarArbol(){
            treeInteres.Nodes.Clear();
            TreeNode nodo = new TreeNode();
            nodo.Text = this.listaInteres.Nombre;
            nodo.Tag = this.listaInteres;
            treeInteres.Nodes.Add(nodo);
            imprimirRamas(this.listaInteres.Intereses, treeInteres.Nodes[0]);
            
           
        }
        private void imprimirRamas(List<Interes> intereses, TreeNode nodoPadre)
        {
            int i = 0;
            foreach (Interes interes in intereses)
            {
                TreeNode nodoHijo = new TreeNode();
                nodoHijo.Text = interes.Nombre;
                nodoHijo.Tag = interes;
                nodoPadre.Nodes.Add(nodoHijo);

                if (interes.Intereses.Count > 0)

                {
                    imprimirRamas(interes.Intereses, nodoPadre.Nodes[i]);
                }
                i++;
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)

        {
            Interes interes = new Interes();
            interes.Id = Convert.ToInt16(txtId.Text);
            interes.Nombre = txtNombre.Text;
            
            /*
            TreeNode nuevoNodo = new TreeNode();
            nuevoNodo.Text = interes.Nombre;
            nuevoNodo.Tag = interes;
            treeInteres.SelectedNode.Nodes.Add(nuevoNodo);
            */
            
            Interes interesPadre = (Interes) treeInteres.SelectedNode.Tag;
            if (this.listaInteres.Id == interesPadre.Id)
                listaInteres.Intereses.Add(interes);
            else
                buscarPadre(listaInteres.Intereses, interesPadre, interes);
            llenarArbol();
             

        }


        private void btnImprimir_Click(object sender, EventArgs e)

        {

            Interes interesPrincipal = this.listaInteres;

            System.Console.WriteLine(" (Negocio) Principal " + interesPrincipal.Id + " " + interesPrincipal.Nombre + " Num Nodos: " + interesPrincipal.Intereses.Count);
            
            interesPrincipal = (Interes) this.treeInteres.Nodes[0].Tag;
            System.Console.WriteLine(" (Vista) Principal "+ interesPrincipal.Id+ " "+interesPrincipal.Nombre+" Num Nodos: " + treeInteres.Nodes[0].Nodes.Count);
            
            imprimirRamas(interesPrincipal.Intereses);
        }

        private void imprimirRamas(List<Interes> intereses)

        {
           
            foreach (Interes interes in intereses)
            {
                System.Console.WriteLine(interes.Id+" - "+interes.Nombre);
                if (interes.Intereses.Count > 0)
                {
                    imprimirRamas(interes.Intereses);
                }
                
            }
        }
        private void buscarPadre(List<Interes> intereses, Interes interesPadre, Interes interesHijo)
        {
            foreach(Interes interes in intereses){
                if (interes.Id == interesPadre.Id)
                {
                    interes.Intereses.Add(interesHijo);
                    break;
                }
                if (interes.Intereses.Count>0)
                {
                    buscarPadre(interes.Intereses, interesPadre, interesHijo);
                }
            }
        }
    }
}

No hay comentarios:

Publicar un comentario