Add N items to a Numericupdown-based Textbox

Asked

Viewed 379 times

2

I created a program that generates passwords, but only generates one at a time.

What I wanted was to be able to provide NumericUpDown so that the user could choose how many passwords or, which would then be presented in a Textbox.

How can I do that?

The code to my button is this:

inserir a descrição da imagem aqui

  • and what’s the problem?

  • @Andrade the problem is that I don’t know the code to do this. I’ve looked, and I can’t find.

  • use a combobox(dropdownlist I don’t know how you know it) with the number quantity, then you just check which quantity you choose and run the function n times.

  • Amigo @Marciano.Andrade I know the Dropdownlist, but I don’t know what code I have to use. Can you help me?

  • Okay, I can, I already have an answer.

  • Thanks friend @Marciano.Andrade , I will be waiting.

Show 1 more comment

1 answer

2


You can do it as follows:

// No clique de um botão, ou num outro evento que queira
private void button1_Click(object sender, System.EventArgs e)
{
    // Remove as passwords que existirem na ListBox (caso as deseja manter remova a linha seguinte.
    listBox1.Items.Clear();

    // Loop ate ao numero seleccionado no numericupdown 
    for (int i = 0; i < numericUpDown1.Value; ++i)
    {
        // A cada iteraccao gere uma password nova
        var password = //seu código de gerar passwords;

        // Por fim adicione a nova password a ListBox.
        listBox1.Items.Add(password);
    }
}

The final result will be (using GUIDs as passwords):

Gerar N passwords para uma ListBox

Edit:

Taking into account your specific case:

if(...)
{
}
else
{
    CopyButton.Enabled = true;
    SaveButton.Enabled = true;

    // Modifique aqui para o seguinte codigo
    listBox1.Items.Clear();

    // Loop ate ao numero seleccionado no numericupdown 
    for (int i = 0; i < numericUpDown1.Value; ++i)
    {
        // A cada iteraccao gere uma password nova
        var password = Generator.GetRandomPassword(Convert.ToInt32(PasswordLengthList.Value), GetStringTemplate());

        // Por fim adicione a nova password a ListBox.
        listBox1.Items.Add(password);
    }
}

The way you currently have it printing the generated password for a textbox. You have to add a listbox and use the code above.

  • Amigo @Omni, I already have the code to generate password’s, but is only to create 1 whenever you touch the button. What I want is: If utliziador wants 2 passwords, selects in Numericupdown "2", presses button, and in textbox appear then 2 passwords.

  • Put the code that is in the event click your button.

  • You got it, buddy.

  • I edited the answer with your specific case.

  • So you are not doing anything friend... What is the "var password" for if I already have the code to generate password? You want me to send you the project?

  • Do not understand, read the comment in front of var password =. Swap this var password by your password to generate passwords.

  • Perfect friend @Omni! So and instead of Listbox, if it’s Textbox?

Show 3 more comments

Browser other questions tagged

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