1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
Public Class frmMonGenerateurNom
'à declarer en portee du class form
Private rndName As Random = New Random
Private Sub btnGenName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenName.Click
Dim strFichier As String = My.Resources.yob2012
Dim MaListeNoms As List(Of String) = ParseFileTextResource(strFichier)
'liste est copie dans tableau lines
Dim lines() As String = MaListeNoms.ToArray()
'cree un indice au hasard entre 1er nom et dernier nom du tableau lines
Dim index As Integer = rndName.Next(0, lines.GetUpperBound(0) - 1)
TextBox1.Text = lines(index)
End Sub
Private Function ParseFileTextResource(ByVal str As String) As List(Of String)
Dim tempListe As New List(Of String)
'recupere tableau des lignes (separees par retour chariot)
Dim lines() As String = str.Split(Environment.NewLine)
'dans chaque ligne recupere le 1er string (index zero) suivi d'une virgule
For Each line As String In lines
Dim nom As String = line.Split(",")(0)
tempListe.Add(nom)
Next
Return tempListe
End Function
End Class |