📄 frmselectxxqhbyatt.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using dist.hb.EnvQueryStat.Pub;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
namespace dist.hb.EnvQueryStat.Pub
{
/// <summary>
/*
*注意1:初始化时,使用SelArrayList,存储的是字符串对象,增加以前选择的行政区划字符串。
*注意2: 保存时,使用SelArrayList,存储的是MyTreeNodeInfo。
*/
/// </summary>
public partial class frmSelectXXQHbyATT : Form
{
public bool IsOK;
public frmSelectXXQHbyATT()
{
InitializeComponent();
m_OleDbConn = null;
mapControl = null;
SelArrayList = null;
ProvCode = "";
SelArrayList = new ArrayList();
IsOK = false;
}
string m_strTbl = "FS_XXQH_CODE";
DataTable m_table;
private OleDbConnection m_OleDbConn;//数据库链接
public OleDbConnection OleDbConn
{
get { return m_OleDbConn; }
set
{
if (value == null) return;
if (value == m_OleDbConn) return;
m_OleDbConn = value;
}
}
public int iLevel;//需要提取的行政单位级别,其值是从1到3
public string FirstLayer;//第一级行政区划
public string SecondLayer;//第二级行政区划
public string ThirdLayer;//第三级行政区划
public string FirstKeyField;//编码字段
public string SecondKeyField;//编码字段
public string ThirdKeyField;//编码字段
public string FirstNameField;//名称字段
public string SecondNameField;//名称字段
public string ThirdNameField;//名称字段
public string ProvCode;//某个省的编码
public ArrayList SelArrayList;//选择的行政区划列表
public ESRI.ArcGIS.Controls.IMapControl4 mapControl;
//窗体LOAD事件
private void frmSelectXXQHbyATT_Load(object sender, EventArgs e)
{
LoadData();
FillProvNode();
SelectNode();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.ByMouse) return;
SetChildCheck(e.Node);
SetParentNodeCheck(e.Node);
}
private void SetChildCheck(TreeNode nd)
{
if(nd == null) return;
foreach (TreeNode subNode in nd.Nodes)
{
subNode.Checked = nd.Checked;
SetChildCheck(subNode);
}
}
private void SetParentNodeCheck(TreeNode nd)
{
if (nd == null) return;
TreeNode pNode = nd.Parent;
if (pNode == null) return;
if (!IsCheckDiff(nd)) pNode.Checked = false;
else pNode.Checked = pNode.Checked = nd.Checked;
SetParentNodeCheck(pNode);
}
//判断同一级节点的状态是否相同
private bool IsCheckDiff(TreeNode nd)
{
if (nd == null) return false;
TreeNode TempNode;
TempNode = nd;
while(true)
{
TempNode = TempNode.PrevNode;
if (TempNode == null) break;
if (TempNode.Checked != nd.Checked) return false;
}
TempNode = nd;
while (true)
{
TempNode = TempNode.NextNode;
if (TempNode == null) break;
if (TempNode.Checked != nd.Checked) return false;
}
return true;
}
private void LoadData()
{
DataSet ds = new DataSet();
string strSQL;
strSQL = "SELECT CODE,NAME FROM " + m_strTbl;
OleDbDataAdapter Adapter = new System.Data.OleDb.OleDbDataAdapter(strSQL, m_OleDbConn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(Adapter);
OleDbCommand cmd = new OleDbCommand();
Adapter.Fill(ds, m_strTbl);
m_table = ds.Tables[m_strTbl];
}
private void FillProvNode()
{
//创建根节点
treeView1.Nodes.Clear();
TreeNode nd = treeView1.Nodes.Add("全部");
if (m_table == null) return;
DataRow[] drs = null;
if (ProvCode != "") drs= m_table.Select("code = '" + ProvCode + "'");
else drs = m_table.Select("SUBSTRING(code,3,4)='0000'");
if (drs == null) return;
foreach (DataRow dr in drs)
{
string name = dr["name"].ToString().Trim();
TreeNode newND = nd.Nodes.Add(name);
newND.Name = dr["code"].ToString().Trim();
//加载第二级节点
if (iLevel > 1) FillDistNode(newND);
}
treeView1.ExpandAll();
}
private void FillDistNode(TreeNode nd)
{
if (m_table == null || nd == null) return;
string code = nd.Name.ToString().Substring(0,2);
DataRow[] drs = m_table.Select("SUBSTRING(code, 1, 2) = '" + code + "' and SUBSTRING(code,3,2)<>'00'" +
" and SUBSTRING(code,5,2) = '00'");
if (drs == null) return;
foreach (DataRow dr in drs)
{
string name = dr["name"].ToString().Trim();
TreeNode newND = nd.Nodes.Add(name);
newND.Name = dr["code"].ToString().Trim();
//加载第三级节点
if (iLevel > 2) FillCNTYNode(newND);
}
}
private void FillCNTYNode(TreeNode nd)
{
if (m_table == null || nd == null) return;
string code = nd.Name.ToString().Substring(0, 4);
DataRow[] drs = m_table.Select("SUBSTRING(code,1,4) = '" + code + "' and " + " SUBSTRING(code,5,2)<>'00'");
if (drs == null) return;
foreach (DataRow dr in drs)
{
string name = dr["name"].ToString().Trim();
TreeNode newND = nd.Nodes.Add(name);
newND.Name = dr["code"].ToString().Trim();
}
}
private void btnFlash_Click(object sender, EventArgs e)
{
IFeature feat = GetFeature();
if (feat == null) return;
ISimpleFillSymbol pFillsyl = new SimpleFillSymbol();
ESRI.ArcGIS.Display.RgbColor color = new RgbColorClass();
color.Red = 255;
color.Blue = 0;
color.Green = 0 ;
pFillsyl.Color = color;
mapControl.FlashShape(feat.Shape, 3, 300, pFillsyl);
}
private void btnPan_Click(object sender, EventArgs e)
{
IFeature feat = GetFeature();
if (feat == null) return;
IPoint pnt = new ESRI.ArcGIS.Geometry.Point();
pnt.X = (feat.Shape.Envelope.XMin + feat.Shape.Envelope.XMax)/2;
pnt.Y = (feat.Shape.Envelope.YMin+ feat.Shape.Envelope.YMax)/2;
mapControl.CenterAt(pnt);
mapControl.Refresh(esriViewDrawPhase.esriViewGeography, Type.Missing, Type.Missing);
}
private void btnZoom_Click(object sender, EventArgs e)
{
IFeature feat = GetFeature();
if (feat == null) return;
mapControl.Extent = feat.Shape.Envelope;
}
private void btnOK_Click(object sender, EventArgs e)
{
SelArrayList.Clear();
SaveSel(treeView1.Nodes[0]);
IsOK = true;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
SelArrayList.Clear();
IsOK = false;
this.Close();
}
private IFeature GetFeature()
{
TreeNode nd = treeView1.SelectedNode;
if (nd == null || nd.Parent == null) return null;
IFeatureLayer FLayer = null;
IFeatureCursor FCur = null;
switch (nd.Level)
{
case 1:
FLayer = clsGISPub.FindFeatureLayer(mapControl.Map, FirstLayer);
if (FLayer == null) return null;
FCur = clsGISPub.SearchFeature(FLayer.FeatureClass, FirstKeyField + "='" + nd.Name + "'");
if (FCur == null) return null;
return FCur.NextFeature();
case 2:
FLayer = clsGISPub.FindFeatureLayer(mapControl.Map, SecondLayer);
if (FLayer == null) return null;
FCur = clsGISPub.SearchFeature(FLayer.FeatureClass, SecondKeyField + "='" + nd.Name + "'");
if (FCur == null) return null;
return FCur.NextFeature();
default:
FLayer = clsGISPub.FindFeatureLayer(mapControl.Map, ThirdLayer);
if (FLayer == null) return null;
FCur = clsGISPub.SearchFeature(FLayer.FeatureClass, ThirdKeyField + "='" + nd.Name + "'");
if (FCur == null) return null;
return FCur.NextFeature();
}
}
private void SaveSel(TreeNode nd)
{
if (nd == null) return;
foreach (TreeNode newND in nd.Nodes)
{
if (newND.Level == iLevel && newND.Checked == true)
{
MyTreeNodeInfo nodeInfo = new MyTreeNodeInfo();
nodeInfo.NameValue = newND.Text;
nodeInfo.KeyValue = newND.Name;
SelArrayList.Add(nodeInfo);
}
SaveSel(newND);
}
}
//设置选择的节点
void SelectNode()
{
SelectNode(treeView1.Nodes[0]);
}
//行政区划编码列表
void SelectNode(TreeNode nd)
{
if (nd == null) return;
foreach (TreeNode newND in nd.Nodes)
{
if (newND.Level == iLevel)
{
if (SelArrayList.Contains(newND.Name)) newND.Checked = true;
}
SelectNode(newND);
}
}
public void SaveSel(ListBox listBox)
{
if (listBox == null) return;
if (SelArrayList == null) return;
listBox.Items.Clear();
for (int i = 0; i < SelArrayList.Count; i++)
{
MyTreeNodeInfo NodeInfo = (MyTreeNodeInfo)SelArrayList[i];
DictItem Item = new DictItem();
Item.code = NodeInfo.KeyValue;
Item.name = NodeInfo.NameValue;
listBox.Items.Add(Item);
}
}
public void SaveSel(ArrayList list)
{
if (list == null) return;
list.Clear();
for (int i = 0; i < SelArrayList.Count; i++)
{
MyTreeNodeInfo NodeInfo = (MyTreeNodeInfo)SelArrayList[i];
list.Add(NodeInfo.KeyValue);
}
}
}
class MyTreeNodeInfo
{
public String KeyValue;
public String NameValue;
public override string ToString()
{
return NameValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -