Question

I am trying to resize evenly all the columns of a ListView and it works as it should, but the code I made is making the screen flash and blink too much...I wonder what may I fix on the code to make it run smoothly? Here is the code:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace Test_ListViews
{
    public partial class Form1 : Form
    {
        Double[] pesos;
        private bool resizing = false;
        private bool limpando = false; 

        public Form1()
        {
            InitializeComponent();

            int i = 0;

            float dx = 96;

            Graphics g = this.CreateGraphics();
            try
            {
                dx = g.DpiX;
            }
            finally
            {
                g.Dispose();
            }

            pesos = new Double[listView1.Columns.Count]; // usado para o resize das colunas da ListView ser proporcional.
            for (i = 0; i < listView1.Columns.Count; i++)
                pesos[i] = ((Double)listView1.Columns[i].Width * dx) / (listView1.Width * 96);
            _listView1_Resize();

            listView1.FullRowSelect = true;

            this.listView1.Resize += new System.EventHandler(this.listView1_Resize);
            this.listView1.ColumnWidthChanged += new System.Windows.Forms.ColumnWidthChangedEventHandler(this.listView1_ColumnWidthChanged);   
        }

        private void bntFill_Click(object sender, EventArgs e)
        {
            int i = 0;

            for (i = 0; i < 5; i++)
            {
                ListViewItem item = new ListViewItem("Test 1");
                item.SubItems.Add("Test 2");
                item.SubItems.Add("Test 3");
                item.SubItems.Add("Test 4");
                item.SubItems.Add("Test 5");
                item.SubItems.Add("Test 6");
                item.SubItems.Add("Test 7");
                item.SubItems.Add("Test 8");
                item.SubItems.Add("Test 9");
                listView1.Items.Add(item);
            }            

            SetWindowTheme(listView1.Handle, "Explorer", null);
        }

        [DllImport("uxtheme.dll")]
        public static extern int SetWindowTheme([In] IntPtr hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubAppName, [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubIdList);

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();

            foreach(ListViewItem item in listView1.SelectedItems )
            {
                list.Add(item);
            }

            foreach (ListViewItem item in list)
            {
                listView1.Items.Remove(item);
            }
        }

        private void listView1_Resize(object sender, System.EventArgs e)
        {
            _listView1_Resize();
        }
        private void _listView1_Resize()
        {
            if (resizing == false && pesos != null)
            {
                resizing = true;

                Int32 largura = listView1.Width;
                int i = 0;


                for (i = 0; i < listView1.Columns.Count; i++)
                {
                    listView1.Columns[i].Width = Convert.ToInt32(pesos[i] * largura);
                }

                if (listView1.Controls.Count > 0)
                {
                    Int32 x = listView1.Items[0].SubItems[listView1.Items[0].SubItems.Count - 1].Bounds.Location.X + 3;//pegando a referencia da ultima coluna.

                    for (i = 0; i < listView1.Controls.Count; i++)
                    {
                        listView1.Controls[i].Location = new System.Drawing.Point(x, listView1.Controls[i].Location.Y);
                        listView1.Controls[i].Width = listView1.Columns[listView1.Columns.Count - 1].Width - 9;
                    }
                }

                if (listView1.Items.Count > 8)
                {
                    listView1.Columns[listView1.Columns.Count - 1].Width -= 10;
                }

                listView1.Scrollable = false;
                listView1.Scrollable = true;

                resizing = false;
            }

            SetWindowTheme(listView1.Handle, "Explorer", null);

        }

        private void listView1_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            _listView1_Resize();
            //int i = 0;
            //for (i = 0; i < listView1.Columns.Count; i++)
            //{
            //    if (listView1.Columns[i].Width < 20)
            //        listView1.Columns[i].Width = 20;
            //} 
        }

    }     
}

Thanks!!

Was it helpful?

Solution

At the start of the resizing operation call listView1.BeginUpdate() and then call listView1.EndUpdate() when you're finished

This way windows won't redraw the control every time a change is made to it, or any of it's children and will redraw them all once when you call EndUpdate()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top