英文:
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 = "" Or drpNumGuests.Text = "Please Select" Or txtCheckin.Text = "" Or txtCheckout.Text = "" Or txtRoomNo.Text = "" Then
errorFlag = True
End If
If errorFlag = True Then
MsgBox("Error with data entered, please try again.")
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) '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 & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
FileClose()
MsgBox("Booking complete")
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("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 & " Thank you " & Name & " for booking your stay with us. Your booking of " & NumGuests & " has been made for " & CheckIn & "to " & CheckOut & "
We look forward to your arrival. Your booking ID is:" & 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("bookings.txt").LastOrDefault()
If Not IsNothing(lastLine) Then
bookingID = Str(Int(lastLine.Substring(0, 8)) + 1) '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("Bookings.txt", True)
sw.WriteLine(NewBooking.GuestID & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
End Using
MsgBox("Booking complete")
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?...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论