现在的位置: 首页 > 综合 > 正文

c# winform 应用编程代码总结 15

2012年11月02日 ⁄ 综合 ⁄ 共 4140字 ⁄ 字号 评论关闭

53、实现拖放操作

        this.richTextBox1.AllowDrop = true;
        private void richTextBox1_DragDrop(object sender,System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {    e.Effect=DragDropEffects.Copy;}
            else
            {e.Effect = DragDropEffects.None;}
        }    
        private void richTextBox1_DragEnter(object sender,System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {    e.Effect=DragDropEffects.Copy;}
            else
            {e.Effect = DragDropEffects.None;}
        }

54、图形文件的拖放操作

        this.AllowDrop = true;

        private void Form1_DragEnter(object sender,  System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
            String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);;
            string  tempstr;
            Bitmap bkImage;
            tempstr = str_Drop[0];
            try
            {
                bkImage = new Bitmap(tempstr);
                this.Size = new System.Drawing.Size(bkImage.Width,
                    bkImage.Height);
                this.BackgroundImage = bkImage;}
            catch {}
        }

55、分类枚举指定计算机的服务

       private void button1_Click(object sender, System.EventArgs e)
        {
            string TempMachineName;
            TempMachineName =this.textBox1.Text;
            if (TempMachineName == "")
            {
                TempMachineName = System.Environment.MachineName;
                //如果textBox1中为空,采用本地计算机   
                this.textBox1.Focus();
                this.textBox1.Text = TempMachineName;
            }

            this.listBox1.Items.Clear();
            this.listBox2.Items.Clear();
            this.listBox3.Items.Clear();
            
            ServiceController[] ArraySrvCtrl
            ArraySrvCtrl = ServiceController.GetServices(TempMachineName);

            try
            {
                //在ArraySrvCtrl数组中存储所有服务
                foreach (ServiceController tempSC in ArraySrvCtrl)
                {
                    if (tempSC.Status == ServiceControllerStatus.Running)
                    {
                        listBox1.Items.Add(tempSC.DisplayName);
                        //将正在运行的服务添加到listBox1中
                    }
                    else if (tempSC.Status == ServiceControllerStatus.Paused)
                    {      
                        listBox3.Items.Add(tempSC.DisplayName);
                        //'将暂停的服务添加到listBox3中
                    }
                    else
                    {
                        listBox2.Items.Add(tempSC.DisplayName);
                        //'将停止的服务添加到listBox2中
                    }
                }
            }
            catch {}
        }

        image

56、响应文件系统事件

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            this.label1.Text = "文件: "+ e.FullPath.ToString()+ "已添加到你的目录中。";
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            this.label2.Text=e.OldName.ToString() +"已经被重命名为:" +e.Name.ToString();  
        }

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            MessageBox.Show( e.ChangeType.ToString() + " changed");
        }

        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            MessageBox.Show(e.Name + " Delete");
        }

        image

57、XML文件编辑器

        string filePath;
        DataSet dsAuthors=new DataSet("authors"); 

        private void button1_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "XML文件(*.XML)|*.xml";
            dsAuthors.Clear();
            if (dlg.ShowDialog()==DialogResult.OK & dlg.FileName!="")
            { //将XML文件的完整路径保存到filePath变量中
                filePath = dlg.FileName;
                try
                {
                    dsAuthors.ReadXml(filePath);
                    dataGrid1.DataSource = dsAuthors;}
                catch {}
            }
        }
        private void button2_Click(object sender, System.EventArgs e)
        {
                    dsAuthors.WriteXml(filePath);
        }

抱歉!评论已关闭.