{"id":1230,"date":"2013-11-19T13:47:18","date_gmt":"2013-11-19T11:47:18","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1230"},"modified":"2013-11-19T14:18:18","modified_gmt":"2013-11-19T12:18:18","slug":"listviewex-auto-resize-selected-column","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2013\/11\/listviewex-auto-resize-selected-column\/","title":{"rendered":"ListViewEx &#8211; Auto resize selected column"},"content":{"rendered":"<p>What do you do when you get fed up having to write the same code over and over again? Write a base class for it&#8230; of course.<\/p>\n<p>I regularly use the WinForms ListView control and often want to specify a specific column (in detail view) that must auto resize when the whole control resize. Therefore I created the following class that does all that (and more stuff) to make life easier.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class ListViewEx : ListView\r\n    {\r\n        public ListViewEx()\r\n            : base()\r\n        {\r\n            DoubleBuffered = true;\r\n            View = View.Details;\r\n            resizeTimer.Tick +=  resizeTimer_Tick;\r\n        }\r\n\r\n        &#x5B;Description(&quot;Use one column to auto resize in detail view&quot;)]\r\n        public bool AutoResizeColumnEnabled { get; set; }\r\n        &#x5B;Description(&quot;Column index of auto resize column&quot;)]\r\n        public int AutoResizeColumnIndex { get; set; }\r\n        public event MethodInvoker EnterKeyPressed;\r\n        public event MethodInvoker DeleteKeyPressed;\r\n        protected override void OnKeyPress(KeyPressEventArgs e)\r\n        {\r\n            if (e.KeyChar == '\\r')\r\n                if (EnterKeyPressed != null)\r\n                {\r\n                    EnterKeyPressed();\r\n                    e.Handled = true;\r\n                }\r\n            base.OnKeyPress(e);\r\n        }\r\n        protected override void OnKeyDown(KeyEventArgs e)\r\n        {\r\n            if (e.KeyCode == Keys.Delete)\r\n                if (DeleteKeyPressed != null)\r\n                {\r\n                    DeleteKeyPressed();\r\n                    e.Handled = true;\r\n                }\r\n            base.OnKeyDown(e);\r\n        }\r\n        private Timer resizeTimer = new Timer() { Interval = 100, Enabled = false };\r\n        private void resizeTimer_Tick(object sender, EventArgs e)\r\n        {\r\n            resizeTimer.Enabled = false;\r\n            try\r\n            {\r\n                if (AutoResizeColumnEnabled &amp;&amp; View == System.Windows.Forms.View.Details &amp;&amp;\r\n                    AutoResizeColumnIndex &gt; -1 &amp;&amp; this.Columns.Count &gt; AutoResizeColumnIndex)\r\n                {\r\n                    int columnsWidth = 0;\r\n                    Application.DoEvents();\r\n                    for (int i = 0; i &lt; this.Columns.Count; i++)\r\n                    {\r\n                        if (i != AutoResizeColumnIndex)\r\n                            columnsWidth += this.Columns&#x5B;i].Width;\r\n                    }\r\n                    this.Columns&#x5B;AutoResizeColumnIndex].Width = this.ClientSize.Width - columnsWidth - 2;\r\n                }\r\n            }\r\n            catch { }\r\n        }\r\n        protected override void OnResize(EventArgs e)\r\n        {\r\n            resizeTimer.Enabled = false;\r\n            resizeTimer.Enabled = true;\r\n            base.OnResize(e);\r\n        }\r\n    }\r\n<\/pre>\n<p>Now all you have to do is set the AutoResizeColumnEnabled property to true and specify a column index for AutoResizeColumnIndex and there you go. Easy hey?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What do you do when you get fed up having to write the same code over and over again? Write a base class for it&#8230; of course. I regularly use the WinForms ListView control and often want to specify a &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2013\/11\/listviewex-auto-resize-selected-column\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,12,303,11,10],"class_list":["post-1230","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-control","tag-development","tag-listview","tag-winforms"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1230\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}