从数组中选择随机元素并显示在文本框中

布雷登推杆

我有一个带有按钮和文本框的表单。我还有一个包含以下内容的文本文件..

Bob:Available:None:0
Jack:Available:None:0
Harry:Available:None:0
Becky:Unavailable:Injured:8
Michael:Available:None:0
Steve:Available:None:0
Annie:Unavailable:Injured:8
Riley:Available:None:0

当用户加载表单时,文本文件的每个值都会存储到一个数组中。这工作正常。我想要发生的是,当按下按钮时,将从数组中检索一个值为“可用”的随机人(姓名)并显示在文本框中。

到目前为止我拥有的代码(将文本文件中的每个项目存储到数组中):

Public Class Form1

    'define profile of person
    Public Structure PersonInfo
        Public name As String
        Public status As String
        Public status_type As String
        Public monthsunavailable As Integer
   End Structure

    'Profile List of persons
    Public Shared personInfos As New List(Of PersonInfo)()   'roster data

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'read all infomations of person from file. lines is profile array
        Dim lines() As String = IO.File.ReadAllLines(filelocation)

        For Each line In lines
            'Parses the line string, make Person Info and add it to Person List

            'split string with ":"
            If line.Trim.Equals("") Then Continue For
            Dim strArr() = line.Split(":")

            'make Person Info
            Dim pi As New PersonInfo()

            pi.name = strArr(0)
            pi.status = strArr(1)
            pi.status_type = strArr(2)
            pi.monthsunavailable = strArr(3)

            'add Person Info to Person List
            personInfos.Add(pi)
        Next

如何从数组中选择一个随机名称并将其显示在文本框中?

甜菜碱

你可以使用这样的东西。尝试阅读文档!

 Private Sub Button1_click (sender As Object, e As EventArgs) Handles Button1.Click
     Dim r As New Random ()
     Textbox1.Text = personinfos(r.Next (0,personinfos.count)).name
 End Sub

更新

仅选择状态为“可用”的名称

 Private Sub Button1_click (sender As Object, e As EventArgs) Handles Button1.Click
     'Instantiate a new random variable
     Dim r As New Random ()
     'This is a LINQ query to select all items from the list where a property of the item
     '(in this case , status) is equal to something. 
     'Try changing Available to something else and see what you get
     Dim qr = From pi in personinfos
                      Where pi.status = "Available"
                      Select pi
      'The following line can be simplified as follows
      'Dim i As Integer = r.Next (0,qr.count)
      'Dim s As String = qr (i).name
      'Textbox1.Text = s
     Textbox1.Text = qr(r.Next (0,qr.count)).name
 End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从文本框中获取数组并在JQuery中选择框

根据datetimepiecker中选择的月份在文本框中显示文本

在 Smalltalk 中从数组中选择随机元素

无法在 selenium java 中选择输入文本框元素

在选择中显示文本框的值

如何在WPF的文本框中显示从日历中选择的月份?

从所选菜单中选择ID后,在文本框中显示价格

如何从Datagirdview中选择“行”并在文本框中显示?

如何从 3 个下拉列表中选择数据并在文本框中显示结果

从数组中选择随机元素

如何根据从 php 组合框中选择的值在数据库中的文本框中显示该值?

在组合框中选择商品代码后,如何在文本框中显示商品价格?

将数组中的文本显示到文本框中

在面板中选择多个文本框

如何在jsp中选择<option>时从数据库中获取多个值并显示在文本框中

在父表单的文本框中显示值,在子表单中选择网格视图的单元格

从数组的特定元素中选择随机元素

如何在Python中从数组中选择随机元素?

从python中的数组中选择3个随机元素

如何从存储在接口文件中的数组中选择随机元素?

从数组属性分配随机文本框

在选择标记中选择选项后,如何显示单选框或文本框?

从列表视图中选择项目后,从数组更改文本框文本值

使用js选择时在文本框中显示数据

在文本框中显示字节数组

在文本框中显示排序数组

如何从数组中选择随机元素?

从数组中选择随机元素,但唯一

从数组列表中选择多个/随机元素