123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using CommonLang.Property;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace CommonFroms
- {
- public static class FormUtils
- {
- [DllImport("user32.dll", SetLastError = true)]
- static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);
- /// <summary>
- /// 切换到当前窗口
- /// </summary>
- /// <param name="form"></param>
- /// <param name="turnOn"></param>
- static public void SwithToThisForm(Form form, bool turnOn)
- {
- SwitchToThisWindow(form.Handle, turnOn);
- }
- /// <summary>
- /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="view"></param>
- /// <param name="list"></param>
- /// <returns></returns>
- public static bool ListViewItemTagEquals<T>(ListView view, ICollection<T> list) where T : class
- {
- if (view.Items.Count != list.Count)
- {
- return false;
- }
- foreach (ListViewItem item in view.Items)
- {
- T it = item.Tag as T;
- if (!list.Contains(it))
- {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
- /// </summary>
- /// <param name="view"></param>
- /// <param name="list"></param>
- /// <param name="compare">参数1表示list中的单位,参数2表示ListView里的Tag</param>
- /// <returns></returns>
- public static bool ListViewItemTagEquals(ListView view, IList list, IComparer compare)
- {
- if (view.Items.Count != list.Count)
- {
- return false;
- }
- foreach (ListViewItem item in view.Items)
- {
- bool finded = false;
- foreach (object tag in list)
- {
- if (compare.Compare(tag, item.Tag) == 0)
- {
- finded = true;
- break;
- }
- }
- if (!finded)
- {
- return false;
- }
- }
- return true;
- }
- public static void GetAllTreeNodes(TreeNodeCollection nodes, List<TreeNode> allnodes)
- {
- foreach (TreeNode node in nodes)
- {
- allnodes.Add(node);
- GetAllTreeNodes(node.Nodes, allnodes);
- }
- }
- public static TreeNode FindTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
- {
- List<TreeNode> allnodes = new List<TreeNode>();
- GetAllTreeNodes(nodes, allnodes);
- int begin = allnodes.IndexOf(start);
- if (begin < 0)
- {
- begin = 0;
- }
- else if (!include)
- {
- begin += 1;
- }
- for (int i = begin; i < allnodes.Count; i++)
- {
- TreeNode tn = allnodes[i];
- if (tn.Text.Contains(text))
- {
- return tn;
- }
- }
- return null;
- }
- public static TreeNode FindLastTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
- {
- List<TreeNode> allnodes = new List<TreeNode>();
- GetAllTreeNodes(nodes, allnodes);
- int end = allnodes.IndexOf(start);
- if (end < 0)
- {
- end = allnodes.Count - 1;
- }
- else if (!include)
- {
- end -= 1;
- }
- for (int i = end; i >= 0; i--)
- {
- TreeNode tn = allnodes[i];
- if (tn.Text.Contains(text))
- {
- return tn;
- }
- }
- return null;
- }
- }
-
- }
|