You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB6二维数组列标签显示及乘客管理程序优化求助

优化你的VB6乘客管理程序

Hey there! As someone who’s been through the VB6 learning curve, let’s work through fixing your program and making it more user-friendly and maintainable. Here’s a breakdown of solutions to your questions:


1. 先搞定列标签显示的问题

Your current printing logic loops by columns first, which makes the output messy. Plus, you’re using Val() for string inputs like names and flight numbers—that’s a big mistake! Val() converts non-numeric text to 0, which means all your passenger names will get erased.

To add clear column labels and fix the display:

  • First print a header row with your column names (姓名、航班号、出发地、目的地)
  • Then loop through each passenger and print all their details in a single row, aligned neatly.

2. 二维数组是否合适?

A 2D array works, but it’s not the most readable option. For passenger data (a collection of related attributes), a custom user-defined type (UDT) is way better. It lets you name each attribute (like PassengerName, FlightNumber) instead of relying on vague array indices (a(i,1), a(i,2)), making your code easier to debug and modify.


3. 完整优化后的代码

Here’s the revised code with all fixes and improvements:

' Define a custom type for passengers at the TOP of your form's code (before any subs)
Private Type PassengerData
    Name As String
    FlightNumber As String
    Departure As String
    Destination As String
End Type

' Declare an array of the custom type (we'll stick to 5 passengers as you did)
Private passengers(1 To 5) As PassengerData
Private currentPassengerCount As Integer ' Track how many passengers we've added

Private Sub Text1_KeyPress(KeyAscii As Integer)
    ' Only trigger when Enter is pressed (KeyAscii 13)
    If KeyAscii = 13 Then
        Select Case UCase(Text1.Text) ' Handle lowercase inputs automatically
            Case "R"
                ' Let users add passengers one by one until the array is full
                If currentPassengerCount < 5 Then
                    currentPassengerCount = currentPassengerCount + 1
                    With passengers(currentPassengerCount)
                        .Name = InputBox("Please enter Passenger Name:")
                        .FlightNumber = InputBox("Please enter Passenger Flight Number:")
                        .Departure = InputBox("Please enter Passenger Departure:")
                        .Destination = InputBox("Please enter Passenger Arrival:")
                    End With
                    MsgBox "Passenger " & currentPassengerCount & " added successfully!"
                Else
                    MsgBox "Maximum 5 passengers allowed!"
                End If
            Case "I"
                Picture1.Cls
                ' Print column labels with alignment
                Picture1.Print "序号"; Space(5); "姓名"; Space(10); "航班号"; Space(8); "出发地"; Space(8); "目的地"
                Picture1.Print String(50, "-") ' Add a separator line for clarity
                
                ' Loop through each added passenger and print their details
                For i = 1 To currentPassengerCount
                    Picture1.Print i; _
                        Space(7 - Len(CStr(i))); _ ' Align 序号
                        passengers(i).Name; _
                        Space(12 - Len(passengers(i).Name)); _ ' Align 姓名
                        passengers(i).FlightNumber; _
                        Space(10 - Len(passengers(i).FlightNumber)); _ ' Align 航班号
                        passengers(i).Departure; _
                        Space(10 - Len(passengers(i).Departure)); _ ' Align 出发地
                        passengers(i).Destination
                Next i
            Case "E"
                End
            Case Else
                MsgBox "Invalid input! Please enter R, I, or E."
        End Select
        Text1.Text = "" ' Clear input box after each action
    End If
End Sub

Key improvements explained:

  • Custom Type: PassengerData makes your code self-documenting—you don’t have to guess what a(i,3) means anymore.
  • No Val(): We use string inputs directly since names, flight numbers, etc., are text, not numbers.
  • Clear Column Labels: The header row clearly marks each column, with a separator line to make output easy to scan.
  • Aligned Output: Using Space() with Len() ensures each column lines up neatly, even if inputs are different lengths.
  • Passenger Count Tracking: Instead of forcing 5 entries at once, users can add passengers one by one until the array is full (way more user-friendly).
  • Input Validation: Handles lowercase inputs and invalid entries with a helpful message box.

内容的提问来源于stack exchange,提问作者Mansoure

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:32:47