可以在同一项目中的两个单独的表单中使用相同的文本文件吗?

huangapple go评论121阅读模式
英文:

Can I use the same text file in two separate forms within the same project?

问题

我已经找到一个解决方法,感谢您的帮助 可以在同一项目中的两个单独的表单中使用相同的文本文件吗?

我正在进行一个计算机科学项目,目前处于原型阶段。一旦校园计算机上的一切都整理好,我将把整个项目转移到数据库中,但目前我正在使用文本文件。它是在Visual Studio中使用VB编写的(这是我学院要求的)。我试图在不同的窗体中的不同时间使用同一个文本文件,但每次运行时都会遇到相同的错误,显示文件在其他地方已经打开。我猜想这是因为它在下一个窗体中从该文件中读取,但我已经使文件在其他窗口上的特定按钮点击之前都没有打开。当我尝试从第一个窗口写入数据到文件时,就会出现这个错误,我从未达到下一个窗体中从文件中读取的阶段。

背景信息:

这是一个青年旅馆的预订系统,预订文本文件是从预订页面写入并保存的。这打开了一个预订确认页面,从文件的最后一行读取数据,取出必要的信息(姓名、入住/退房日期、预订参考等),以便插入到预订确认消息中,使其适应每个预订,但保持相同的结构。我有一个“保存预订”的按钮,会将数据写入文件,但在这里出现了错误。另一个窗体没有打开,文本文件也没有在其他地方打开,写入文件后,它被关闭。

是否有解决方法?我是否应该尝试使用变量来保存输入数据,以在下一个窗体中使用?我并不太担心它是否完美,因为这只是一个原型,直到我的学院解决了与数据库的问题,但我仍希望它能按照我想要的方式运行。

谢谢 可以在同一项目中的两个单独的表单中使用相同的文本文件吗?

来自预订页面:

Private Sub btnBook_Click(sender As Object, e As EventArgs) Handles btnBook.Click
    Dim NewBooking As Bookings
    Dim errorFlag As Boolean
    Dim bookingID As Integer
    errorFlag = False
    If txtID.Text = "" Or drpNumGuests.Text = "Please Select" Or txtCheckin.Text = "" Or txtCheckout.Text = "" Or txtRoomNo.Text = "" Then
        errorFlag = True
    End If
    If errorFlag = True Then
        MsgBox("输入数据有误,请重试。")
    Else
        Dim sw As New System.IO.StreamWriter("Bookings.txt", True) 'fix
        Dim Booking() As String = File.ReadAllLines("bookings.txt")
        If UBound(Booking) < 0 Then
            bookingID = 0
        Else
            bookingID = Str(Int(Booking(UBound(Booking)).Substring(0, 8)) + 1) '自动分配预订ID
        End If
        NewBooking.GuestID = LSet(txtID.Text, 9)
        NewBooking.BookingID = LSet(bookingID, 9)
        NewBooking.Name = LSet(txtName.Text, 25)
        NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
        NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
        NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
        NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
        NewBooking.Requests = txtRequests.Text

        sw.WriteLine(NewBooking.GuestID & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
        FileClose()
        MsgBox("预订完成")
    End If
End Sub

Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
    Booking_Confirmation.Show()
    Me.Hide()

来自预订确认页面:

Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
    Dim Bookconfirm() As String = File.ReadAllLines("bookings.txt")
    Dim Name As String
    Dim CheckIn As String
    Dim CheckOut As String
    Dim NumGuests As String
    Dim RoomNum As String
    Dim ID As String
    Dim BookingID As String
    Dim Count As Integer = UBound(Bookconfirm)
    ID = Mid(Bookconfirm(Count), 1, 8)
    BookingID = Mid(Bookconfirm(Count), 10, 8)
    Name = Mid(Bookconfirm(Count), 19, 25)
    CheckIn = Mid(Bookconfirm(Count), 46, 10)
    CheckOut = Mid(Bookconfirm(Count), 57, 10)
    NumGuests = Mid(Bookconfirm(Count), 44, 2)
    RoomNum = Mid(Bookconfirm(Count), 68, 2)

    txtConfirmation.Text = ID & "  谢谢 " & Name & " 预订您的住宿。您的" & NumGuests & "人的预订已经完成,入住日期为" & CheckIn & "至" & CheckOut & "。期待您的到来。您的预订ID是:" & BookingID
End Sub

希望这对您有所帮助。如果您有其他问题,请随时提出。

英文:

Edit: I've found a work around, thanks for all your help 可以在同一项目中的两个单独的表单中使用相同的文本文件吗?

I'm working on a computer science project and I'm in the prototype stage right now. I will eventually switch this entire thing to a database once everything for it is sorted on the campus computers but for now I'm using text files. It's written on visual studio using VB (Required by my college). I'm trying to use the same text file in two different forms at different times but I keep getting the same error when I run it, saying that the file is open elsewhere. I assume this is because it reads from that file in the next form but I have made it so that the file isn't even opened until a specific button click on that other window. It reaches this error when I attempt to write data to the file from the first window, I never get to the stage where I read from the file in the next form.

Context:

It's a booking system for a youth hostel, the booking text file is written to, from the booking page and saved. This opens up a booking confirmation page where data is read from the last line of the file and necessary information is taken out (Name, checkin/checkout dates, booking reference etc.) to be inserted into the booking confirmation message so it's sort of tailored to each booking but keeps the same structure. I have a button to "save booking" which would write the data to the file but this is where the error occurs. The other form isn't open, the text file isnt open anywhere else and after writing to the file, it's closed.

Is there a workaround? should I just try using variables to hold the input data to use it in the next form? I'm not too worried about it being perfect because this is just a prototype until my college sorts out its issue with databases but I'd still like it to function the way I want it to.

Thank you 可以在同一项目中的两个单独的表单中使用相同的文本文件吗?

From booking page:

Private Sub btnBook_Click(sender As Object, e As EventArgs) Handles btnBook.Click
        Dim NewBooking As Bookings
        Dim errorFlag As Boolean
        Dim bookingID As Integer
        errorFlag = False
        If txtID.Text = &quot;&quot; Or drpNumGuests.Text = &quot;Please Select&quot; Or txtCheckin.Text = &quot;&quot; Or txtCheckout.Text = &quot;&quot; Or txtRoomNo.Text = &quot;&quot; Then
            errorFlag = True
        End If
        If errorFlag = True Then
            MsgBox(&quot;Error with data entered, please try again.&quot;)
        Else
            Dim sw As New System.IO.StreamWriter(&quot;Bookings.txt&quot;, True)      &#39;fix
            Dim Booking() As String = File.ReadAllLines(&quot;bookings.txt&quot;)
            If UBound(Booking) &lt; 0 Then
                bookingID = 0
            Else
                bookingID = Str(Int(Booking(UBound(Booking)).Substring(0, 8)) + 1)      &#39;Automatic BookingID assignment
            End If
            NewBooking.GuestID = LSet(txtID.Text, 9)
            NewBooking.BookingID = LSet(bookingID, 9)
            NewBooking.Name = LSet(txtName.Text, 25)
            NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
            NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
            NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
            NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
            NewBooking.Requests = txtRequests.Text

            sw.WriteLine(NewBooking.GuestID &amp; NewBooking.BookingID &amp; NewBooking.Name &amp; NewBooking.GuestNum &amp; NewBooking.CheckIn &amp; NewBooking.CheckOut &amp; NewBooking.RoomNum &amp; NewBooking.Requests)
            FileClose()
            MsgBox(&quot;Booking complete&quot;)

        End If

    End Sub

    Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        Booking_Confirmation.Show()
        Me.Hide()

From booking confirmation page:

Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
        Dim Bookconfirm() As String = File.ReadAllLines(&quot;bookings.txt&quot;)
        Dim Name As String
        Dim CheckIn As String
        Dim CheckOut As String
        Dim NumGuests As String
        Dim RoomNum As String
        Dim ID As String
        Dim BookingID As String
        Dim Count As Integer = UBound(Bookconfirm)
        ID = Mid(Bookconfirm(Count), 1, 8)
        BookingID = Mid(Bookconfirm(Count), 10, 8)
        Name = Mid(Bookconfirm(Count), 19, 25)
        CheckIn = Mid(Bookconfirm(Count), 46, 10)
        CheckOut = Mid(Bookconfirm(Count), 57, 10)
        NumGuests = Mid(Bookconfirm(Count), 44, 2)
        RoomNum = Mid(Bookconfirm(Count), 68, 2)

        txtConfirmation.Text = ID &amp; &quot;  Thank you &quot; &amp; Name &amp; &quot; for booking your stay with us. Your booking of &quot; &amp; NumGuests &amp; &quot; has been made for &quot; &amp; CheckIn &amp; &quot;to &quot; &amp; CheckOut &amp; &quot;
                               We look forward to your arrival. Your booking ID is:&quot; &amp; BookingID
End Sub

答案1

得分: 0

以下是翻译好的代码部分:

这是您可以尝试的另一版本如评论中所建议使用`Using`语句将确保文件完全关闭和释放

bookingID = 0
Dim lastLine As String = File.ReadAllLines("bookings.txt").LastOrDefault()
If Not IsNothing(lastLine) Then
    bookingID = Str(Int(lastLine.Substring(0, 8)) + 1) '自动分配BookingID
End If

NewBooking.GuestID = LSet(txtID.Text, 9)
NewBooking.BookingID = LSet(bookingID, 9)
NewBooking.Name = LSet(txtName.Text, 25)
NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
NewBooking.Requests = txtRequests.Text

Using sw As New System.IO.StreamWriter("Bookings.txt", True)
    sw.WriteLine(NewBooking.GuestID & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
End Using

MsgBox("预订完成")

如果所有信息已经包含在`NewBooking`那么要么将其传递给第二个窗体要么将其声明移到FORM级别以便可以访问然后您不需要再次读取文件来检索刚刚获得的信息
英文:

Here's an alternate version you can try. As suggested by Hursey in the comments, a Using statement will make sure the file is completely closed and released.

bookingID = 0
Dim lastLine As String = File.ReadAllLines(&quot;bookings.txt&quot;).LastOrDefault()
If Not IsNothing(lastLine) Then
    bookingID = Str(Int(lastLine.Substring(0, 8)) + 1) &#39;Automatic BookingID assignment
End If

NewBooking.GuestID = LSet(txtID.Text, 9)
NewBooking.BookingID = LSet(bookingID, 9)
NewBooking.Name = LSet(txtName.Text, 25)
NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
NewBooking.Requests = txtRequests.Text

Using sw As New System.IO.StreamWriter(&quot;Bookings.txt&quot;, True)
    sw.WriteLine(NewBooking.GuestID &amp; NewBooking.BookingID &amp; NewBooking.Name &amp; NewBooking.GuestNum &amp; NewBooking.CheckIn &amp; NewBooking.CheckOut &amp; NewBooking.RoomNum &amp; NewBooking.Requests)
End Using

MsgBox(&quot;Booking complete&quot;)

If all of the information is already in NewBooking then either PASS that to the secondary form, or simply move it's declaration out to FORM level so that it's accessible. Then you won't need to read the file again to retrieve the information you literally just had?...

huangapple
  • 本文由 发表于 2023年2月10日 03:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定