How to increase column width in Listbox C#

Asked

Viewed 271 times

0

Good morning, I am a beginner in C# I have a doubt, I already searched the forum and found the solution that did not work for me.

Problem: I can’t enlarge the Listbox Column.

COD:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Trabalhodoprof.daniel2
{
    public partial class Form1 : Form
    {
        public string recebo;
        public Form1()
        {
            InitializeComponent();

        }
        #region Botão cadastrar
        private void btnCadastrar_Click(object sender, EventArgs e)
        {


            ClassProdutos produto = new ClassProdutos();
            classValidacao objValidacao = new classValidacao();

            if(txtValor.Text == "")
            {
                txtValor.Text = "0";
            }
            if (txtQuantidade.Text == "")
            {
                txtQuantidade.Text = "0";
            }
            if (txtEstoque.Text == "")
            {
                txtEstoque.Text = "0";
            }
            produto.nome = txtNome.Text;
            produto.valor = Convert.ToDouble(txtValor.Text);
            produto.quantidade = Convert.ToInt32(txtQuantidade.Text);
            produto.estoque = Convert.ToInt32(txtEstoque.Text);
            produto.status = cbbStatus.Text;


            MessageBox.Show(objValidacao.Validador(produto.valor));
            if(produto.valor > 0) {
                if (!string.IsNullOrWhiteSpace(txtNome.Text))
                {
                    lstCadastro.Items.Add(produto.nome);
                    lstCadastro.Items.Add(Convert.ToString(produto.valor));
                    lstCadastro.Items.Add(Convert.ToString(produto.quantidade));
                    lstCadastro.Items.Add(Convert.ToString(produto.estoque));
                    lstCadastro.Items.Add(produto.status);
                }
                else
                {
                    MessageBox.Show("Preencha os campos", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                }

            }
            txtNome.Clear();
            txtValor.Clear();
            txtQuantidade.Clear();
            txtEstoque.Clear();
            txtEstoque.Clear();
            txtNome.Focus();
            #endregion

            #region Botão Sair

            lstCadastro.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            lstCadastro.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Deseja sair da aplicação?", "Sair",MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        #endregion

        private void txtValor_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtValor_Leave(object sender, EventArgs e)
        {
            bool sair = true;
            if (string.IsNullOrWhiteSpace(txtValor.Text) && sair == true)
            {
                MessageBox.Show("Preenchimento de campo obrigatório ");
                txtValor.Focus();
                sair = false;
            }
        }

        private void txtValor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Char.IsLetter(e.KeyChar)) || (Char.IsWhiteSpace(e.KeyChar)))
                e.Handled = true;
        }

        private void txtQuantidade_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Char.IsLetter(e.KeyChar)) || (Char.IsWhiteSpace(e.KeyChar)))
                e.Handled = true;
        }

        private void txtEstoque_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Char.IsLetter(e.KeyChar)) || (Char.IsWhiteSpace(e.KeyChar)))
                e.Handled = true;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

inserir a descrição da imagem aqui

  • According to the code, your columns are being auto-resized according to their content and header. Probably, if you have text in these cells, the width will be resized.

  • "depending on your content and header" I’m sorry my ignorance, but I didn’t really understand, could you please give me some example a little more practical.

  • I meant the line lstCadastro.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);, which indicates that the columns will be resized according to the value of the cells.

  • as the code I placed lstCadastro.Autoresizecolumns(Columnheaderautoresizestyle.Columncontent); ,why was it not resized ?.

  • Tried to change the 'Autoresizecolumns' property after adding listview content.

1 answer

0


Try assigning value to property AutoResizeColumns after adding listview content.

ColumnHeader header1, header2;
header1 = new ColumnHeader();
header2 = new ColumnHeader();

header1.Text = "Id";
header1.TextAlign = HorizontalAlignment.Left;

header2.TextAlign = HorizontalAlignment.Left;
header2.Text = "Nome";

listView1.Columns.Add(header1);
listView1.Columns.Add(header2);

listView1.View = View.Details;

string[] row = { "10", "foo" };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);

listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.