阻止组合框在选择项目之前折叠下拉菜单

huangapple go评论104阅读模式
英文:

Prevent dropdown of Combo Box collapse before item selected

问题

以下是您要翻译的内容:

"I want when the button in Pagination is clicked, the dropdown of the Combo Box is still opened, so I can see the result of the pagination in the items of the Combo Box and the dropdown closed when I selected item of Combo Box.

My problem is when I click the buttons in Pagination, the dropdown of the Combo Box closed, so I can't see the result of the pagination.

How to prevent dropdown of Combo Box collapse before item selected in C# Win Form?"

CtechComboBoxPagination.cs

  1. using static DiscountCard.View.CustomControl.CtechPagination;
  2. namespace DiscountCard.View.CustomControl
  3. {
  4. public partial class CtechComboBoxPagination : ComboBox
  5. {
  6. public CtechPagination? Pagination { get; set; }
  7. protected override CreateParams CreateParams
  8. {
  9. get
  10. {
  11. CreateParams cp = base.CreateParams;
  12. cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
  13. return cp;
  14. }
  15. }
  16. public CtechComboBoxPagination()
  17. {
  18. InitializeComponent();
  19. DoubleBuffered = true;
  20. DropDownStyle = ComboBoxStyle.DropDownList;
  21. DrawMode = DrawMode.OwnerDrawFixed;
  22. Font = new Font("Consolas", 11F, FontStyle.Regular, GraphicsUnit.Point);
  23. Pagination = new CtechPagination();
  24. Pagination.SetInitialize(Enumerable.Range(0, 100).AsQueryable(), newContent: true, PaginationPosition.LastPage);
  25. Pagination.FirstPaginationButton_Click += Pagination_FirstPaginationButton_Click;
  26. Pagination.PrevPaginationButton_Click += Pagination_PrevPaginationButton_Click;
  27. Pagination.NextPaginationButton_Click += Pagination_NextPaginationButton_Click;
  28. Pagination.LastPaginationButton_Click += Pagination_LastPaginationButton_Click;
  29. }
  30. private void Pagination_FirstPaginationButton_Click(object? sender, EventArgs e)
  31. {
  32. DroppedDown = true;
  33. }
  34. private void Pagination_PrevPaginationButton_Click(object? sender, EventArgs e)
  35. {
  36. DroppedDown = true;
  37. }
  38. private void Pagination_NextPaginationButton_Click(object? sender, EventArgs e)
  39. {
  40. DroppedDown = true;
  41. }
  42. private void Pagination_LastPaginationButton_Click(object? sender, EventArgs e)
  43. {
  44. DroppedDown = true;
  45. }
  46. protected override void OnDrawItem(DrawItemEventArgs e)
  47. {
  48. e.DrawBackground();
  49. if (e.Index < 0 || e.Index >= Items.Count)
  50. {
  51. return;
  52. }
  53. var item = Items[e.Index];
  54. String? text = item == null ? "(null)" : item.ToString();
  55. TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
  56. base.OnDrawItem(e);
  57. }
  58. protected override void OnDropDown(EventArgs e)
  59. {
  60. base.OnDropDown(e);
  61. if (Pagination == null)
  62. {
  63. return;
  64. }
  65. // for test
  66. if (Pagination.CurrentPage != null)
  67. {
  68. Items.Clear();
  69. for (int i = Pagination.CurrentPage.MinIndex; i <= Pagination.CurrentPage.MaxIndex; i++)
  70. {
  71. Items.Add(i);
  72. }
  73. }
  74. Pagination.Margin = new Padding(0);
  75. Pagination.MinimumSize = new Size(this.Width, 30);
  76. ToolStripControlHost host = new ToolStripControlHost(Pagination);
  77. host.Padding = new Padding(0);
  78. ToolStripDropDown dropdown = new ToolStripDropDown();
  79. dropdown.Padding = new Padding(0);
  80. dropdown.Items.Add(host);
  81. if (this.Items.Count > 0)
  82. {
  83. dropdown.Show(this, 0, 3 + this.Height + (this.Items.Count * this.ItemHeight));
  84. }
  85. }
  86. }
  87. }

CtechPagination.cs

  1. using CardData.Ext.Mod.Logger;
  2. using DiscountCard.Ext.Mod.General;
  3. using DiscountCard.View.CustomControl.Pagination;
  4. namespace DiscountCard.View.CustomControl
  5. {
  6. public partial class CtechPagination : UserControl
  7. {
  8. public enum PaginationPosition
  9. {
  10. FirstPage,
  11. PreviousPage,
  12. NextPage,
  13. LastPage,
  14. CurrrentPage
  15. }
  16. public CtechPagination()
  17. {
  18. InitializeComponent();
  19. FirstPaginationButton.Click += OnFirstPaginationButton_Click;
  20. PrevPaginationButton.Click += OnPrevPaginationButton_Click;
  21. NextPaginationButton.Click += OnNextPaginationButton_Click;
  22. LastPaginationButton.Click += OnLastPaginationButton_Click;
  23. }
  24. public event EventHandler? FirstPaginationButton_Click;
  25. public event EventHandler? PrevPaginationButton_Click;
  26. public event EventHandler? NextPaginationButton_Click;
  27. public event EventHandler? LastPaginationButton_Click;
  28. protected virtual void OnFirstPaginationButton_Click(object? sender, EventArgs e)
  29. {
  30. try
  31. {
  32. if (Pages == null)
  33. {
  34. return;
  35. }
  36. if (Pages.Count == 0)
  37. {
  38. return;
  39. }
  40. PageIndex = 0;
  41. Page? firstPage = Pages[PageIndex];
  42. if (firstPage != null)
  43. {
  44. PagesLabel.Text = String.Format("{0:N0} of {1:N0}", firstPage.MinIndex, firstPage.MaxIndex);
  45. FirstPage = firstPage;
  46. CurrentPage = firstPage;
  47. FirstPaginationButton_Click?.Invoke(this, e);
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. ExLogger.LogException(ex, "");
  53. }
  54. }
  55. protected virtual void OnPrevPaginationButton_Click(object? sender, EventArgs e)
  56. {
  57. try
  58. {
  59. if (Pages == null)
  60. {
  61. return;
  62. }
  63. if (Pages.Count == 0)
  64. {
  65. return;
  66. }
  67. --PageIndex;
  68. if (PageIndex < 0)
  69. {
  70. PageIndex = 0;
  71. }
  72. Page? prevPage = Pages[PageIndex];
  73. if (prevPage != null)
  74. {
  75. PagesLabel.Text = String.Format("{0:N0} of {1:N0}", prevPage.MinIndex, prevPage.MaxIndex);
  76. PreviousPage = prevPage;
  77. CurrentPage = prevPage;
  78. PrevPaginationButton_Click?.Invoke(this, e);
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. ExLogger.LogException(ex, "");
  84. }
  85. }
  86. protected virtual void OnNextPaginationButton_Click(object? sender, EventArgs e)
  87. {
  88. try
  89. {
  90. if (Pages == null)
  91. {
  92. return;
  93. }
  94. if (Pages.Count == 0)
  95. {
  96. return;
  97. }
  98. ++PageIndex;
  99. if (PageIndex > Pages.Count - 1)
  100. {
  101. PageIndex = Pages.Count - 1;
  102. }
  103. Page? nextPage = Pages[PageIndex];
  104. if (nextPage != null)
  105. {
  106. PagesLabel.Text = String.Format("{0:N0} of {1:N0}", nextPage.MinIndex, nextPage.MaxIndex);
  107. NextPage = nextPage;
  108. CurrentPage = nextPage;
  109. NextPaginationButton_Click?.Invoke(this, e);
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. ExLogger.LogException(ex, "");
  115. }
  116. }
  117. protected virtual void OnLastPaginationButton_Click(object? sender, EventArgs e)
  118. {
  119. try
  120. {
  121. if (Pages == null)
  122. {
  123. return;
  124. }
  125. if (Pages.Count == 0)
  126. {
  127. return;
  128. }
  129. PageIndex = Pages.Count - 1;
  130. Page? lastPage = Pages[PageIndex
  131. <details>
  132. <summary>英文:</summary>
  133. I want when the button in Pagination is clicked, the dropdown of the Combo Box is still opened, so I can see the result of the pagination in the items of the Combo Box and the dropdown closed when I selected item of Combo Box.
  134. My problem is when I click the buttons in Pagination, the dropdown of the Combo Box closed, so I can&#39;t see the result of the pagination.
  135. How to prevent dropdown of Combo Box collapse before item selected in C# Win Form?
  136. **CtechComboBoxPagination.cs**
  137. using static DiscountCard.View.CustomControl.CtechPagination;
  138. namespace DiscountCard.View.CustomControl
  139. {
  140. public partial class CtechComboBoxPagination : ComboBox
  141. {
  142. public CtechPagination? Pagination { get; set; }
  143. protected override CreateParams CreateParams
  144. {
  145. get
  146. {
  147. CreateParams cp = base.CreateParams;
  148. cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
  149. return cp;
  150. }
  151. }
  152. public CtechComboBoxPagination()
  153. {
  154. InitializeComponent();
  155. DoubleBuffered = true;
  156. DropDownStyle = ComboBoxStyle.DropDownList;
  157. DrawMode = DrawMode.OwnerDrawFixed;
  158. Font = new Font(&quot;Consolas&quot;, 11F, FontStyle.Regular, GraphicsUnit.Point);
  159. Pagination = new CtechPagination();
  160. Pagination.SetInitialize(Enumerable.Range(0, 100).AsQueryable(), newContent: true, PaginationPosition.LastPage);
  161. Pagination.FirstPaginationButton_Click += Pagination_FirstPaginationButton_Click;
  162. Pagination.PrevPaginationButton_Click += Pagination_PrevPaginationButton_Click;
  163. Pagination.NextPaginationButton_Click += Pagination_NextPaginationButton_Click;
  164. Pagination.LastPaginationButton_Click += Pagination_LastPaginationButton_Click;
  165. }
  166. private void Pagination_FirstPaginationButton_Click(object? sender, EventArgs e)
  167. {
  168. DroppedDown = true;
  169. }
  170. private void Pagination_PrevPaginationButton_Click(object? sender, EventArgs e)
  171. {
  172. DroppedDown = true;
  173. }
  174. private void Pagination_NextPaginationButton_Click(object? sender, EventArgs e)
  175. {
  176. DroppedDown = true;
  177. }
  178. private void Pagination_LastPaginationButton_Click(object? sender, EventArgs e)
  179. {
  180. DroppedDown = true;
  181. }
  182. protected override void OnDrawItem(DrawItemEventArgs e)
  183. {
  184. e.DrawBackground();
  185. if (e.Index &lt; 0 || e.Index &gt;= Items.Count)
  186. {
  187. return;
  188. }
  189. var item = Items[e.Index];
  190. String? text = item == null ? &quot;(null)&quot; : item.ToString();
  191. TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
  192. base.OnDrawItem(e);
  193. }
  194. protected override void OnDropDown(EventArgs e)
  195. {
  196. base.OnDropDown(e);
  197. if (Pagination == null)
  198. {
  199. return;
  200. }
  201. // for test
  202. if (Pagination.CurrentPage != null)
  203. {
  204. Items.Clear();
  205. for (int i = Pagination.CurrentPage.MinIndex; i &lt;= Pagination.CurrentPage.MaxIndex; i++)
  206. {
  207. Items.Add(i);
  208. }
  209. }
  210. Pagination.Margin = new Padding(0);
  211. Pagination.MinimumSize = new Size(this.Width, 30);
  212. ToolStripControlHost host = new ToolStripControlHost(Pagination);
  213. host.Padding = new Padding(0);
  214. ToolStripDropDown dropdown = new ToolStripDropDown();
  215. dropdown.Padding = new Padding(0);
  216. dropdown.Items.Add(host);
  217. if (this.Items.Count &gt; 0)
  218. {
  219. dropdown.Show(this, 0, 3 + this.Height + (this.Items.Count * this.ItemHeight));
  220. }
  221. }
  222. }
  223. }
  224. **CtechPagination.cs**
  225. using CardData.Ext.Mod.Logger;
  226. using DiscountCard.Ext.Mod.General;
  227. using DiscountCard.View.CustomControl.Pagination;
  228. namespace DiscountCard.View.CustomControl
  229. {
  230. public partial class CtechPagination : UserControl
  231. {
  232. public enum PaginationPosition
  233. {
  234. FirstPage,
  235. PreviousPage,
  236. NextPage,
  237. LastPage,
  238. CurrrentPage
  239. }
  240. public CtechPagination()
  241. {
  242. InitializeComponent();
  243. FirstPaginationButton.Click += OnFirstPaginationButton_Click;
  244. PrevPaginationButton.Click += OnPrevPaginationButton_Click;
  245. NextPaginationButton.Click += OnNextPaginationButton_Click;
  246. LastPaginationButton.Click += OnLastPaginationButton_Click;
  247. }
  248. public event EventHandler? FirstPaginationButton_Click;
  249. public event EventHandler? PrevPaginationButton_Click;
  250. public event EventHandler? NextPaginationButton_Click;
  251. public event EventHandler? LastPaginationButton_Click;
  252. protected virtual void OnFirstPaginationButton_Click(object? sender, EventArgs e)
  253. {
  254. try
  255. {
  256. if (Pages == null)
  257. {
  258. return;
  259. }
  260. if (Pages.Count == 0)
  261. {
  262. return;
  263. }
  264. PageIndex = 0;
  265. Page? firstPage = Pages[PageIndex];
  266. if (firstPage != null)
  267. {
  268. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, firstPage.MinIndex, firstPage.MaxIndex);
  269. FirstPage = firstPage;
  270. CurrentPage = firstPage;
  271. FirstPaginationButton_Click?.Invoke(this, e);
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. ExLogger.LogException(ex, &quot;&quot;);
  277. }
  278. }
  279. protected virtual void OnPrevPaginationButton_Click(object? sender, EventArgs e)
  280. {
  281. try
  282. {
  283. if (Pages == null)
  284. {
  285. return;
  286. }
  287. if (Pages.Count == 0)
  288. {
  289. return;
  290. }
  291. --PageIndex;
  292. if (PageIndex &lt; 0)
  293. {
  294. PageIndex = 0;
  295. }
  296. Page? prevPage = Pages[PageIndex];
  297. if (prevPage != null)
  298. {
  299. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, prevPage.MinIndex, prevPage.MaxIndex);
  300. PreviousPage = prevPage;
  301. CurrentPage = prevPage;
  302. PrevPaginationButton_Click?.Invoke(this, e);
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. ExLogger.LogException(ex, &quot;&quot;);
  308. }
  309. }
  310. protected virtual void OnNextPaginationButton_Click(object? sender, EventArgs e)
  311. {
  312. try
  313. {
  314. if (Pages == null)
  315. {
  316. return;
  317. }
  318. if (Pages.Count == 0)
  319. {
  320. return;
  321. }
  322. ++PageIndex;
  323. if (PageIndex &gt; Pages.Count - 1)
  324. {
  325. PageIndex = Pages.Count - 1;
  326. }
  327. Page? nextPage = Pages[PageIndex];
  328. if (nextPage != null)
  329. {
  330. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, nextPage.MinIndex, nextPage.MaxIndex);
  331. NextPage = nextPage;
  332. CurrentPage = nextPage;
  333. NextPaginationButton_Click?.Invoke(this, e);
  334. }
  335. }
  336. catch (Exception ex)
  337. {
  338. ExLogger.LogException(ex, &quot;&quot;);
  339. }
  340. }
  341. protected virtual void OnLastPaginationButton_Click(object? sender, EventArgs e)
  342. {
  343. try
  344. {
  345. if (Pages == null)
  346. {
  347. return;
  348. }
  349. if (Pages.Count == 0)
  350. {
  351. return;
  352. }
  353. PageIndex = Pages.Count - 1;
  354. Page? lastPage = Pages[PageIndex];
  355. if (lastPage != null)
  356. {
  357. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, lastPage.MinIndex, lastPage.MaxIndex);
  358. LastPage = lastPage;
  359. CurrentPage = lastPage;
  360. LastPaginationButton_Click?.Invoke(this, e);
  361. }
  362. }
  363. catch (Exception ex)
  364. {
  365. ExLogger.LogException(ex, &quot;&quot;);
  366. }
  367. }
  368. public void SetInitialize(IQueryable&lt;int&gt; source, bool newContent, PaginationPosition selectedPagination)
  369. {
  370. PageSize = Common.DEFAULT_PAGE_SIZE;
  371. TotalCount = source.Count();
  372. if (TotalCount &lt;= 0)
  373. {
  374. return;
  375. }
  376. TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
  377. if (TotalPages &lt;= 0)
  378. {
  379. return;
  380. }
  381. if (newContent)
  382. {
  383. Pages = new List&lt;Page&gt;();
  384. for (int i = 0; i &lt; TotalPages; i++)
  385. {
  386. IQueryable&lt;int&gt; indexes = (IQueryable&lt;int&gt;)source.Skip(i * PageSize).Take(PageSize);
  387. if (indexes != null)
  388. {
  389. Page page = new Page();
  390. page.MinIndex = 1 + indexes.Min();
  391. page.MaxIndex = 1 + indexes.Max();
  392. Pages.Add(page);
  393. }
  394. }
  395. }
  396. if (Pages != null)
  397. {
  398. switch (selectedPagination)
  399. {
  400. case PaginationPosition.FirstPage:
  401. if (newContent)
  402. {
  403. PageIndex = 0;
  404. }
  405. Page? firstPage = Pages[PageIndex];
  406. if (firstPage != null)
  407. {
  408. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, firstPage.MinIndex, firstPage.MaxIndex);
  409. FirstPage = firstPage;
  410. CurrentPage = firstPage;
  411. }
  412. break;
  413. case PaginationPosition.PreviousPage:
  414. if (newContent)
  415. {
  416. --PageIndex;
  417. }
  418. if (PageIndex &lt; 0)
  419. {
  420. PageIndex = 0;
  421. }
  422. Page? prevPage = Pages[PageIndex];
  423. if (prevPage != null)
  424. {
  425. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, prevPage.MinIndex, prevPage.MaxIndex);
  426. PreviousPage = prevPage;
  427. CurrentPage = prevPage;
  428. }
  429. break;
  430. case PaginationPosition.NextPage:
  431. if (newContent)
  432. {
  433. ++PageIndex;
  434. }
  435. if (PageIndex &gt; Pages.Count - 1)
  436. {
  437. PageIndex = Pages.Count - 1;
  438. }
  439. Page? nextPage = Pages[PageIndex];
  440. if (nextPage != null)
  441. {
  442. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, nextPage.MinIndex, nextPage.MaxIndex);
  443. NextPage = nextPage;
  444. CurrentPage = nextPage;
  445. }
  446. break;
  447. case PaginationPosition.LastPage:
  448. if (newContent)
  449. {
  450. PageIndex = Pages.Count - 1;
  451. }
  452. if (PageIndex &lt; 0)
  453. {
  454. PageIndex = 0;
  455. }
  456. Page? lastPage = Pages[PageIndex];
  457. if (lastPage != null)
  458. {
  459. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, lastPage.MinIndex, lastPage.MaxIndex);
  460. LastPage = lastPage;
  461. CurrentPage = lastPage;
  462. }
  463. break;
  464. case PaginationPosition.CurrrentPage:
  465. if (PageIndex &lt; 0)
  466. {
  467. PageIndex = 0;
  468. }
  469. if (PageIndex &gt; Pages.Count - 1)
  470. {
  471. PageIndex = Pages.Count - 1;
  472. }
  473. Page? curentPage = Pages[PageIndex];
  474. if (curentPage != null)
  475. {
  476. PagesLabel.Text = String.Format(&quot;{0:N0} of {1:N0}&quot;, curentPage.MinIndex, curentPage.MaxIndex);
  477. CurrentPage = curentPage;
  478. }
  479. break;
  480. }
  481. }
  482. }
  483. public int PageIndex { get; private set; }
  484. public int PageSize { get; private set; }
  485. public int TotalCount { get; private set; }
  486. public int TotalPages { get; private set; }
  487. public List&lt;Page&gt;? Pages { get; private set; }
  488. public Page? CurrentPage { get; private set; }
  489. public Page? FirstPage { get; private set; }
  490. public Page? PreviousPage { get; private set; }
  491. public Page? NextPage { get; private set; }
  492. public Page? LastPage { get; private set; }
  493. }
  494. }
  495. **CtechPagination.Designer.cs**
  496. namespace DiscountCard.View.CustomControl
  497. {
  498. partial class CtechPagination
  499. {
  500. /// &lt;summary&gt;
  501. /// Required designer variable.
  502. /// &lt;/summary&gt;
  503. private System.ComponentModel.IContainer components = null;
  504. /// &lt;summary&gt;
  505. /// Clean up any resources being used.
  506. /// &lt;/summary&gt;
  507. /// &lt;param name=&quot;disposing&quot;&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
  508. protected override void Dispose(bool disposing)
  509. {
  510. if (disposing &amp;&amp; (components != null))
  511. {
  512. components.Dispose();
  513. }
  514. base.Dispose(disposing);
  515. }
  516. #region Component Designer generated code
  517. /// &lt;summary&gt;
  518. /// Required method for Designer support - do not modify
  519. /// the contents of this method with the code editor.
  520. /// &lt;/summary&gt;
  521. private void InitializeComponent()
  522. {
  523. this.PaginationTableLayoutPanel = new DiscountCard.View.CustomControl.CtechTableLayoutPanel();
  524. this.FirstPaginationButton = new System.Windows.Forms.Button();
  525. this.PrevPaginationButton = new System.Windows.Forms.Button();
  526. this.NextPaginationButton = new System.Windows.Forms.Button();
  527. this.LastPaginationButton = new System.Windows.Forms.Button();
  528. this.PagesLabel = new System.Windows.Forms.Label();
  529. this.PaginationTableLayoutPanel.SuspendLayout();
  530. this.SuspendLayout();
  531. //
  532. // PaginationTableLayoutPanel
  533. //
  534. this.PaginationTableLayoutPanel.ColumnCount = 9;
  535. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
  536. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
  537. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
  538. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
  539. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
  540. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
  541. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
  542. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
  543. this.PaginationTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
  544. this.PaginationTableLayoutPanel.Controls.Add(this.FirstPaginationButton, 1, 0);
  545. this.PaginationTableLayoutPanel.Controls.Add(this.PrevPaginationButton, 3, 0);
  546. this.PaginationTableLayoutPanel.Controls.Add(this.NextPaginationButton, 5, 0);
  547. this.PaginationTableLayoutPanel.Controls.Add(this.LastPaginationButton, 7, 0);
  548. this.PaginationTableLayoutPanel.Controls.Add(this.PagesLabel, 4, 0);
  549. this.PaginationTableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
  550. this.PaginationTableLayoutPanel.Location = new System.Drawing.Point(0, 0);
  551. this.PaginationTableLayoutPanel.Name = &quot;PaginationTableLayoutPanel&quot;;
  552. this.PaginationTableLayoutPanel.RowCount = 1;
  553. this.PaginationTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
  554. this.PaginationTableLayoutPanel.Size = new System.Drawing.Size(816, 30);
  555. this.PaginationTableLayoutPanel.TabIndex = 0;
  556. //
  557. // FirstPaginationButton
  558. //
  559. this.FirstPaginationButton.Dock = System.Windows.Forms.DockStyle.Fill;
  560. this.FirstPaginationButton.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
  561. this.FirstPaginationButton.Location = new System.Drawing.Point(8, 3);
  562. this.FirstPaginationButton.Name = &quot;FirstPaginationButton&quot;;
  563. this.FirstPaginationButton.Size = new System.Drawing.Size(44, 24);
  564. this.FirstPaginationButton.TabIndex = 0;
  565. this.FirstPaginationButton.Text = &quot;⟨⟨&quot;;
  566. this.FirstPaginationButton.UseVisualStyleBackColor = true;
  567. //
  568. // PrevPaginationButton
  569. //
  570. this.PrevPaginationButton.Dock = System.Windows.Forms.DockStyle.Fill;
  571. this.PrevPaginationButton.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
  572. this.PrevPaginationButton.Location = new System.Drawing.Point(63, 3);
  573. this.PrevPaginationButton.Name = &quot;PrevPaginationButton&quot;;
  574. this.PrevPaginationButton.Size = new System.Drawing.Size(44, 24);
  575. this.PrevPaginationButton.TabIndex = 1;
  576. this.PrevPaginationButton.Text = &quot;&quot;;
  577. this.PrevPaginationButton.UseVisualStyleBackColor = true;
  578. //
  579. // NextPaginationButton
  580. //
  581. this.NextPaginationButton.Dock = System.Windows.Forms.DockStyle.Fill;
  582. this.NextPaginationButton.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
  583. this.NextPaginationButton.Location = new System.Drawing.Point(709, 3);
  584. this.NextPaginationButton.Name = &quot;NextPaginationButton&quot;;
  585. this.NextPaginationButton.Size = new System.Drawing.Size(44, 24);
  586. this.NextPaginationButton.TabIndex = 2;
  587. this.NextPaginationButton.Text = &quot;&quot;;
  588. this.NextPaginationButton.UseVisualStyleBackColor = true;
  589. //
  590. // LastPaginationButton
  591. //
  592. this.LastPaginationButton.Dock = System.Windows.Forms.DockStyle.Fill;
  593. this.LastPaginationButton.Font = new System.Drawing.Font(&quot;Segoe UI&quot;, 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
  594. this.LastPaginationButton.Location = new System.Drawing.Point(764, 3);
  595. this.LastPaginationButton.Name = &quot;LastPaginationButton&quot;;
  596. this.LastPaginationButton.Size = new System.Drawing.Size(44, 24);
  597. this.LastPaginationButton.TabIndex = 3;
  598. this.LastPaginationButton.Text = &quot;⟩⟩&quot;;
  599. this.LastPaginationButton.UseVisualStyleBackColor = true;
  600. //
  601. // PagesLabel
  602. //
  603. this.PagesLabel.AutoSize = true;
  604. this.PagesLabel.Dock = System.Windows.Forms.DockStyle.Fill;
  605. this.PagesLabel.Location = new System.Drawing.Point(113, 0);
  606. this.PagesLabel.Name = &quot;PagesLabel&quot;;
  607. this.PagesLabel.Size = new System.Drawing.Size(590, 30);
  608. this.PagesLabel.TabIndex = 4;
  609. this.PagesLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  610. //
  611. // CtechPagination
  612. //
  613. this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
  614. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  615. this.Controls.Add(this.PaginationTableLayoutPanel);
  616. this.Margin = new System.Windows.Forms.Padding(0);
  617. this.Name = &quot;CtechPagination&quot;;
  618. this.Size = new System.Drawing.Size(816, 30);
  619. this.PaginationTableLayoutPanel.ResumeLayout(false);
  620. this.PaginationTableLayoutPanel.PerformLayout();
  621. this.ResumeLayout(false);
  622. }
  623. #endregion
  624. private CtechTableLayoutPanel PaginationTableLayoutPanel;
  625. private Button FirstPaginationButton;
  626. private Button PrevPaginationButton;
  627. private Button NextPaginationButton;
  628. private Button LastPaginationButton;
  629. private Label PagesLabel;
  630. }
  631. }
  632. [![enter image description here](https://i.stack.imgur.com/bYQcK.png)](https://i.stack.imgur.com/bYQcK.png)
  633. [![enter image description here](https://i.stack.imgur.com/a7Xhc.png)](https://i.stack.imgur.com/a7Xhc.png)
  634. </details>
  635. # 答案1
  636. **得分**: 1
  637. 根据我的理解,您已经为分页制作了一个`UserControl`,当单击导航按钮之一时,会导致不希望的下拉关闭。
  638. 我能够重现您描述的行为,然后通过为用户控件实施[IMessageFilter](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.imessagefilter)来拦截鼠标事件并将它们标记为“已处理”来抑制它。
  639. ```csharp
  640. public partial class Pagination : UserControl, IMessageFilter
  641. {
  642. public Pagination()
  643. {
  644. InitializeComponent();
  645. Application.AddMessageFilter(this);
  646. Disposed += (sender, e) => Application.RemoveMessageFilter(this);
  647. PagesLabel.Text = $"Page {Page} of {Pages}";
  648. }
  649. const int WM_LBUTTONDOWN = 0x0201;
  650. const int WM_LBUTTONUP = 0x0202;
  651. public bool PreFilterMessage(ref Message m)
  652. {
  653. var client = PointToClient(MousePosition);
  654. switch (m.Msg)
  655. {
  656. case WM_LBUTTONDOWN:
  657. if (ClientRectangle.Contains(client))
  658. {
  659. IterateControlTree((control) => hitTest(control, client));
  660. return true;
  661. }
  662. break;
  663. case WM_LBUTTONUP:
  664. if (ClientRectangle.Contains(client))
  665. {
  666. return true;
  667. }
  668. break;
  669. }
  670. return false;
  671. }
  672. .
  673. .
  674. .
  675. }

要清楚,这意味着也不会发生Button.Click事件!因此,有必要迭代用户控件的Control树,以对其包含的按钮控件执行命中测试。

  1. bool IterateControlTree(Func<Control, bool> action, Control control = null)
  2. {
  3. if (control == null) control = this;
  4. if (action(control)) return true;
  5. foreach (Control child in control.Controls)
  6. {
  7. if (IterateControlTree(action, child))
  8. {
  9. return true;
  10. }
  11. }
  12. return false;
  13. }

例如,检测在其中一个按钮的客户端矩形内的点击可以修改Page属性,从而触发组合框对事件的响应。

  1. private bool hitTest(Control control, Point client)
  2. {
  3. if (control.Bounds.Contains(client))
  4. {
  5. switch (control.Name)
  6. {
  7. case nameof(FirstPaginationButton): Page = 1; return true;
  8. case nameof(PrevPaginationButton): Page--; return true;
  9. case nameof(NextPaginationButton): Page++; return true;
  10. case nameof(LastPaginationButton): Page = Pages; return true;
  11. default: break;
  12. }
  13. }
  14. return false;
  15. }

如果有帮助,可以随时浏览或克隆我编写的代码来测试这个答案。

英文:

As I understand it, you've made a UserControl for the pagination and when you click on one of the nav buttons it results in an unwanted close of the drop down.

阻止组合框在选择项目之前折叠下拉菜单

I was able to reproduce the behavior you're describing and then was able to suppress it by implementing IMessageFilter for the user control in order to intercept the mouse events and mark them as "handled" in the filter (if they occur inside the client rectangle of the UC).

阻止组合框在选择项目之前折叠下拉菜单

public partial class Pagination : UserControl, IMessageFilter
{
public Pagination()
{
InitializeComponent();
Application.AddMessageFilter(this);
Disposed += (sender, e) =&gt;Application.RemoveMessageFilter(this);
PagesLabel.Text = $&quot;Page {Page} of {Pages}&quot;;
}
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
public bool PreFilterMessage(ref Message m)
{
var client = PointToClient(MousePosition);
switch (m.Msg)
{
case WM_LBUTTONDOWN:
if (ClientRectangle.Contains(client))
{
IterateControlTree((control) =&gt; hitTest(control, client));
return true;
}
break;
case WM_LBUTTONUP:
if(ClientRectangle.Contains(client))
{
return true;
}
break;
}
return false;
}
.
.
.
}

To be clear, this means no Button.Click events are going to take place either! For this reason it's necessary to iterate the Control tree of the user control to perform a hit test on the button controls it contains.

bool IterateControlTree(Func&lt;Control, bool&gt; action, Control control = null)
{
if (control == null) control = this;
if(action(control)) return true;
foreach (Control child in control.Controls)
{
if(IterateControlTree(action, child))
{
return true;
}
}
return false;
}

For example, detecting a click in the client rectangle of one of the buttons could modify a Page property which in turn fires an event for the combo box to react.

private bool hitTest(Control control, Point client)
{
if (control.Bounds.Contains(client))
{
switch (control.Name)
{
case nameof(FirstPaginationButton): Page = 1; return true;
case nameof(PrevPaginationButton): Page --; return true;
case nameof(NextPaginationButton): Page ++; return true;
case nameof(LastPaginationButton): Page = Pages; return true;
default: break;
}
}
return false;
}

Feel free to browse or clone the code I wrote to test this answer if that would be helpful.

huangapple
  • 本文由 发表于 2023年2月18日 22:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定