使用TreeView+ListBox+TxtBox 资料管理器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 资料管理器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //加载类别到TreeView
            LoadCategoryToTree(treeView1.Nodes, GetCategoriesByParentId(-1));

            //设置TreeView中节点单击的时候显示右键菜单

            AddMouseClickShowContentMenu();
        }

        private void AddMouseClickShowContentMenu()
        {
            //遍历TreeView的每个节点
            SearchNode(treeView1.Nodes);
        }

        private void SearchNode(TreeNodeCollection treeNodeCollection)
        {
            foreach (TreeNode item in treeNodeCollection)
            {
                if (item.Level == 1)
                {
                    item.ContextMenuStrip = contextMenuStrip1;
                }
                SearchNode(item.Nodes);
            }
        }

        //递归加载类别信息到TreeView
        private void LoadCategoryToTree(TreeNodeCollection treeNodeCollection, List<Category> list)
        {
            foreach (Category item in list)
            {
                //把当前节点加到treeNodeCollection集合中
                TreeNode tnode = treeNodeCollection.Add(item.TName);

                //把当前类别的Id记录到Tag中。
                tnode.Tag = item.TId;
                //List<Category> listSub = GetCategoriesByParentId(item.TId);
                //if (listSub.Count == 0)
                //{
                //    tnode.ContextMenuStrip = contextMenuStrip1;
                //}
                LoadCategoryToTree(tnode.Nodes, GetCategoriesByParentId(item.TId));
            }
        }

        private List<Category> GetCategoriesByParentId(int pid)
        {
            List<Category> list = new List<Category>();
            string sql = "select tid,tname from Category where [email protected]";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@pid", pid)))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Category model = new Category();
                        model.TId = reader.GetInt32(0);
                        model.TName = reader.GetString(1);
                        list.Add(model);
                    }
                }
            }
            return list;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                LoadTitleToListBox(e.Node);

            }
        }

        private void LoadTitleToListBox(TreeNode node)
        {

            //获取当前节点对应的类别id
            int categoryId = (int)node.Tag;
            //从文章表中查询dtid为categoryId的所有文章
            List<ContentInfo> list = GetContentByCategoryId(categoryId);
            listBox1.Items.Clear();
            //遍历list集合将文章信息加到Listbox中
            foreach (var item in list)
            {
                listBox1.Items.Add(item);
            }
        }

        private List<ContentInfo> GetContentByCategoryId(int categoryId)
        {
            List<ContentInfo> list = new List<ContentInfo>();
            //select did,dname from ContentInfo where [email protected]
            using (SqlDataReader reader = SqlHelper.ExecuteReader("select did,dname from ContentInfo where [email protected]", new SqlParameter("@categoryId", categoryId)))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ContentInfo model = new ContentInfo();
                        model.DId = reader.GetInt32(0);
                        model.DName = reader.GetString(1);
                        list.Add(model);
                    }
                }
            }

            return list;

        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //ListBox的鼠标双击事件
            // e.
            if (listBox1.SelectedItem != null)
            {
                //获取选中项中的文章的Id
                ContentInfo article = listBox1.SelectedItem as ContentInfo;
                int id = article.DId;
                textBox1.Text = GetContentInfoByContentId(id);
            }
        }

        //根据文章Id获取文章内容
        private string GetContentInfoByContentId(int id)
        {
            //string sql = "select dcontent from ContentInfo where [email protected]";
            //using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@id", id)))
            //{
            //    if (reader.HasRows)
            //    {
            //        if (reader.Read())
            //        {
            //            return reader.GetString(0);
            //        }
            //    }
            //}
            //return string.Empty;
            string sql = "select dcontent from ContentInfo where [email protected]";
            return SqlHelper.ExecuteScalar(sql, new SqlParameter("@id", id)).ToString();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeView1.SelectedNode = e.Node;
        }

        private void 导入文章ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                //获取当前选中类别的categoryId
                int categoryId = (int)treeView1.SelectedNode.Tag;
                openFileDialog1.Multiselect = false;
                openFileDialog1.Filter = "txt files (*.txt)|*.txt";
                openFileDialog1.FileName = string.Empty;
                //导入文章
                //1.弹出一个选择路径的一个对话框
                DialogResult result = openFileDialog1.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    //获取用户选择的文件的路径
                    string path = openFileDialog1.FileName;
                    string title = Path.GetFileNameWithoutExtension(path);
                    string content = File.ReadAllText(path, System.Text.Encoding.Default);
                    //执行insert语句将该文章导入到数据库中
                    string sql = "insert into ContentInfo(dtid,dname,dcontent) values(@categoryId,@title,@content)";
                    SqlParameter[] pms = new SqlParameter[] {
                    new SqlParameter("@categoryId",categoryId),
                    new SqlParameter("@title",title),
                    new SqlParameter("@content",content)
                    };
                    //直接执行插入语句
                    SqlHelper.ExecuteNonQuery(sql, pms);
                }

                //重新加载ListBox
                LoadTitleToListBox(treeView1.SelectedNode);
            }

        }
    }
}
时间: 2024-08-15 15:48:44

使用TreeView+ListBox+TxtBox 资料管理器的相关文章

第20课-数据库开发及ado.net 可空值类型,资料管理器,多条件查询,Case

第20课-数据库开发及ado.net 可空值类型,资料管理器,多条件查询,Case SqlHelper using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Text; namespace _02省市联动 { public static  class SqlHelper { //

C#WinForm treeview 简单文件夹管理器 查看文件夹下的文件,子文件下的文件

1 查看的文件夹中的内容 2 UI 3 代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 usi

mount挂载,dd 工具,配置配额系统,RAID阵列,逻辑卷管理器LVM

mount mount挂载 vim /etc/fstab 将常用的挂载的设备写入系统表中 文件挂载配置文件 /etc/fstab /etc/fstab每行定义一个要挂载的文件系统: 要挂载的设备或伪文件系统 挂载点 文件系统类型 挂载选项 转储频率 自检次序 要挂载的设备或伪文件系统: 设备文件.LABEL(LABEL="").UUID(UUID="").伪文件系统名称(proc, sysfs) 挂载选项:defaults 转储频率:0:不做备份 每天转储 每隔一天

MacOS 如何使用软件包管理器 Homebrew-Cask 安装软件?

本文标签: Mac效率工具 Mac小工具 MacOS Homebrew-Cask Mac软件包管理器 软件包管理简介 苹果系统结合了Windows的图形界面以及Linux的命令行,现在开发人员都清一色的用Mac了. Linux系统下安装.更新软件都很方便,除了我感觉用CentOS系统的一些人还喜欢 老三部曲 :下载.编译.安装.大部分人都更习惯基于包管理系统安装软件,当然更新.移除软件也用包管理系统搞定了. Mac系统的包管理不像Linux是由操作系统自带的,Mac官方默认没有提供这样的包管理系

记一次AD域域管理员密码更改导致某系统群集管理器故障排查解决过程

环境描述 某公司有一套系统,2台Windows2003系统,采用自带的群集管理器功能实现双机热备功能,正常情况下,服务器10.1.1.1承载中间件应用服务,服务器10.1.1.2承载数据库服务,仅当其中一台服务器故障时才会把资源切换至另一台. 系统名称 系统版本 IP地址 备注 S-EIP-APP Windows2003ENTSP2 10.1.1.1 采用Windows2003自带的群集管理器实现双机热备功能 S-EIP-DATA Windows2003ENTSP2 10.1.1.2 故障表现

java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配

java数据库连接(JDBC-ODBC方式) 环境:win7 64位和NetBeans IDE 7.1.2  jdk1.7.0_17(64位) 写好程序后运行报错:java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配. 注意:win7 64位配置数据源,控制面板->管理工具打开数据源(ODBC)会找不到驱动程序,因为是64位操作系统,但是安装的是32位的office.需要在路 径"C:

步步为营-16-资料管理器

说明:涉及到知识点:TreeView控件,递归调用, 1 先把架子搭起来 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 Syst

[C++]内存管理器--谈论如何自定义内存分配机制

内存管理器–谈论如何自定义内存分配机制 Memory pools, also called fixed-size blocks allocation, is the use of pools for memory management that allows dynamic memory allocation comparable to malloc or C++'s operator new. As those implementations suffer from fragmentation

在“DNS管理器”中手工增加DNS主机(A)或者别名(CNAME)记录时,出现被拒绝的错误

问题现象: AD域控制器操作系统为Win2008R2,在"DNS管理器"中手工增加DNS主机(A)或者别名(CNAME)记录时,出现被拒绝的错误.但是将客户端加入域后,在"DNS管理器"中能看到对应的客户端的DNS主机(A)记录. 在系统日志中,能查看到ID为4015的错误日志: 事件类型:   错误 事件来源:   DNS 事件种类:   无 事件 ID:    4015 日期:       2016/4/1 事件:       1:06:53 用户: