Guest User

Untitled

a guest
Jun 29th, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.92 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Windows.Forms;
  3. using System;
  4. using System.Linq;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7.  
  8. namespace scripts
  9. {
  10. public partial class CustomGUIs
  11. {
  12. public static List<Dictionary<string, object>> DataGrid(List<Dictionary<string, object>> entries, List<string> propertyNames, bool spanAllScreens)
  13. {
  14. List<Dictionary<string, object>> filteredEntries = new List<Dictionary<string, object>>();
  15. bool escapePressed = false; // Flag to track if Escape was pressed
  16.  
  17. var form = new Form();
  18. form.StartPosition = FormStartPosition.CenterScreen;
  19. form.Text = $"Total Entries: {entries.Count}";
  20. form.BackColor = Color.White;
  21.  
  22. var dataGridView = new DataGridView();
  23. dataGridView.Dock = DockStyle.Fill;
  24. dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  25. dataGridView.MultiSelect = true;
  26. dataGridView.ReadOnly = true;
  27. dataGridView.AutoGenerateColumns = false;
  28. dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  29. dataGridView.BackgroundColor = Color.White;
  30.  
  31. foreach (var propertyName in propertyNames)
  32. {
  33. dataGridView.Columns.Add(new DataGridViewTextBoxColumn
  34. {
  35. HeaderText = propertyName,
  36. DataPropertyName = propertyName
  37. });
  38. }
  39.  
  40. // Manually populate DataGridView rows
  41. foreach (var entry in entries)
  42. {
  43. var row = new DataGridViewRow();
  44. row.CreateCells(dataGridView);
  45.  
  46. for (int i = 0; i < propertyNames.Count; i++)
  47. {
  48. row.Cells[i].Value = entry.ContainsKey(propertyNames[i]) ? entry[propertyNames[i]] : null;
  49. }
  50.  
  51. dataGridView.Rows.Add(row);
  52. }
  53.  
  54. var searchBox = new TextBox();
  55. searchBox.Dock = DockStyle.Top;
  56.  
  57. searchBox.TextChanged += (sender, e) =>
  58. {
  59. string searchText = searchBox.Text.ToLower();
  60. var searchTerms = searchText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  61. .Select(query => query.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
  62.  
  63. var filtered = entries.Where(entry =>
  64. {
  65. return searchTerms.All(orQuery =>
  66. {
  67. return orQuery.Any(term =>
  68. {
  69. return entry.Values.Any(value => value != null && value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) >= 0);
  70. });
  71. });
  72. }).ToList();
  73.  
  74. // Clear and repopulate DataGridView with filtered data
  75. dataGridView.Rows.Clear();
  76. foreach (var item in filtered)
  77. {
  78. var newRow = new DataGridViewRow();
  79. newRow.CreateCells(dataGridView);
  80. for (int i = 0; i < propertyNames.Count; i++)
  81. {
  82. newRow.Cells[i].Value = item.ContainsKey(propertyNames[i]) ? item[propertyNames[i]] : null;
  83. }
  84. dataGridView.Rows.Add(newRow);
  85. }
  86.  
  87. // Adjust form size
  88. dataGridView.AutoResizeColumns();
  89. int requiredWidth = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + SystemInformation.VerticalScrollBarWidth + 50;
  90.  
  91. int availableWidth = Screen.PrimaryScreen.WorkingArea.Width - 20;
  92. form.Width = Math.Min(requiredWidth, availableWidth);
  93.  
  94. if (form.Width != requiredWidth)
  95. {
  96. form.StartPosition = FormStartPosition.Manual;
  97. form.Location = new Point(
  98. (Screen.PrimaryScreen.WorkingArea.Width - form.Width) / 2,
  99. form.Location.Y
  100. );
  101. }
  102. };
  103.  
  104. // KeyDown event handling for DataGridView
  105. dataGridView.KeyDown += (sender, e) =>
  106. {
  107. if (e.KeyCode == Keys.Escape)
  108. {
  109. escapePressed = true; // Set flag when Escape is pressed
  110. form.Close();
  111. }
  112. else if (e.KeyCode == Keys.Enter)
  113. {
  114. // Clear previously filtered entries if any
  115. filteredEntries.Clear();
  116.  
  117. foreach (DataGridViewRow selectedRow in dataGridView.SelectedRows)
  118. {
  119. Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
  120. for (int i = 0; i < propertyNames.Count; i++)
  121. {
  122. selectedEntry[propertyNames[i]] = selectedRow.Cells[i].Value;
  123. }
  124. filteredEntries.Add(selectedEntry);
  125. }
  126. escapePressed = true; // Set flag when Escape is pressed
  127. form.Close();
  128. }
  129. else if (e.KeyCode == Keys.Tab)
  130. {
  131. searchBox.Focus();
  132. }
  133. // Check for Shift + Left/Right Arrow keys
  134. else if ((e.KeyCode == Keys.Right || e.KeyCode == Keys.Left) && e.Shift)
  135. {
  136. int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
  137. int visibleWidth = dataGridView.ClientRectangle.Width;
  138. int currentOffset = dataGridView.HorizontalScrollingOffset;
  139. int scrollOffset = 1000; // You can adjust this value to change scroll speed
  140.  
  141. if (e.KeyCode == Keys.Right)
  142. {
  143. // Attempt to scroll right
  144. if (currentOffset + visibleWidth < totalColumnWidth)
  145. {
  146. dataGridView.HorizontalScrollingOffset += scrollOffset;
  147. }
  148. }
  149. else if (e.KeyCode == Keys.Left)
  150. {
  151. // Attempt to scroll left
  152. dataGridView.HorizontalScrollingOffset = Math.Max(currentOffset - scrollOffset, 0);
  153. }
  154. e.Handled = true; // Prevent further processing of the key in other controls
  155. }
  156. else if (e.KeyCode == Keys.Right)
  157. {
  158. int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
  159. int visibleWidth = dataGridView.ClientRectangle.Width;
  160. int currentOffset = dataGridView.HorizontalScrollingOffset;
  161. int scrollOffset = 50; // You can adjust this value to change scroll speed
  162.  
  163. if (currentOffset + visibleWidth < totalColumnWidth)
  164. {
  165. dataGridView.HorizontalScrollingOffset += scrollOffset;
  166. }
  167. e.Handled = true; // Prevent further processing of the key in other controls
  168. }
  169. else if (e.KeyCode == Keys.Left)
  170. {
  171. int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
  172. int visibleWidth = dataGridView.ClientRectangle.Width;
  173. int currentOffset = dataGridView.HorizontalScrollingOffset;
  174. int scrollOffset = 50; // You can adjust this value to change scroll speed
  175.  
  176. dataGridView.HorizontalScrollingOffset = Math.Max(currentOffset - scrollOffset, 0);
  177. e.Handled = true; // Prevent further processing of the key in other controls
  178. }
  179. };
  180.  
  181. // KeyDown event handling for SearchBox
  182. searchBox.KeyDown += (sender, e) =>
  183. {
  184. if (e.KeyCode == Keys.Escape)
  185. {
  186. escapePressed = true; // Set flag when Escape is pressed
  187. form.Close();
  188. }
  189. else if (e.KeyCode == Keys.Down)
  190. {
  191. if (dataGridView.Rows.Count > 0)
  192. {
  193. dataGridView.Focus();
  194. dataGridView.CurrentCell = dataGridView.Rows[0].Cells[0];
  195. dataGridView.Rows[0].Selected = true;
  196. }
  197. }
  198. else if (e.KeyCode == Keys.Enter)
  199. {
  200. dataGridView.Focus();
  201. dataGridView.Rows[0].Selected = true;
  202. foreach (DataGridViewRow selectedRow in dataGridView.SelectedRows)
  203. {
  204. Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
  205. for (int i = 0; i < propertyNames.Count; i++)
  206. {
  207. selectedEntry[propertyNames[i]] = selectedRow.Cells[i].Value;
  208. }
  209. filteredEntries.Add(selectedEntry);
  210. }
  211. form.Close();
  212. }
  213. };
  214.  
  215. form.Load += (sender, e) =>
  216. {
  217. //sort the entries based on the first column
  218. dataGridView.Sort(dataGridView.Columns[0], ListSortDirection.Ascending);
  219.  
  220. foreach (DataGridViewColumn column in dataGridView.Columns)
  221. {
  222. column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  223. }
  224. dataGridView.AutoResizeColumns();
  225.  
  226. int padding = 20;
  227. int requiredHeight = dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible) + dataGridView.ColumnHeadersHeight + 2 * dataGridView.Rows[0].Height + SystemInformation.HorizontalScrollBarHeight;
  228. int availableHeight = Screen.PrimaryScreen.WorkingArea.Height - padding * 2;
  229.  
  230. form.Height = Math.Min(requiredHeight, availableHeight);
  231.  
  232. if (spanAllScreens)
  233. {
  234. int totalScreenWidth = Screen.AllScreens.Sum(screen => screen.WorkingArea.Width);
  235. form.Width = totalScreenWidth;
  236. form.StartPosition = FormStartPosition.Manual;
  237. form.Location = new Point(
  238. Screen.AllScreens.OrderBy(screen => screen.Bounds.X).First().Bounds.X,
  239. (Screen.PrimaryScreen.WorkingArea.Height - form.Height) / 2
  240. );
  241. }
  242. else
  243. {
  244. int requiredWidth = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + SystemInformation.VerticalScrollBarWidth + 43;
  245.  
  246. // Calculate the available size minus padding
  247. int availableWidth = Screen.PrimaryScreen.WorkingArea.Width - padding * 2;
  248.  
  249. // Ensure the form fits within the padded screen working area
  250. form.Height = Math.Min(requiredHeight, availableHeight);
  251. form.Width = Math.Min(requiredWidth, availableWidth);
  252.  
  253. // Center the form manually if any dimension is adjusted
  254. if (form.Height != requiredHeight || form.Width != requiredWidth)
  255. {
  256. form.StartPosition = FormStartPosition.Manual;
  257. form.Location = new Point(
  258. (Screen.PrimaryScreen.WorkingArea.Width - form.Width) / 2,
  259. (Screen.PrimaryScreen.WorkingArea.Height - form.Height) / 2
  260. );
  261. }
  262. }
  263. };
  264.  
  265. form.Controls.Add(dataGridView);
  266. form.Controls.Add(searchBox);
  267. searchBox.Select();
  268. form.ShowDialog();
  269.  
  270. if (!escapePressed)
  271. {
  272. // Extract filtered entries based on selected rows
  273. foreach (DataGridViewRow row in dataGridView.SelectedRows)
  274. {
  275. Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
  276. for (int i = 0; i < propertyNames.Count; i++)
  277. {
  278. selectedEntry[propertyNames[i]] = row.Cells[i].Value;
  279. }
  280. filteredEntries.Add(selectedEntry);
  281. }
  282. }
  283.  
  284. return filteredEntries;
  285. }
  286. }
  287. }
  288.  
Advertisement
Add Comment
Please, Sign In to add comment