Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用从列表框中选择的文本文件中的数据填充第二个表单_Vb.net_Vb.net 2010 - Fatal编程技术网

Vb.net 使用从列表框中选择的文本文件中的数据填充第二个表单

Vb.net 使用从列表框中选择的文本文件中的数据填充第二个表单,vb.net,vb.net-2010,Vb.net,Vb.net 2010,VB.Net: 我使用的是两种形式——frmDetails和frmInventory frmInventory包含一个列表框,用于读取包含书名、作者、类别、库存和每项价格(五个元素)列表的文本文件。列表框仅显示图书标题 frmDetails包含与文本文件中的元素匹配的单个文本框 如果用户在frmInventory列表框中选择一个项目(标题)并从下拉菜单中选择update,则frmDetails中的文本框需要填充与其标签匹配的元素(标题与标题、作者与作者等)。换句话说,选中后,需要读取文本文件,解

VB.Net: 我使用的是两种形式——frmDetails和frmInventory frmInventory包含一个列表框,用于读取包含书名、作者、类别、库存和每项价格(五个元素)列表的文本文件。列表框仅显示图书标题

frmDetails包含与文本文件中的元素匹配的单个文本框

如果用户在frmInventory列表框中选择一个项目(标题)并从下拉菜单中选择update,则frmDetails中的文本框需要填充与其标签匹配的元素(标题与标题、作者与作者等)。换句话说,选中后,需要读取文本文件,解析数据,并在每个文本框中填充

我尝试了几种不同形式的代码:

Dim selectedUpdate As String = lstBookList.SelectedItem.ToString
If selectedUpdate = lstBookList.SelectedItem.ToString Then
  Dim queryUpdate = From item In File.ReadAllLines("Books.txt")
                    Let ti = item.Split(","c)(0)
                    Let au = item.Split(","c)(1)
                    Let ca = item.Split(","c)(2)
                    Let qt = item.Split(","c)(3)
                    Let co = item.Split(","c)(4)
                    Where ti = selectedUpdate
                    Select ti & "," & au & "," & ca & "," & qt & "," & co
  For Each ti In queryUpdate
    frmDetails.txtTitle.Text = ti
    For Each au In queryUpdate
      frmDetails.txtAuthor.Text = au
      For Each qt In queryUpdate
        frmDetails.txtStock.Text = qt
        For Each co In queryUpdate
          frmDetails.txtPrice.Text = co
        Next
      Next
    Next
  Next
End If
frmDetails.ShowDialog()
End Sub
或:

或:

问题在于解析记录中的数据,以便将其分散到不同的文本字段中


如果您有任何帮助,我们将不胜感激。

好的,我坚持我最初的意见,您应该创建一个图书类,并将文本文件读入某种(图书)集合中

特定于您的需求,bindinglist是合适的

例如:

Public Class frmInventory
Public bookList As New BindingList(Of Book)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Presumes each book is stored on one line, with data seperated with a ";"
    'eg: Wrox Beginning Visual Basic 2010;Thearon Willis;non-fiction;100;17.5
    Dim books As String() = IO.File.ReadAllLines("C:\Users\Admin\Documents\allBooks.txt")

    For i = 0 To books.Count - 1
        Dim bookdata As String() = books(i).Split(";")
        bookList.Add(New Book(bookdata(0), bookdata(1), bookdata(2), CInt(bookdata(3)), CDbl(bookdata(4))))
    Next

    'set listbox1 datasource to our newly populated bindinglist(of book)
    ListBox1.DataSource = bookList
    'set displaymember to title. Could be any one of the other attributes of book.
    ListBox1.DisplayMember = "title"
End Sub

'When edit button is clicked: if you want the following to fire when an option is selected from a dropdown, handle accordingly
Private Sub btnEditSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditSelected.Click

    'the listbox contains book objects, not a string! This can catch people out
    Dim selectedBook As Book = ListBox1.SelectedItem
    'create an instance of frmDetails, and pass it the selected book object
    Dim editform As New frmDetails(selectedBook)
    'Show editform as model and if the user clicks the OK button on the model form:
    If editform.ShowDialog() = DialogResult.OK Then
        'replace the existing book object in booklist with the modified one.
        bookList(bookList.IndexOf(selectedBook)) = selectedBook
    End If

End Sub


End Class
您的FRM详细信息如下所示:

Public Class frmDetails
'auto property: creates a private book object called _book (note the underscore), see
'http://msdn.microsoft.com/en-us/library/dd293589.aspx for more details on autoproperties
Property book As Book

'require a book object to be sent when a new instance is created, by reference so we can edit the actual book object, not a copy
Public Sub New(ByRef book As Book)

    _book = book

    InitializeComponent()

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = _book.title
    TextBox2.Text = _book.author
    TextBox3.Text = _book.category
    TextBox4.Text = _book.stock
    TextBox5.Text = _book.wholesaleprice
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    _book.title = TextBox1.Text
    _book.author = TextBox2.Text
    _book.category = TextBox3.Text
    _book.stock = TextBox4.Text
    _book.wholesaleprice = TextBox5.Text
    DialogResult = DialogResult.OK
End Sub
End Class
在设计器中,将表单“AcceptButton”属性设置为btnOK。您还应该将其cancel button属性设置为适当的按钮

最后,本书的内容如下:

Public Class Book
Property title As String
Property author As String
Property category As String
Property stock As Integer
Property wholesaleprice As Double

Public Sub New(ByVal title As String, ByVal author As String, ByVal category As String, ByVal stock As Integer, ByVal wholesaleprice As Double)

    _title = title
    _author = author
    _category = category
    _stock = stock
    _wholesaleprice = wholesaleprice
End Sub
End Class

我不完全清楚你想做什么。更糟糕的是,您的代码遵循非常糟糕的编码标准。当我看到这样写的4个嵌套循环时,我的眼睛很痛
Let xx=item.Split(“,”c)(0)
也不好。无意冒犯,但我建议先阅读VB.NET上的一本书,然后开始编写代码。我不知道这是不是一个标准的家庭作业,但它和我最近看到的几个问题很相似。无论如何,看看我在这里的答案:它涉及到创建一个Book类,将所有书籍读入一个(书籍)列表,这样您就可以使用linq轻松地查询它。如果书单太大而无法保存在内存中,我建议建立一个数据库。sqlite将是一个简单的选项使用这种方法,您可以将单个book对象传递给新表单,并简单地设置文本框,例如txtAuthor.Text=book.author,txtTitle.Text=book.title等为了清晰起见,我需要将文本文件中的元素以单独的形式传递给各个文本框。所选择的只是标题。我的代码可能看起来很简单,因为它是。我正在上大学课程,还在学习。我插入的代码就是我尝试过的,因为这是我目前所知道的全部。这正是上面提到的user574632项目,但它没有涵盖我的具体问题。我所需要的帮助就是更新部分——“添加和编辑过程使用第二种形式,frmDetails。”添加了对我的链接答案的修改,具体用于更新
Public Class frmDetails
'auto property: creates a private book object called _book (note the underscore), see
'http://msdn.microsoft.com/en-us/library/dd293589.aspx for more details on autoproperties
Property book As Book

'require a book object to be sent when a new instance is created, by reference so we can edit the actual book object, not a copy
Public Sub New(ByRef book As Book)

    _book = book

    InitializeComponent()

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = _book.title
    TextBox2.Text = _book.author
    TextBox3.Text = _book.category
    TextBox4.Text = _book.stock
    TextBox5.Text = _book.wholesaleprice
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    _book.title = TextBox1.Text
    _book.author = TextBox2.Text
    _book.category = TextBox3.Text
    _book.stock = TextBox4.Text
    _book.wholesaleprice = TextBox5.Text
    DialogResult = DialogResult.OK
End Sub
End Class
Public Class Book
Property title As String
Property author As String
Property category As String
Property stock As Integer
Property wholesaleprice As Double

Public Sub New(ByVal title As String, ByVal author As String, ByVal category As String, ByVal stock As Integer, ByVal wholesaleprice As Double)

    _title = title
    _author = author
    _category = category
    _stock = stock
    _wholesaleprice = wholesaleprice
End Sub
End Class