treeView控件应用-IE地址栏下拉列表清理实例

 新建一个项目,在窗体上添加一个treeView控件,名称默认treeView1,checkBoxes属性设为True,添加两个按钮button1和button2,text属性分别改成"清理选中项"和"刷新",再添加一个checkBox1.

窗体代码如下:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  Microsoft.Win32;
using  System.Diagnostics;

namespace  URL_Address_Clean
{
    
public partial class Form1 : Form
    
{
        
public string[] ValueName=new string[100];  //定义一个一维数组,用来存储值的名称,因为在删除地址时要用到

        
public Form1()
        
{
            InitializeComponent();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            GetURL();
        }


        
/// <summary>
        
/// 从注册表中取得IE地址栏下拉列表中的地址
        
/// </summary>

        private void GetURL()
        
{
            RegistryKey myKey 
= Registry.CurrentUser.CreateSubKey("Software/Microsoft/Internet Explorer/TypedURLs");
            
string[] Values = myKey.GetValueNames();
            
object ValueData;

            treeView1.Nodes.Clear();    
//每次操作之前都先清空treeView1中的所有节点
            TreeNode pNode;
            pNode 
= treeView1.Nodes.Add("IE地址栏下拉列表");    //添加根节点

            
for (int i = 0; i < Values.Length; i++)
            
{
                ValueName[i] 
= Values[i];   //保存值的名称,删除的时候需要
                ValueData = myKey.GetValue(ValueName[i]);
                pNode.Nodes.Add(ValueData.ToString());  
//添加子节点
            }


            myKey.Close();
            treeView1.ExpandAll();
        }


        
/// <summary>
        
/// 删除选中的地址
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)
        
{
            RegistryKey myKey 
= Registry.CurrentUser.CreateSubKey("Software/Microsoft/Internet Explorer/TypedURLs");

            
for (int i = 0; i < treeView1.GetNodeCount(true)-1; i++)
            
{
                
if (treeView1.Nodes[0].Nodes[i].Checked)
                
{
                    myKey.DeleteValue(ValueName[i],
false);  //ValueName[i]即是地址treeView1.Nodes[0].Nodes[i].Text在注册表中对应的值的名称
                }

            }


            myKey.Close();
            GetURL();
        }


        
/// <summary>
        
/// 调用GetURL()函数来刷新
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)
        
{
            GetURL();
        }


        
/// <summary>
        
/// 选中根节点时,将其子节点全选中,否则全取消
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        
{
            
if (e.Node.Level==0//如果是根节点
            {
                TreeNode cNode;
                
if (e.Node.Checked)
                
{
                    cNode 
= e.Node.FirstNode;
                    
for (int i = 1; i <= e.Node.GetNodeCount(true); i++)
                    
{
                        cNode.Checked 
= true;
                        cNode 
= cNode.NextNode;
                    }

                }

                
else
                
{
                    cNode 
= e.Node.FirstNode;
                    
for (int i = 1; i <= e.Node.GetNodeCount(true); i++)
                    
{
                        cNode.Checked 
= false;
                        cNode 
= cNode.NextNode;
                    }

                }

            }

        }


        
/// <summary>
        
/// 节点双击的时候在IE中打开相应的地址
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        
{
            
if (e.Node.Level == 0return;  //如果是根节点,返回
            if (checkBox1.Checked)
            
{
                ProcessStartInfo StartInfo 
= new ProcessStartInfo("IExplore.exe");
                StartInfo.WindowStyle 
= ProcessWindowStyle.Maximized;
                StartInfo.Arguments 
= e.Node.Text;
                Process.Start(StartInfo);
            }

        }

    }

}

你可能感兴趣的:(String,object,IE,System,Class,button)