关于地址添加 子窗体向父窗体传值 树的使用详细
namespace ItcastSIMS
{
public partial class FrmArea : Form
{
public FrmArea()
{
InitializeComponent();
}
AreaBLL bll = new AreaBLL();
List<Areas> list;//数据库地址信息
private void FrmArea_Load(object sender, EventArgs e)
{
list = bll.GetAllAreas();
tvAreas.Nodes.Clear();//默认节点清空
CreateParent();
}
private void CreateParent()
{
TreeNode parent = new TreeNode();
parent.Text = "全国";
parent.Tag = 0;
tvAreas.Nodes.Add(parent);//添加父节点
//添加子节点
CreateChild(parent);
//展开
//tvAreas.ExpandAll();
parent.Expand();
}
private void CreateChild(TreeNode parent)
{
//父节点的id
int pid = Convert.ToInt32(parent.Tag);
foreach (Areas a in list)
{
if (a.APid == pid)
{
TreeNode tn = new TreeNode();
tn.Text = a.AName;
tn.Tag = a.AID;
parent.Nodes.Add(tn);
//递归调用,加载子节点
CreateChild(tn);
}
}
}
private void btnSure_Click(object sender, EventArgs e)
{
//获取选中的节点
GetTNText(tvAreas.SelectedNode);
txt += tvAreas.SelectedNode.Text;
DialogResult = System.Windows.Forms.DialogResult.OK;
}
public string txt = "";
/// <summary>
/// 获取选中节点的父节点的内容
/// </summary>
/// <param name="tn"></param>
/// <returns></returns>
void GetTNText(TreeNode tn)
{
if (tn.Parent != null)
{
GetTNText(tn.Parent);
txt += tn.Parent.Text + "-";
}
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
//选择地区
private void btnChooseArea_Click(object sender, EventArgs e)
{
FrmArea frm = new FrmArea();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtArea.Text = frm.txt;
}
}