/// <summary> /// Generates label objects based on the values returned from the Fetchtasks() values stored in labelNames /// </summary> public void GenerateLabelsAndButtons() { foreach (var text in labelNames) { Label label = new Label(); label.Text = text; label.Size = new Size(60, 20); label.Location = new Point(labelStartX, labelStartY); label.BackColor = Color.Transparent; label.AutoSize = true; label.Font = new Font("Segoe UI", 20); label.MaximumSize = new Size(260, 0); label.Click += DeleteClicked; Button delete = new Button(); delete.Size = new Size(32, 32); delete.Location = new Point(labelStartX + 260, labelStartY + 5); delete.Name = text; delete.Image = Image.FromFile("trash.png"); delete.Click += DeleteClicked; labelStartY += 50; // Add the generated item to the actual label collection MyLabels.Add(label); MyDelete.Add(delete); form.Controls.Add(label); form.Controls.Add(delete); } labelStartY = 90; }
//delegate:委托是一种数据类型,像类一样。(可以声明委托类型变量),方法参数可以是int,string、类类型。 void TestDelegate() { //2.声明委托变量md,并且new了一个委托对象并且把方法m1传递进去。那md委托保存了M1方法。 MyDelete md = new MyDelete(M1); //3。调用md委托的时候,相当于调用了m1方法。 md(); Console.WriteLine("OK"); }
/// <summary> /// Cleares all stored info about the tasks so that it can be retrieved again if a change has been made /// </summary> public void UpdateLabelsAndButtons() { foreach (var label in MyLabels) { form.Controls.Remove(label); } foreach (var button in MyDelete) { form.Controls.Remove(button); } labelNames.Clear(); MyLabels.Clear(); MyDelete.Clear(); }
//删除文件/文件夹 private void delete_Click(object sender, EventArgs e) { //SelectedItems.Count代表listView中选中的数目 //删除treeView中的文件 MyDelete d = new MyDelete(); if (listView1.SelectedItems.Count == 0) { string treeViewSeletedFolderPath = treeView1.SelectedNode.Tag.ToString(); if (treeViewSeletedFolderPath != null) { if (d.DeleteFolder(treeViewSeletedFolderPath)) { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } } //删除listView中的文件 else { int deleteFiles = 0; int deleteFolders = 0; bool tag = true; ListView.SelectedIndexCollection indexs = listView1.SelectedIndices; DirectoryInfo currentFolder = new DirectoryInfo(treeView1.SelectedNode.Tag.ToString()); //获取本文件夹下的所有子文件夹 DirectoryInfo[] subFolders = currentFolder.GetDirectories(); int folderNum = subFolders.Length; //Console.WriteLine("folderNum:"+folderNum); //获取本文件夹下的所有文件 FileInfo[] files = currentFolder.GetFiles(); foreach (int index in indexs) { //remove时文件夹中的文件减少i个,剩余文件的index也需要减少i listView1.Items.RemoveAt(index - deleteFiles - deleteFolders); //Console.WriteLine(index); //删除文件 if (index >= folderNum) { //文件的路径 string filePath = treeView1.SelectedNode.Tag.ToString() + "/" + files[index - folderNum].Name; //若存在文件夹删除失败,则break if (!d.DeleteFile(filePath)) { tag = false; break; } deleteFiles++; } //删除文件夹 else { //文件夹的路径 string folderPath = treeView1.SelectedNode.Tag.ToString() + "/" + subFolders[index].Name; //若存在文件夹删除失败,则break if (!d.DeleteFolder(folderPath)) { tag = false; break; } deleteFolders++; } }//end foreach if (tag) { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } TreeNode i = treeView1.SelectedNode; treeView1.SelectedNode.Parent.Collapse(); treeView1.SelectedNode.Expand(); treeView1.SelectedNode = i; i.Expand(); }