0

I have two radio buttons side by side in my forms and one of them generates a random password when clicked which is fine but how do I get it to, if clicked again (while already checked) it runs the code again, this isn't an issue with my code so none is required, its more of a question with visual studio's winforms radio button. Thanks in advance

private void NAPOCustom_CheckedChanged(object sender, EventArgs e) //Weird radiobutton name i know
        {
            NAPasswordE.Clear(); //text box which generation password goes into
                string validChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
            Random random = new Random();
            char[] chars = new char[12];
            for (int i = 0; i < 12; i++)
            {
                chars[i] = validChars[random.Next(0, validChars.Length)];
            }
            NAPasswordE.Text = new string(chars);
        }

I click the radiobutton

Desired Output: random string (this works)

I click the same radiobutton again

Desired Output: random string but different to first one (doesnt work)

Evorage
  • 493
  • 3
  • 15
  • 1
    You should not use a radio button do such behavior because this is a UX anti-pattern. –  Jul 09 '20 at 12:03
  • You're most probably looking for the [Click](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.click?view=netcore-3.1) event. But I highly recommend you don't misuse the radio button is such a way. Instead you should have a regular button that does this – MindSwipe Jul 09 '20 at 12:04
  • The checked change event must not be called if clicking on an already checked button... But it does that using the mouse click event. –  Jul 09 '20 at 12:21
  • @OlivierRogier thanks for telling me my problem. Any way to fix it? – Evorage Jul 09 '20 at 12:22
  • Don't know, perhaps that: https://stackoverflow.com/questions/11493845/radio-button-checked-changed-event-fires-twice. Can you add a link to the code file or put it in the question? –  Jul 09 '20 at 12:24

1 Answers1

1

You can handle both the Click() and CheckChanged() event. Technically it will produce two passwords, but in this case I don't think it really matters. Move your password generation code out to a separate method and call that from both event handlers. Also move your Random out to class level.

Something like:

private Random random = new Random();

private void NAPOCustom_Click(object sender, EventArgs e)
{
    RandomPassword();
}

private void NAPOCustom_CheckedChanged(object sender, EventArgs e)
{
    if (NAPOCustom.Checked)
    {
        RandomPassword();
    }
}

private void RandomPassword()
{           
    string validChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
    char[] password = validChars.ToCharArray().OrderBy(x => random.Next()).Take(12).ToArray();            
    NAPasswordE.Text = new string(password);
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • i feel like this will work but i copied and pasted this and im not getting a reference for the _Click bit so it does work, any ideas? – Evorage Jul 09 '20 at 15:49
  • This does generate a random password but only when i click a different radiobutton and i cant click it multiple times – Evorage Jul 09 '20 at 15:50
  • You have to wire up the Click() event for your RadioButton. Select "NAPOCustom" in your Form. In the Properties Pane, bottom right by default, click the "Lightning Bolt Icon" to get a list of the Events. Find the "Click" entry and change the dropdown to the right so that it points "NAPOCustom_Click". – Idle_Mind Jul 09 '20 at 15:51
  • Okay that works to generate a random string but doesnt work if clicked multiple times (stays the same) – Evorage Jul 09 '20 at 15:53
  • It worked for multiple clicks on my system. You probably don't have the CLICK event wired up correctly. – Idle_Mind Jul 09 '20 at 15:56
  • I got it working using the mouse click thing not the click thing, idk why or how tho – Evorage Jul 09 '20 at 15:59