Listbox - how to show full product name and bring up another column of values

Asked

Viewed 569 times

3

I have this ListBox.

inserir a descrição da imagem aqui

When loaded he wears one txt file being like this:

inserir a descrição da imagem aqui Code to load the ListBox:

private void frmOrdemServico_Load(object sender, EventArgs e)
    {
        string[] d = File.ReadAllLines(@"C:\\Users\\willian\\Downloads\\dbProdutos.txt");
        foreach (var line in d)
        {
            string[] produtos = line.Split(' ');
            lbProdutos.Items.Add(produtos[0]);
        }
    }

I have 2 problems:

first The names of the products are not complete because where there is space is cut: example: Liquid soap, stay: Soap only. [solved]

2nd I do not know how to bring in another column inside the Listbox the values of the products, because I intend to use them. [solved]

My file txt is in this shape:

inserir a descrição da imagem aqui

disregard data only call structure

Note: I read a little on the internet about Listview but not I was able to implement it because I couldn’t pass data on one side to another etc. And bring the two columns finally..

  • 1

    It seems your field separator is the -, soon make Split for - would already solve the problem of cutting words

  • Truth, solved this problem, I didn’t even notice it, I was giving space, because I thought he took the space of words..

3 answers

1

If you have the option to use ListView, then I guess I’d better use it in that case.

The code below shows how you can move items from a listview to another.

I left the option MultiSelect = true to allow moving multiple items between Listviews.

void frmOrdemServico_Load(object sender, EventArgs e)
{
    lvwProdutos.Columns.Add("Produto",100,HorizontalAlignment.Left);
    lvwProdutos.Columns.Add("Valor",60,HorizontalAlignment.Right);
    lvwProdutos.View = View.Details;
    lvwProdutos.FullRowSelect = true;
    lvwProdutos.MultiSelect = true;

    lvwProdutosUsados.Columns.Add("Produto",100,HorizontalAlignment.Left);
    lvwProdutosUsados.Columns.Add("Valor",60,HorizontalAlignment.Right);
    lvwProdutosUsados.View = View.Details;
    lvwProdutosUsados.FullRowSelect = true;
    lvwProdutosUsados.MultiSelect = true;

    string[] d = File.ReadAllLines(@"C:\desenv\dbProdutos.txt");
    foreach (var line in d)
    {
        string[] produtos = line.Split('-');

        var item = new ListViewItem();
        item.Text = produtos[0];
        item.SubItems.Add(produtos[1]);

        lvwProdutos.Items.Add(item);
    }
}


void btnAdicionar_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in lvwProdutos.SelectedItems)
    {               
        lvwProdutos.Items.Remove(item);
        lvwProdutosUsados.Items.Add(item);
    }   
}


void btnRemover_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in lvwProdutosUsados.SelectedItems)
    {               
        lvwProdutosUsados.Items.Remove(item);
        lvwProdutos.Items.Add(item);
    }       
}       
  • Thanks man, that’s just what I needed, I even had it with the ListBox but I will not be able to do another implementation that I need.. Because I need when I go to the other ListView call a Forms with a txt field quantidade and multiply, alias if you know that too xD helps me now. Thanks!!

1


For knowledge purposes, I was able to implement using the ListBox also, in this way:

private void frmOrdemServico_Load(object sender, EventArgs e)
        {


            string[] lineOfContents = File.ReadAllLines(@"C:\\Users\\willian\\Downloads\\dbClientes.txt");
            cbClientes.Items.Clear(); // limpar para não duplicar valores
            foreach (var line in lineOfContents)
            {
                string[] nomes = line.Split(',');
                cbClientes.Items.Add(nomes[0]);
            }

            string[] d = File.ReadAllLines(@"C:\\Users\\willian\\Downloads\\dbProdutos.txt");
            foreach (var line in d)
            {
                string[] produtos = line.Split(';');
                lbProdutos.Items.Add(produtos[0] + " R$" + Convert.ToDouble(produtos[1]));
            }
        }

Amended line: lbProdutos.Items.Add(produtos[0] + " R$" + Convert.ToDouble(produtos[1]));

This way I was able to bring the other value that was in txt file.

-1

Browser other questions tagged

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