Question Do several label inherit numbers from other label and textBox?

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
I'm trying to retrieve 12 label and 3 textBox numbers and put in another 15 label, I tried with array, but I don't really get into arrays yet, I would like if anyone can help me, I'm very grateful, I'll leave an Ex:

I'm using this on the button to generate 12 numbers on the label from Class Aleatorios

C#:
private void BtnGeraAleatorios_Click(object sender, EventArgs e)

        {
            Aleatorios ale = new Aleatorios();

            int[] numerosSorteados = ale.GeradorNaoRepetidoss(12, 1, 99);

            Array.Sort(numerosSorteados);

            Control[] controles = { lblAle1, lblAle2, lblAle3, lblAle4, lblAle5, lblAle6, lblAle7, lblAle8, lblAle9, lblAle10, lblAle11, lblAle12 };


                  //if (lblAle1.Text.Length != 0)

                 //{

                 //    int[] vetor = label2.Text.ToCharArray().Cast<int>().ToArray();

                //}


            for (int i = 0; i < controles.Length; i++)

            {
                int valor = numerosSorteados[i];

                controles[i].Text = valor + "";
}

then there are 3 textBox that will be filled by the user with numbers of their choice, thus generating a total of 15 numbers from 1 to 99, I would like these 15 numbers (12 Label and 3 TextBox to appear elsewhere on the page or in 15 label or an array, I confess that I have no idea how to do this, I've already looked for a way but I couldn't,

if anyone is willing to help me i would appreciate it.
 
Label controls are a means to display data, not to store it. You should NEVER be getting data from a Label. The data that the Label is displaying must have come from somewhere in the first place so, if you need to use that data elsewhere, you should be getting it from that same place. That might be an array or whatever. That means that, in your case, you should probably already have an array of 12 int values and that should be your source for the first set of Labels, so you can use it as the source for a second set of Labels in the same way. In that case, your only real task is reading data from three TextBoxes and converting it to numbers to display in another set of Labels.
 
With regard to arrays, I always recommend that they be thought of like egg cartons. They are a fixed size and each element is empty by default. Just as you can add and remove eggs in an egg carton without changing the carton itself, so you can add an remove values in an array without changing the array itself. An array is basically just a group of variables so you can treat each element you would a distinct variable.
 
Label controls are a means to display data, not to store it. You should NEVER be getting data from a Label. The data that the Label is displaying must have come from somewhere in the first place so, if you need to use that data elsewhere, you should be getting it from that same place. That might be an array or whatever. That means that, in your case, you should probably already have an array of 12 int values and that should be your source for the first set of Labels, so you can use it as the source for a second set of Labels in the same way. In that case, your only real task is reading data from three TextBoxes and converting it to numbers to display in another set of Labels.
here's my problem, I'm not able to get data from two arrays at the same time (label and TextBox), to form the set of 15 values, I'm new to C# and I'm not finding myself in this part, if you can help me, I'm grateful.
 
Why would you need to do it at the same time? It's not one operation so don't treat it as one operation. You have an array of 12 numbers somewhere. You display those 12 numbers in 12 Labels. Later on, you display the same 12 numbers in another 12 Labels. You also get 3 numbers from 3 TextBoxes and display those in 3 other Labels. These are all distinct operations.

This is a perfect example of the main reason that beginners get themselves into trouble. They look at a multi-step process as a single operation and either expect to find the exact code required to perform these multiple steps or else blat out a chunk of code to do it in one go. That's not how software development works. You need to break a problem down into its smallest constituent parts and then address each part individually. Each part will be quite simple and thus not to hard to figure out, or you can get specific help with the specific parts you are having trouble with. Once you have addressed each separate part, you put the solutions together and you have inherently solved the final problem.

So, forget the TextBoxes for now. Where are you getting the data that populates the first 12 Labels? That's the place to start. You need to put that data somewhere that it can be accessed in the future, e.g. an array assign to a field. Do that first. You can then load that data from that store into the first 12 Labels. Later, you can load the same data from the same store into the second 12 Labels in exactly the same way. That's half the problem solved and you already have the solution, because you can already populate the first 12 Labels and the second 12 are populated in exactly the same way. Only once that part is done and working should you even think about the TextBoxes.

So, what is the actual problem here? Show us your data store and how you're populating the first 12 Labels from it.
 
Thank you so much for replying, I understand what you mean, so come on, why do I want to take the generated 12 labels and move to other labels? because I'm generating 12 random label (Tens from 01 to 99) with the click of the button, this is working perfectly.

however, I need to get the tens as they were generated on the first button, if I generate it again on the second button they will come different, I need to capture the tens as they were generated and add the other 3 tens of texttbox forming 15 tens,

that's what I'm not able to do: capture 12 dozen of the labels and add another 3 dozen that will be typed by the user in the textbox and form a matrix of 15 dozen...
 
C#:
 //button that generates the random numbers on the labels

        private void BtnGeraAleatorios_Click(object sender, EventArgs e)
        {

            Aleatorios ale = new Aleatorios();
            int[] numerosSorteados = ale.GeradorNaoRepetidoss(12, 1, 99);
            Array.Sort(numerosSorteados);
            Control[] controles = { lblAle1, lblAle2, lblAle3, lblAle4, lblAle5, lblAle6, lblAle7, lblAle8, lblAle9, lblAle10, lblAle11, lblAle12 };

            for (int i = 0; i < controles.Length; i++)
            {

                int valor = numerosSorteados[i];
                controles[i].Text = valor + "";

            }
        }


   //class that generates the numbers

    public class Aleatorios
    {
        int[] numeros;
        private Random _random;
        public Aleatorios()
        {
            this._random = new Random();
        }
        //generates random numbers
        public int GeradorAleatorio(int min, int max)
        {
            if (min > max)
            {
                int aux = min;
                min = max;
                max = aux;
            }
            return this._random.Next(min, max + 1);
        }
        public int[] GeradorAleatorios(int longetud, int min, int max)
        {
            if (longetud <= 0)
            {
                return null;
            }
            int[] numeros = new int[longetud];

            for (int i = 0; i < numeros.Length; i++)
            {
                numeros[i] = GeradorAleatorio(min, max);
            }
            return numeros;
        }
        //generates non-repeating random numbers
        public int[] GeradorNaoRepetidoss(int longetud, int min, int max)
        {
            if (min > max)
            {
                int aux = min;
                min = max;
                max = aux;
            }


            if (longetud <= 0 || (max - min) < longetud - 1)
            {
                return null;
            }
            int[] numeros = new int[longetud];

            bool repetido;
            int numero;
            int indice = 0;

            while (indice < numeros.Length)
            {
                repetido = false;
                numero = GeradorAleatorio(min, max);

                for (int i = 0; i < indice; i++)
                {
                    if (numeros[i] == numero)
                    {
                        repetido = true;
                    }
                }

                if (!repetido)
                {
                    numeros[indice] = numero;
                    indice++;
                }
            }

            return numeros;
        }
        //ascending order
        public int[] Ordenar()
        {
            for (int i = 0; i < numeros.Length - 1; i++)
            {
                for (int j = i + 1; j < numeros.Length; j++)
                {
                    if (numeros[i] > numeros[j])
                    {
                        int aux = numeros[i];
                        numeros[i] = numeros[j];
                        numeros[j] = aux;
                    }
                }
            }
            return numeros;
        }
     
    }
 
Last edited:
It's not the 1980's anymore where controls hold values and you try to shuffle things around between the controls. Programmers in the mid 90s have since learned that it's a really bad idea and have moved on to using one of the MV* design patterns where data is stored in the Model (the M in MV), and the only job of the View (the V in MV) is to provide the display and a way for the user to interact with the Model. It's 2021 now. You're still programming stuff as if it were the 80's.
 
Thank you so much for replying, I understand what you mean, so come on, why do I want to take the generated 12 labels and move to other labels? because I'm generating 12 random label (Tens from 01 to 99) with the click of the button, this is working perfectly.

however, I need to get the tens as they were generated on the first button, if I generate it again on the second button they will come different, I need to capture the tens as they were generated and add the other 3 tens of texttbox forming 15 tens,

that's what I'm not able to do: capture 12 dozen of the labels and add another 3 dozen that will be typed by the user in the textbox and form a matrix of 15 dozen...
You're not listening. Read the words this time. DO NOT put the data into the Label controls and then try to get it out again later. Do what I have already told you to do multiple times. Generate the numbers once and put them into an array. You can then get the data from that array as many times as you like. Do it once to populate the first set of Labels and then do it again later to populate the second set of Labels. It's exactly the same operation so it is done exactly the same way both times, just with a different set of Labels. If you're doing the same thing multiple times, that's a perfect opportunity to write a method that performs the action and then call it multiple times. Whatever changes each time - the Labels, in this case - is what you pass in as arguments.
 
In other words instead of just storing these values in a local variable:
C#:
int[] numerosSorteados = ale.GeradorNaoRepetidoss(12, 1, 99);
Also put them into your data model. Later when you need to render the other labels, pull the values from your data model and stuff into your view's labels.
 
Back
Top Bottom