C# 动态创建出来的窗体间的通讯 delegate3
附件1:http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate03.rar

一个RTF文件管理器
描述
Form2,Form3,Form4都是独立存在的,同时完成独立的功能,相互不依赖。
最后把Form2,Form3,Form4集合放在FORM1上,实际上需要这3个的窗体Form2,Form3,Form4间相互得到改变后的变量值。
FORM2 文件夹列表
在FORM1中设置一初始文件夹路径
要实现的功能:当单击本窗体中的TreeView.node时,要求Form3.ListView的文件列表同时更新。
FORM3 文件列表
需要得到Form2当前选中的文件夹路径
要实现的功能:当本窗体中的ListView选中一个节点时时,同时Form4.RichtextBox 打开并读取这个文件。
FORM4是一个RFT编辑器
需要得到FORM3 文件列表中选中的文件完整路径
要实现的功能:当单击本窗体中的保存按钮,要保存Form3.ListView中选中的那个文件。
实现步骤
1 创建4个空窗体分别为:Form1.cs,Form2.cs,Form3.cs,Form4.cs
Form1:3个Panel

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.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); } Form2 f2 = new Form2();
Form3 f3 = new Form3();
Form4 f4 = new Form4(); private void Form1_Load(object sender, EventArgs e)
{
f2.Dock = DockStyle.Fill;
f2.TopLevel = false;
f2.FormBorderStyle = FormBorderStyle.FixedToolWindow;
f2.Parent = panel1;
f2.Show(); f3.Dock = DockStyle.Fill;
f3.TopLevel = false;
f3.FormBorderStyle = FormBorderStyle.FixedToolWindow;
panel2.Controls.Add(f3);
f3.Show(); f4.Dock = DockStyle.Fill;
f4.TopLevel = false;
f4.FormBorderStyle = FormBorderStyle.FixedToolWindow;
panel3.Controls.Add(f4);
f4.Show(); f2.CallBackPro += new Form2.CallBack(GetForm2TreeViewNodePath);
f3.CallBackPro += new Form3.CallBack(GetForm3ListViewFileFullPath);
} private void GetForm2TreeViewNodePath(string path)
{
f3.DirPath = path;
f3.UpdateFileListView();
} private void GetForm3ListViewFileFullPath(string s)
{
f4.FileName =s;
f4.LoadFile();
}
}
}
Form2:treeView1,textBox1

代码:
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.IO; namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
#region 添加目录 //目录 D:\Administrator\Documents\TestData
//返回 D:\Administrator\Documents
public string GetDirectoryParentPath(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
return d.Parent.FullName;
} /// <summary>
/// 把一个文件夹的所有文件夹添加 到TREEVIEW中
/// DirectoryToTreeNode(@"D:\Administrator\Documents\TestData", treeViewCtrl1);
/// </summary>
/// <param name="dirPath"></param>
/// <param name="treeView"></param>
public void DirectoryToTreeNode(string dirPath, TreeView treeView)
{
treeView.Nodes.Clear();
DirectoryInfo di = new DirectoryInfo(dirPath);
TreeNode rootNode = new TreeNode(di.Name);
GetDirs(di.GetDirectories(), rootNode);
treeView.Nodes.Add(rootNode);
rootNode.Expand();
} public void GetDirs(DirectoryInfo[] subDirs, TreeNode treeNode)
{
foreach (DirectoryInfo d in subDirs)
{
TreeNode node = new TreeNode(d.Name, , );
DirectoryInfo[] subSubDirs = d.GetDirectories();
GetDirs(subSubDirs, node);
treeNode.Nodes.Add(node);
}
}
#endregion public delegate void CallBack(string path);
public event CallBack CallBackPro; //public delegate string s ="s";
public string DirPath = "TestData";// @"D:\Administrator\Documents\TestData";
private string RootDir = ""; //返回 D:\Administrator\Documents
public string CurrentPath = "";//当前选中的文件夹
public string FileExtend = "";//.rtf
public ListView listView1 = null; private void Form2_Load(object sender, EventArgs e)
{ RootDir = GetDirectoryParentPath(DirPath);
DirectoryToTreeNode(DirPath, treeView1); CurrentPath = RootDir;
textBox1.Text = CurrentPath;
} private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode node = e.Node;
if (e.Button == MouseButtons.Right)
{ //右键选择节点
treeView1.SelectedNode = node;
}
CurrentPath = RootDir + "\\" + e.Node.FullPath;
textBox1.Text = CurrentPath; CallBackPro(CurrentPath);
//UpdateListViewFileList(CurrentPath);
}
}
}
Form3:listView1,textBox1

代码
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.IO;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} #region function...
public static void AddFileListToListView1(ListView listView, string path, string FileExtended)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fi = di.GetFiles("*" + FileExtended);//只添加.txt的文件 for (int i = ; i < fi.Length; i++)
{
string fileName1 = fi[i].ToString();
string fileName2 = RemoveFileExt(fileName1, FileExtended);// fileName1.Replace(FileExtended, "");
listView.Items.Add(fileName2);
//listView.Items[i].ImageIndex = 0;
}
}
#region 复制一个文件名不包括扩展名
//Text = RemoveFileExt(@"C:\Tmp\quickview_src.zip", ".zip");
//返回 C:\Tmp\quickview_src
public static string RemoveFileExt(string s, string FileExt)
{
string value = s;
string f1 = s.Substring(s.Length - , );
if (f1 == FileExt)
value = s.Substring(, s.Length - );
return value;
}
#endregion /// <summary>
/// 更新目录文件
/// </summary>
public void UpdateFileListView()
{
listView1.BeginUpdate();
listView1.Items.Clear();
AddFileListToListView1(listView1, DirPath, FileExtend);
//listViewCtrl1.SetSmallIcon(imageList1, 0);
listView1.EndUpdate();
} #endregion public delegate void CallBack(string path);
public event CallBack CallBackPro; public string DirPath ="";//= "TestData";// @"D:\Administrator\Documents\TestData";
public string CurrentFileName = "";//当前选中的文件夹
public string FileExtend = ".rtf";//.rtf private void Form3_Load(object sender, EventArgs e)
{
DirPath = System.Windows.Forms.Application.StartupPath + "\\TestData";
UpdateFileListView();
textBox1.Text = CurrentFileName;
} private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedIndices != null)
{
if (listView1.SelectedItems.Count == )
//--------------------------------"C:\abc\" 21312 ".rtf"
CurrentFileName = DirPath + "\\" + listView1.SelectedItems[].SubItems[].Text + FileExtend;
}
else
{
CurrentFileName = "";
}
textBox1.Text = CurrentFileName;
CallBackPro(CurrentFileName);
}
}
}
Form4:richTextBox1,toolStrip1,toolStrip1.按钮,textBox1

代码
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; namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
} public string FileName = ""; public void LoadFile()
{
richTextBox1.LoadFile(FileName);
} private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
try
{
richTextBox1.SaveFile(FileName);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
} } private void richTextBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = FileName;
} }
}
一个SpilterContain拖拽好结果正确的模型架 可以保存SpilterContain的位置 用的是XML
关于效果你可以自己用个splitContainer1 做3个面板 去做个拖拽窗体时大小改变和WINDOWS的EXPLORER一样的 结果为正确
附件2:http://files.cnblogs.com/xe2011/WinForm_ExplorerFrame_Read_AndSave_XML.rar

C# 动态创建出来的窗体间的通讯 delegate3的更多相关文章
- C# 动态创建出来的窗体间的通讯 delegate2
附件:http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate02.rar 窗体2 和窗体3 都是动态创建出来的 现在 FORM3.TE ...
- C# 动态创建出来的窗体间的通讯 delegate1
附件 http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate01.rar 需要每个窗体是独立存在,禁止相与引用窗体 这样干净并且可以反 ...
- 自定义事件实现不同窗体间的通讯Delphi篇
要实现子窗体与父窗体之间的通讯,有多种方法(比如:重载子窗体的构造函数,将父窗体的引用作为参数传递给子窗体).下面我要介绍的是利用自定义事件的方法,它能够最大程度的避免模块之间的耦合,充分体现面向对象 ...
- C#窗体间传值的简便方法/工具
一.问题:窗体间传值必须需要窗体之间有联系,具体有如下方式 窗体间传值涉及到窗体A必须拥有窗体B,这样才可以实现A-B之间传值 窗体A与窗体B在窗体/实例C中,A-B可互相通讯 其他方式,不细讨论,复 ...
- C# 学习笔记(一) Winform利用Assembly反射动态创建窗体
1. 添加Reflection //添加对Reflection程序集引用 using System.Reflection; // 引用窗体创建方法CreateForm,传入参数 private voi ...
- Delphi中动态创建窗体有四种方式
Delphi中动态创建窗体有四种方式,最好的方式如下: 比如在第一个窗体中调用每二个,主为第一个,第二个设为动态创建 Uses Unit2; //引用单元文件 procedure TForm1.But ...
- c#反射动态创建窗体
根据窗体的名称动态创建窗体 Assembly assembly = Assembly.GetExecutingAssembly(); // 实例化窗体 try { Form f ...
- DELPHI动态创建窗体
//第一种方式 procedure TForm1.btn1Click(Sender: TObject); begin With TForm2.Create(Application) do Try Sh ...
- winform 用户控件、 动态创建添加控件、timer控件、控件联动
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
随机推荐
- WEB架构师成长之路之三-架构师都要懂哪些知识
Web架构师究竟都要学些什么?具备哪些能力呢?先网上查查架构师的大概的定义,参见架构师修炼之道这篇文章,写的还不错,再查查公司招聘Web架构师的要求. 总结起来大概有下面几点技能要求: 一. 架构师有 ...
- POJ 3349 Snowflake Snow Snowflakes Hash
题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...
- the apply of backbone
http://www.developer.com/print/lang/jscript/creating-a-javascript-driven-online-notebook-with-backbo ...
- 用js判断操作系统和浏览器类型
判断操作系统和浏览器的js代码 navigator.userAgent:userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. navigator.pla ...
- Mono for Android 优势与劣势
原文:Mono for Android 优势与劣势 最近有兴趣了解一下Mono for Andriod,也就是使用.NET平台来开发Andriod程序.Mono for Android API 几乎映 ...
- C++中new和不new的区别
我们都知道C++中有三种创建对象的方法,如下: 复制代码代码如下: #include <iostream>using namespace std; class A{private: ...
- XSS跨站脚本攻击在Java开发中防范的方法
1. 防堵跨站漏洞,阻止攻击者利用在被攻击网站上发布跨站攻击语句不可以信任用户提交的任何内容,首先代码里对用户输入的地方和变量都需要仔细检查长度和对”<”,”>”,”;”,”’”等字符做过 ...
- 基础算法(搜索):NOIP 2015 斗地主
Description 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3& ...
- 数学之美 zt
数学是美丽的,哪里有数哪里就有美. 数学的定义是研究数量关系和空间形式的一门科学.但有句名言说:数学比科学大得多,因为它是科学的语言.数学不仅用来写科学,而且可用来写人生.所以说数学是一切学科的基础, ...
- devi into python 笔记(二)元组 变量声明 和列表解析
元组tuple: 类似list,只是tuple是不可变的list.类似java的String都是不可改变的.注意:tuple没有方法(有待考证),不可以像list那样那个list.pop 或者list ...