title: Visual Basic Password Generator
body: <p>I have been tasked with creating a visual basic random password generator. I have come up with the below which works however its a bit crude with its password criteria. I would like each password it generates to have at least 1 number, uppercase and lowercase in it. However how I have coded this it will generate a random combination which quite often results in one of the criteria being missed out. </p>
  
  <p>I have had a play myself and I was going to have three strings, one with uppercase, one with lower case and a third with numbers. Once it has one of each it will then generate the rest of the password using my code. This doesn't sound very clean and I have been having problems doing this. </p>
  
  <p>Can anyone help point me in the right direction or assist with the code below. Passwords must be between 6 and 20 characters in length. </p>
  
  <p>Thanks in advance </p>
  
  <pre><code>Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          TextBox2.Text = GenerateCode()
      End Sub
  
      Private Function GenerateCode() As Object
          Dim intRnd As Object
          Dim intStep As Object
          Dim strName As Object
          Dim intNameLength As Object
          Dim intLength As Object
          Dim strInputString As Object
          strInputString = "1234567890ABCDEFGHIJKLMEOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz"
  
          intLength = Len(strInputString)
  
          Randomize()
  
  
          strName = ""
  
          'Check for valid numeric entry
          If Integer.TryParse(TextBox1.Text, intNameLength) And intNameLength &gt;= 6 And intNameLength &lt;= 20 Then
              For intStep = 1 To intNameLength
                  intRnd = Int((intLength * Rnd()) + 1)
  
                  strName = strName &amp; Mid(strInputString, intRnd, 1)
  
              Next
              GenerateCode = strName
          Else
              TextBox1.Text =("Please enter a valid password length")
          End If
  
      End Function
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  
          My.Computer.Clipboard.SetText(TextBox2.Text)
  
      End Sub
  
      Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
          TextBox1.Text = ""
      End Sub
  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
      End Sub
  End Class
  </code></pre>
  
  <p>========== UPDATE ==========</p>
  
  <p>Right I have changed my code a little so I have now got three strings with the different characters in, 1 for uppers, one for lowers and one for numbers. </p>
  
  <p>This enables me to have a checkbox functionality in the form which is a plus. I have played around with the code and it now generates passwords depending on what I select with the checkboxes which as I say is awesome however I cant always guarantee that if I select, number, upper and lower I will get a password that contains all three, and sometimes the password will only contain numbers even though all three boxes are checked.</p>
  
  <p>In guessing this is something to do with the fact I'm just asking it to generate random password using the characters I give it, and there is no verification that it has used all three of the options. </p>
  
  <p>Any help on this would be awesome. I am trying and I'm not just posting and hoping someone will do the work for me. If someone can point me in the right direction would be great. </p>
  
  <p>Here is my new code. </p>
  
  <pre><code>Public Function GenerateCode()
      Dim intRnd As Integer
      Dim intStep As Integer = Nothing
      Dim strname As String
      Dim intlength As Integer
      Dim strinputstring As String = ""
      Dim Numbers As String = "12345678901234567890"
      Dim Lower As String = "abcdefghijklmnopqrstuvwxyzyz"
      Dim Upper As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZYZ"
      Dim intnamelength As Integer = 1
  
  
      If CheckBox1.Checked Then strinputstring &amp;= Lower
      If CheckBox2.Checked Then strinputstring &amp;= Numbers
      If CheckBox3.Checked Then strinputstring &amp;= Upper
  
      intlength = Len(strinputstring)
  
      Integer.TryParse(NumericUpDown1.Text, intnamelength)
  
      Randomize()
  
      strname = ""
  
      For inStep = 1 To intnamelength
  
          intRnd = Int(Rnd() * intlength) + 1
  
          strname = strname &amp; Mid(strinputstring, intRnd, 1)
  
      Next
  
      Return strname
  
  End Function
  </code></pre>