📄 fmaddressdata.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AddressList.Utils;
using System.Threading;
using System.Data.Common;
using System.IO;
namespace AddressList.Forms
{
public partial class fmAddressData : fmBaseForm
{
public fmAddressData()
{
InitializeComponent();
InitForm();
}
//private static string[] sWeeks = { "日", "一", "二", "三", "四", "五", "六" };
private DataSet friendDataSet = new DataSet();
private const string SCurUser = "操作用户:";
//private const string SWeek = " 星期";
private const string SChinaDay = "农历:";
private const string SError = "错误";
private const string SDeleteRecord = "删除数据将不能恢复,是否删除?";
private const string SDataTableName = "t_Friend";
private DateTime lunarHolDate;
private DateTime loginDate = DateTime.Today;
private Int32 oldRowIndex = -1;
/// <summary>
/// 初始化Form
/// </summary>
protected virtual void InitForm()
{
notifyIcon.Icon = this.Icon;
notifyIcon.Visible = true;
const string loginString = "{0} 你已登录[{1}]程序。";
string tipText = string.Format(loginString, CurrentUserInfo.UserName, ClientFuncion.AssemblyTitle);
notifyIcon.ShowBalloonTip(3000, ClientFuncion.AssemblyTitle, tipText, ToolTipIcon.Info);
tsUser.Text = SCurUser + CurrentUserInfo.UserName; //加入用户名称
ReSetDate();
dgvData.AutoGenerateColumns = false;
ReLoadGroup();
}
/// <summary>
/// 重新设置日期
/// </summary>
private void ReSetDate()
{
loginDate = DateTime.Today;
lunarHolDate = new CNDate(loginDate).GetLunarHolDate();
tsToday.Text = loginDate.ToLongDateString() + " " + loginDate.ToString("dddd"); //星期
tsChinaDay.Text = SChinaDay + lunarHolDate.ToLongDateString(); //加入农历日期
}
/// <summary>
/// 退出程序
/// </summary>
protected void ExitApplication()
{
notifyIcon.Visible = false;
Close();
Application.Exit();
}
private void miExitApplication_Click(object sender, EventArgs e)
{
ExitApplication();
}
/// <summary>
/// 显示窗体
/// </summary>
private void miShowForm_Click(object sender, EventArgs e)
{
Show();
}
/// <summary>
/// 隐藏窗体
/// </summary>
private void miHideForm_Click(object sender, EventArgs e)
{
Hide();
}
/// <summary>
/// 托盘弹出菜单时
/// </summary>
private void cmsNotify_Opening(object sender, CancelEventArgs e)
{
miShowForm.Enabled = !this.Visible;
miHideForm.Enabled = this.Visible;
}
/// <summary>
/// 最小化时,隐藏主窗体
/// </summary>
private void fmAddressData_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
Hide();
}
/// <summary>
/// 关于
/// </summary>
private void menuAbout_Click(object sender, EventArgs e)
{
fmAboutBox aboutBox = new fmAboutBox();
try
{
aboutBox.ShowDialog();
}
finally
{
aboutBox.Dispose();
}
}
/// <summary>
/// 退出程序
/// </summary>
private void menuExit_Click(object sender, EventArgs e)
{
ExitApplication();
}
/// <summary>
/// 新增、修改 组别
/// </summary>
/// <param name="billState">新增、修改</param>
private void DoAddEditGroup(BillStates billState)
{
TreeNode node = tvGroups.SelectedNode;
if (node == null || node.Name.Trim().Length == 0)
return;
fmEditGroups fmGroups = new fmEditGroups(billState, Convert.ToInt32(node.Name));
try
{
if (fmGroups.ShowDialog() == DialogResult.OK)
ReLoadGroup();
}
finally
{
fmGroups.Dispose();
}
}
/// <summary>
/// 新增、修改 联系人
/// </summary>
/// <param name="billState"></param>
private void DoAddEditFriend(BillStates billState)
{
if (BillStates.bsNew != billState && null == dgvData.CurrentRow)
return;
int integerID = 0, parentID = 0;
if (null != tvGroups.SelectedNode && tvGroups.SelectedNode.Name.Length > 0)
parentID = Convert.ToInt32(tvGroups.SelectedNode.Name);
if (BillStates.bsNew != billState)
{
string number = dgvData[ColumnNumber.Index, dgvData.CurrentRow.Index].Value.ToString();
DataTable friendTable = friendDataSet.Tables[SDataTableName];
DataRow dataRow = friendTable.DefaultView[dgvData.CurrentRow.Index].Row;
if (dataRow == null || dataRow.IsNull("FItemID"))
return;
else
{
integerID = Convert.ToInt32(dataRow["FItemID"]);
parentID = Convert.ToInt32(dataRow["FParentID"]);
}
}
fmEditData fmFriend = new fmEditData(billState, integerID, parentID);
try
{
if (fmFriend.ShowDialog() == DialogResult.OK)
{
if (null != tvGroups.SelectedNode)
{
int itemid = Convert.ToInt32(tvGroups.SelectedNode.Name);
string sqlWhere = (itemid > 0) ? string.Format("WHERE FParentID={0} ", itemid) : string.Empty;
ReadFriendData(sqlWhere);
}
}
}
finally
{
fmFriend.Dispose();
}
}
/// <summary>
/// 新增 联系人
/// </summary>
private void tbNew_Click(object sender, EventArgs e)
{
DoAddEditFriend(BillStates.bsNew);
}
/// <summary>
/// 修改 联系人
/// </summary>
private void tbEdit_Click(object sender, EventArgs e)
{
DoAddEditFriend(BillStates.bsEdit);
}
/// <summary>
/// 删除 联系人
/// </summary>
private void tbDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show(SDeleteRecord, ClientFuncion.AssemblyTitle, MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
//删除数据
const string delFriendData = "DELETE FROM t_Friend WHERE FItemID={0}";
for(int I = dgvData.SelectedRows.Count -1; I >= 0; I--)
{
DataGridViewRow gridRow = dgvData.SelectedRows[I];
DataTable friendTable = friendDataSet.Tables[SDataTableName];
DataRow dataRow = friendTable.DefaultView[gridRow.Index].Row;
int itemID = Convert.ToInt32(dataRow["FItemID"]);
DBConn.DBCommand(string.Format(delFriendData, itemID)).ExecuteNonQuery();
friendDataSet.Tables[0].DefaultView.Delete(gridRow.Index);
}
};
}
/// <summary>
/// 新增组别
/// </summary>
private void menuAddGroup_Click(object sender, EventArgs e)
{
DoAddEditGroup(BillStates.bsNew);
}
/// <summary>
/// 修改组别
/// </summary>
private void menuEditGroup_Click(object sender, EventArgs e)
{
DoAddEditGroup(BillStates.bsEdit);
}
/// <summary>
/// 双击树 修改组别
/// </summary>
private void tvGroups_DoubleClick(object sender, EventArgs e)
{
DoAddEditGroup(BillStates.bsEdit);
}
/// <summary>
/// 删除组别
/// </summary>
protected bool DeleteGroup(int integerID)
{
const string readGroupName = "SELECT FName FROM t_FriendGroup WHERE FInterID > 0 AND FInterID={0}";
object[] groupName = DBConn.DBSelectValue(string.Format(readGroupName, integerID));
bool result = false;
if (null == groupName || groupName[0].ToString().Length == 0)
{
const string notHasGroup = "组别[{0}]不存在,请刷新数据。";
ClientFuncion.ShowErrorMsg(string.Format(notHasGroup, groupName[0].ToString()));
}
else
{
object[] values = DBConn.DBSelectValue(string.Format("SELECT FInterID FROM t_FriendGroup WHERE FParentID={0}", integerID));
if (null != values && (int)values[0] > 0)
{
const string hasSubGroup = "组别[{0}]存在下级组,不能删除。";
ClientFuncion.ShowErrorMsg(string.Format(hasSubGroup, groupName[0].ToString()));
}
else
{
object[] friends = DBConn.DBSelectValue(string.Format("SELECT FItemID FROM t_Friend WHERE FParentID={0}", integerID));
if (null != friends && (int)friends[0] > 0)
{
const string hasFriend = "组别[{0}]存在相关数据,不能删除。";
ClientFuncion.ShowErrorMsg(string.Format(hasFriend, friends[0].ToString()));
}
else
{
if (MessageBox.Show(SDeleteRecord, ClientFuncion.AssemblyTitle, MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
const string deleteGroup = "DELETE FROM t_FriendGroup WHERE FInterID > 0 AND FInterID={0}";
result = (DBConn.DBCommand(string.Format(deleteGroup, integerID)).ExecuteNonQuery() >= 1);
}
}
}
}
return result;
}
private void menuDeleteGroup_Click(object sender, EventArgs e)
{
TreeNode node = tvGroups.SelectedNode;
if (node == null || node.Name.Trim().Length == 0)
return;
if (DeleteGroup(Convert.ToInt32(node.Name)))
{
if (null != node.PrevNode) tvGroups.SelectedNode = node.PrevNode;
node.Remove();
node = null;
};
}
private void menuToolBar_Click(object sender, EventArgs e)
{
menuToolBar.Checked = !menuToolBar.Checked;
toolStrip.Visible = menuToolBar.Checked;
}
private void menuStatusBar_Click(object sender, EventArgs e)
{
menuStatusBar.Checked = !menuStatusBar.Checked;
statusStrip.Visible = menuStatusBar.Checked;
}
private void miGroupsTree_Click(object sender, EventArgs e)
{
menuTree.Checked = !menuTree.Checked;
tvGroups.Visible = menuTree.Checked;
}
/// <summary>
/// 读取组数据
/// </summary>
private void ReLoadGroup()
{
DateTime t1 = DateTime.Now;
string oldSelKey = string.Empty;
tvGroups.BeginUpdate();
try
{
if (tvGroups.SelectedNode != null)
oldSelKey = tvGroups.SelectedNode.Name;
tvGroups.Nodes.Clear();
const string SReadGroup = "SELECT FInterID, FParentID, FName FROM t_FriendGroup ORDER BY FParentID,FInterID";
try
{
DbCommand cmd = DBCommand(SReadGroup);
DbDataReader dr = cmd.ExecuteReader();
TreeNode parentNode = null, supNode = null;
string oldParentKey = string.Empty;
string parentKey, groupKey, groupName;
while (dr.Read())
{
parentKey = dr["FParentID"].ToString();
groupKey = dr["FInterID"].ToString();
groupName = dr["FName"].ToString().Trim();
if (oldParentKey != parentKey)
{
//如果是第一笔,当作主 组别
if (oldParentKey.Length == 0)
parentNode = tvGroups.Nodes.Add(groupKey, groupName, 0);
else
{
parentNode = (null != supNode)? supNode: tvGroups.Nodes[0];
supNode = parentNode.Nodes.Add(groupKey, groupName, 0);
}
Font font = new Font(tvGroups.Font, FontStyle.Bold);
parentNode.NodeFont = font;
oldParentKey = parentKey;
}
else
supNode = parentNode.Nodes.Add(groupKey, groupName, 0);
}
cmd.Cancel();
dr.Close();
}
catch (InvalidOperationException E)
{
ClientFuncion.ShowErrorMsg(E.ToString());
}
}
finally
{
if ((oldSelKey.Length > 0) /*& (oldSelKey < tvGroups.GetNodeCount(true))*/)
{
foreach (TreeNode node in tvGroups.Nodes)
{
//TreeNode node = tvGroups.Nodes. [tvGroups.Nodes.IndexOfKey(oldSelKey)];
//if (node.Name == oldSelKey)
tvGroups.SelectedNode = PrintRecursive(node, oldSelKey);
}
}
if (null == tvGroups.SelectedNode && tvGroups.Nodes.Count > 0)
tvGroups.SelectedNode = tvGroups.Nodes[0];
if (null != tvGroups.SelectedNode) tvGroups.SelectedNode.Expand();
tvGroups.EndUpdate();
}
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
tsTips.Text = "读取数据花费了:" + sp.Seconds.ToString() + "秒";
}
/// <summary>
/// 遍历树节点
/// </summary>
/// <param name="treeNode">树节点</param>
/// <param name="AKey">主键值</param>
/// <returns></returns>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -