Print a list according to the number of labels

Asked

Viewed 62 times

2

My C# Console Application has a list of 7 items in all, and you need to print these items on a label. By default each label fits 5 items. I’m not able to print the items according to the amount of labels.

So what I did (very simply) was:

int nrEtiquetas = Lista.Count / 5

for(int contador = 0; contador <= nrEtiquetas; contador ++)
{
  for(int i = 0; i <= 5; i++)
  {
    MessageBox.Show("Item: " i.ToString())
  }
}
EnviaParaImpressora();

Meanwhile the imprint is on loop and the two remaining items are not displayed.

  • It seems to me a college job, your problem is not exactly in code but in logic as I understand it... I will not solve the exercise for you but to give you a help, use the following to find the amount of labels: int nrEtiquetas = (int)Math.Ceiling((double)Lista.Count / 5); the way it was, it would result in 1.

  • Other than that, take a good look at your loops (for), the logic is wrong :)

  • Hi George, no... I would really like to be enrolled in a college :( I am just studying by video-classes of udemy. The Math.Ceiling() method always rounds the decimal places to the next value?

  • Yeah, round it up. Start there and try to see where you’re going wrong with the rest, if I just answer here to you I won’t help you with pretty much anything, you won’t learn. Try to structure your problem well you arrive at the expected result :)

  • Ahh, beauty! thank you very much... this method I did not know in depth. It has given me a light! I will hit more head here kkk :)

1 answer

1


As comments, start by correcting the division.

Using Math.Ceiling you can divide by rounding up:

int nrEtiquetas = (int)Math.Ceiling((double)Lista.Count / 5);

The code so far would look like this and doesn’t change much of yours, except for the fact that you should use the < and not the <=. In addition, I created a fictitious list of string as if it were your labels.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<string> Lista = new List<string>{
            "Etiqueta 1",
            "Etiqueta 2",
            "Etiqueta 3",
            "Etiqueta 4",
            "Etiqueta 5",
            "Etiqueta 6",
            "Etiqueta 7",
        };

        int nrEtiquetas = (int)Math.Ceiling((double)Lista.Count / 5);

        for(int quantidade = 0; quantidade < nrEtiquetas; quantidade ++)
        {

            //Aqui você precisa corrigir seu loop, a lógica dele está errada.

            EnviaParaImpressora();
        }
    }

    public static void EnviaParaImpressora()
    {
        Console.WriteLine("Enviado para impressora...");
        Console.WriteLine("");
    }
}
  • Hello George... good evening! I followed your example :) I set the variable nrEticks using the method Celing, then I started to test. In a list of 7 items it would be necessary 2 labels... Soon I used the following excerpt: for(int cont = 0; cont < nrEtiquetas; cont++) &#xA;{&#xA; for(int i = 0; i <= 5; i++)&#xA; {&#xA; Console.WriteLine("O item " + i.ToString()+ " foi impresso.");&#xA; }&#xA;EnviaParaImpressora();&#xA;}

  • At the end of the first loop in the repeat loop of the items, the first loop is executed again for where it would be necessary to display on the screen the 2 items that were not printed the first time, but it turns out that the list is again listed of the first item

  • @Noob, I didn’t post the excerpt here just so you can break your head a little bit, the logic that sent me up doesn’t really do what you want... the first time you pass you have to show the item 1 through 5, the second of 6 through 10... in this case there is no up to 10, then on Monday he must print as far as there is

  • @Noob, I have an example code ready here, if you no longer want to break your head in logic I command you, but it is important you try to understand at what point your logic failed.

  • Hello George, I tried to do it this way: storing the index of the last item that the second one went through, so when the first loop of repetition passes to the second label, pass as parameter the stored input, but even so I kept having error pq the Indice is = 10 and the for will occur when the Indice is < 10 :(

  • Take a look at the code I made, try to understand what’s happening in it: https://dotnetfiddle.net/s5agND

  • Thank you George! It worked! Sorry for the delay in responding because I was a little frustrated with kkk programming

  • @Noob don’t get frustrated, our world isn’t perfect but it’s pretty fun! :P

Show 3 more comments

Browser other questions tagged

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