Created
July 8, 2015 20:34
-
-
Save Fhernd/f2b737102f284bd6194d to your computer and use it in GitHub Desktop.
Formulario Windows con control TreeView.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OrtizOL - xCSw - http://ortizol.blogspot.com | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace R05_06 | |
{ | |
public partial class Main : Form | |
{ | |
#region Constructores | |
/// <summary> | |
/// Creación de la ventana principal de la aplicación. | |
/// </summary> | |
public Main() | |
{ | |
InitializeComponent(); | |
} | |
#endregion | |
#region Métodos | |
/// <summary> | |
/// Carga los nodos (directorios) de un elemento (directorio). | |
/// </summary> | |
/// <param name="nodoDirectorio">Nodo a cargar elementos.</param> | |
private void CargarNodo(TreeNode nodoDirectorio) | |
{ | |
// Creación de instancia de DirectoryInfo para el directorio a explorar: | |
DirectoryInfo directorio = new DirectoryInfo(nodoDirectorio.FullPath); | |
foreach (DirectoryInfo dir in directorio.GetDirectories()) | |
{ | |
TreeNode nuevoNodo = new TreeNode(dir.Name); | |
nodoDirectorio.Nodes.Add(nuevoNodo); | |
nuevoNodo.Nodes.Add("*"); | |
} | |
} | |
#endregion | |
#region Eventos | |
/// <summary> | |
/// Evento que ocurre al cargar el formulario. | |
/// </summary> | |
/// <param name="sender">Objeto generador del evento.</param> | |
/// <param name="e">Datos del evento</param> | |
private void Main_Load(object sender, EventArgs e) | |
{ | |
// Primer nodo del árbol: | |
TreeNode raiz = new TreeNode(@"C:\Windows"); | |
// Adición del nodo al objeto `TreeView`: | |
tvwTreeDirectory.Nodes.Add(raiz); | |
// Carga del primer nodo, además de su expansión: | |
CargarNodo(raiz); | |
tvwTreeDirectory.Nodes[0].Expand(); | |
} | |
/// <summary> | |
/// Activa la carga de los elementos del nodo expandido. | |
/// </summary> | |
/// <param name="sender">Objeto generador del evento.</param> | |
/// <param name="e">Datos del evento</param> | |
private void tvwTreeDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e) | |
{ | |
// Para un nodo que ha sido marcado para posterior visualización, | |
// remover la marca y leer el contenido real de ese nodo (directorio): | |
if (e.Node.Nodes[0].Text == "*") | |
{ | |
e.Node.Nodes.Clear(); | |
CargarNodo(e.Node); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment