- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
[DEVEXPRESS] Hướng dẫn sử dụng TreeListLookupEdit và lưu trạng thái đóng mở từng Node C#
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách sử dụng TreeListLookupEdit Devexpress C#, và lưu lại trạng thái đóng mở của từng Node.
[DEVEXPRESS] Hướng dẫn sử dụng TreeListLookupEdit và lưu trạng thái đóng mở từng Node C#
Trong bài viết trước, mình đã có hướng dẫn các bạn cách làm việc với TreeList Devepress, bài này mình sẽ nói về TreeListLookupEdit.
Các bạn có thể tham khảo thêm về TreeList Devexpress C#
Dưới đây là giao diện demo ứng dụng sử dụng TreeListLookupEdit Devexpress C#:
TreeListLookupEdit C# thường được sử dụng để hiển thị các loại như: Danh mục, phân quyền người dùng, theo sơ đồ cây...
Trong bài viết này ngoài chức năng hiển thị mình hỗ trợ thêm các chức năng sau:
- Expand all node
- Collapse all node
- Save States Node sang File XML
- Retore States Node
Chức năng Save State của TreeListLookupEdit là gì?
Như các bạn nhìn hình ở trên có rất nhiều danh mục đang được chúng ta mở ra. Và mình mong muốn là sau khi mở ra và đóng ứng dụng lại.
Thì lần sau, khi mở vào TreeList này, vẫn sẽ hiển thị lại trạng thái như cũ mình đã chọn.
Để lưu trữ trạng thái của Node Expand hay Collapse trên TreeListLookupEdit, các bạn tạo cho mình 1 class TreeListViewState.cs
Source code TreeListViewState.cs
C#
using System;
using System.Collections;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;
using System.Xml.Linq;
using System.Windows.Forms;
namespace TreeListLookupEdit
{
class TreeListViewState
{
private ArrayList expanded;
private ArrayList selected;
private object focused;
private int topIndex;
public TreeListViewState() : this(null) { }
public TreeListViewState(TreeList treeList)
{
this.treeList = treeList;
expanded = new ArrayList();
selected = new ArrayList();
}
public void Clear()
{
expanded.Clear();
selected.Clear();
focused = null;
topIndex = 0;
}
private ArrayList GetExpanded()
{
OperationSaveExpanded op = new OperationSaveExpanded();
TreeList.NodesIterator.DoOperation(op);
return op.Nodes;
}
private ArrayList GetSelected()
{
ArrayList al = new ArrayList();
foreach (TreeListNode node in TreeList.Selection)
al.Add(node.GetValue(TreeList.KeyFieldName));
return al;
}
string fileName = "treeListState.xml";
public void LoadState()
{
try
{
XDocument xmlDocument = XDocument.Load(fileName);
foreach (XElement el in xmlDocument.Root.Elements())
{
switch (el.Name.LocalName)
{
case "NodeData":
focused = Convert.ToDouble(el.Attribute("Focused").Value);
topIndex = Convert.ToInt32(el.Attribute("Top").Value);
break;
case "SelectedNodes":
selected.Clear();
foreach (var item in el.Attribute("IDs").Value.Split(','))
selected.Add(Convert.ToDouble(item));
break;
case "ExpandedNodes":
expanded.Clear();
foreach (var item in el.Attribute("IDs").Value.Split(','))
expanded.Add(Convert.ToDouble(item));
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("No data. Check an XML file");
return;
}
TreeList.BeginUpdate();
try
{
TreeList.CollapseAll();
TreeListNode node;
foreach (object key in expanded)
{
//node = TreeList.FindNodeByKeyID(key);
node = TreeList.FindNode(n => n.GetValue("ID").ToString() == key.ToString());
if (node != null)
node.Expanded = true;
}
foreach (object key in selected)
{
node = TreeList.FindNode(n => n.GetValue("ID").ToString() == key.ToString());
if (node != null)
TreeList.Selection.Add(node);
}
TreeList.FocusedNode = TreeList.FindNode(n => n.GetValue("ID").ToString() == focused.ToString());
}
finally
{
TreeList.EndUpdate();
TreeList.TopVisibleNodeIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - topIndex;
}
}
public void SaveState()
{
//if (TreeList.FocusedNode != null)
//{
expanded = GetExpanded();
selected = GetSelected();
focused = TreeList.FocusedNode[TreeList.KeyFieldName];
topIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - TreeList.TopVisibleNodeIndex;
XDocument xmlDocument = new XDocument(new XElement("TreeListState"));
xmlDocument.Root.Add(
new XElement("NodeData",
new XAttribute("Focused", focused),
new XAttribute("Top", topIndex)));
var selectedNodes = string.Join(",", selected.ToArray());
var expandedNodes = string.Join(",", expanded.ToArray());
xmlDocument.Root.Add(
new XElement("SelectedNodes",
new XAttribute("IDs", selectedNodes)));
xmlDocument.Root.Add(
new XElement("ExpandedNodes",
new XAttribute("IDs", expandedNodes)));
xmlDocument.Save(fileName);
//}
//else
// Clear();
}
private TreeList treeList;
public TreeList TreeList
{
get
{
return treeList;
}
set
{
treeList = value;
Clear();
}
}
class OperationSaveExpanded : TreeListOperation
{
private ArrayList al = new ArrayList();
public override void Execute(TreeListNode node)
{
if (node.HasChildren && node.Expanded)
al.Add(node.GetValue(node.TreeList.KeyFieldName));
}
public ArrayList Nodes { get { return al; } }
}
}
}
Và source code Form1.cs
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TreeListLookupEdit
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
treeListViewState = new TreeListViewState(treeListLookUpEdit1TreeList);
}
TreeListViewState treeListViewState;
private void Form1_Load(object sender, EventArgs e)
{
string json_danhmuc_muaban = File.ReadAllText("data.json");
var table = (DataTable)JsonConvert.DeserializeObject(json_danhmuc_muaban, (typeof(DataTable)));
DataTable tbl_danhmuc_muaban = table.AsEnumerable()
.CopyToDataTable();
treeListLookUpEdit1.Properties.AutoExpandAllNodes = false;
treeListLookUpEdit1.Properties.DataSource = tbl_danhmuc_muaban;
treeListLookUpEdit1.Properties.ValueMember = "ID";
treeListLookUpEdit1.Properties.DisplayMember = "name";
treeListLookUpEdit1TreeList.StateImageList = imageList1;
treeListLookUpEdit1TreeList.ViewStyle = DevExpress.XtraTreeList.TreeListViewStyle.TreeView;
treeListLookUpEdit1TreeList.OptionsView.ShowTreeLines = DevExpress.Utils.DefaultBoolean.True;
treeListLookUpEdit1TreeList.CollapseAll();
}
private void treeListLookUpEdit1TreeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
{
try
{
int index = int.Parse(e.Node.GetValue("img_index").ToString());
if (index >= 0)
{
e.NodeImageIndex = index;
}
}
catch (Exception) { }
}
public bool isExpand = false;
private void btn_expand_Click(object sender, EventArgs e)
{
isExpand = !isExpand;
if (isExpand)
{
treeListLookUpEdit1TreeList.ExpandAll();
btn_expand.Text = "Collapse All";
}
else
{
treeListLookUpEdit1TreeList.CollapseAll();
btn_expand.Text = "Expand All";
}
}
private void btn_save_Click(object sender, EventArgs e)
{
treeListViewState.SaveState();
}
private void btn_restore_Click(object sender, EventArgs e)
{
treeListViewState.LoadState();
}
}
}
Các bạn có thể download source code ở bên dưới về để tham khảo.
Thanks for watching!