By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

Two dimension Arrays

P: 9
I posted my problem in ref to my Airline Reservation Program. I am using VB 2005 for this program

I have to right a program that req a users name for a text box then assigns the user a seat (row(x), seat(x). The seat assignment is based on a two dimensional array seatingchart(9,3). When the user enters name in the text box a click event must be activated in order to store the passenger in a variable string passenger. I also have a control array btnseatresv(9,3) that display a 10 X 4 tabular table of button which signifies the seat location, if a seat is red then the seat is already taken if the seat is green then the seat is open. each button has a click event on it. I am having some problems with the logic behind the code.

1. When the passenger name is typed in the text box and the addpassenger button is clicked it assigns the variable to the passenger name code below:

passenger = txtPassName.Text

then an input box is displayed which then ask the user to enter the row pass wants to seat in. (See Below)
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
next another input box is displayed which then ask the user to enter the seat pass wants to seat in. (See Below)
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))the if statement is neccessary to determine if seat has already been taken
(this line of code does not work: need help
If btnSeatResv(rowpos - 1, colpos - 1).Enabled <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
basically it alway skip the first part of if statement and goes to the else portion rather the seat has already been assigned or not.
the next line of code assigns the pass name and desired seat location.
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
it then assigns the location to the btnarray and change color from green to red and enables it. (see below)
btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False

This works for the first passenger assignment but when I try to add another passenger it does not change the color of button and disable it, but it does assign to seating chart array

Can anyone give my some suggestions to how to fix my code I need the project done by 11:59 pm tonight. Code is given agian but not broken up below

I thank you all and appreiciate your suggestions

(ARRAYS ARE DECLARED OUT SIDE OF THE btnAddPass procedure)


Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
Dim row, row2 As Integer
Dim col, col2 As Integer
Dim x As Integer = 221
Dim y As Integer = 150
Dim a As Integer = 128
Dim b As Integer = 153
Dim c As Integer = 150
Dim d As Integer = 130
Dim rowpos As Integer
Dim colpos As Integer

Dim passenger As String
AddButton(row, col, x, y)
AddSeatLabel(col2, c, d)
AddRowNum(row2, a, b)
passenger = txtPassName.Text
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))

If btnSeatResv(rowpos - 1, colpos - 1).Enabled <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
btnSeatResv(rowpos - 1, colpos - 1).Text = " "

btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False

End If
txtPassName.Clear()
txtPassName.Focus()

End Sub
Nov 7 '06 #1
Share this Question
Share on Google+
7 Replies


100+
P: 1,646
I posted my problem in ref to my Airline Reservation Program. I am using VB 2005 for this program

I have to right a program that req a users name for a text box then assigns the user a seat (row(x), seat(x). The seat assignment is based on a two dimensional array seatingchart(9,3). When the user enters name in the text box a click event must be activated in order to store the passenger in a variable string passenger. I also have a control array btnseatresv(9,3) that display a 10 X 4 tabular table of button which signifies the seat location, if a seat is red then the seat is already taken if the seat is green then the seat is open. each button has a click event on it. I am having some problems with the logic behind the code.

1. When the passenger name is typed in the text box and the addpassenger button is clicked it assigns the variable to the passenger name code below:

passenger = txtPassName.Text

then an input box is displayed which then ask the user to enter the row pass wants to seat in. (See Below)
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
next another input box is displayed which then ask the user to enter the seat pass wants to seat in. (See Below)
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))the if statement is neccessary to determine if seat has already been taken
(this line of code does not work: need help
If btnSeatResv(rowpos - 1, colpos - 1).Enabled <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
basically it alway skip the first part of if statement and goes to the else portion rather the seat has already been assigned or not.
the next line of code assigns the pass name and desired seat location.
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
it then assigns the location to the btnarray and change color from green to red and enables it. (see below)
btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False

This works for the first passenger assignment but when I try to add another passenger it does not change the color of button and disable it, but it does assign to seating chart array

Can anyone give my some suggestions to how to fix my code I need the project done by 11:59 pm tonight. Code is given agian but not broken up below

I thank you all and appreiciate your suggestions

(ARRAYS ARE DECLARED OUT SIDE OF THE btnAddPass procedure)


Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
Dim row, row2 As Integer
Dim col, col2 As Integer
Dim x As Integer = 221
Dim y As Integer = 150
Dim a As Integer = 128
Dim b As Integer = 153
Dim c As Integer = 150
Dim d As Integer = 130
Dim rowpos As Integer
Dim colpos As Integer

Dim passenger As String
AddButton(row, col, x, y)
AddSeatLabel(col2, c, d)
AddRowNum(row2, a, b)
passenger = txtPassName.Text
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))

If btnSeatResv(rowpos - 1, colpos - 1).Enabled <> True Then
MsgBox("Seat already taken. Please choose another seat")
Else
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
btnSeatResv(rowpos - 1, colpos - 1).Text = " "

btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False

End If
txtPassName.Clear()
txtPassName.Focus()

End Sub
Hi could you show us how the arrays are declared as that seems to be important
thanks
Nov 8 '06 #2

P: 9
First and foremost I want to say thank you I really appreciate any help you may be able to give.

I have modified the code since that last posting,
and my previous problem has been resolved
i moved my subprocedures which calls the control arrays
Dim btnSeatResv(9, 3) As Button
Dim lblSeatNum(4) As Label
Dim lblRowNum(10) As Label
to form load and it works now.

My new problem is: I need to be able to determine when a seat has already been assigned, prompt user to chose another seat, and then assign that seat it needs to continue this check until user finds and unsigned seat - I attempted this with the code below, but it has a problem with it.

I added a do until loop to the following code (see below)
if codition is true to exit loop it also exits the if statement, I know this is a simple fix but my brain is about fried now any suggestions to get it to execute the next line of code: to assign
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos

Changed Code
If btnSeatResv(rowpos - 1, colpos - 1).Enabled = False Then
do until btnSeatResv(rowpos - 1, colpos - 1).Enabled = True
MsgBox("Seat already taken. Please choose another seat")
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
Loop
Else
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
btnSeatResv(rowpos - 1, colpos - 1).Text = "."

btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False
End If



Complete Code
Dim btnSeatResv(9, 3) As Button
Dim lblSeatNum(4) As Label
Dim lblRowNum(10) As Label
Dim lblrow As Label
Dim SeatingChart(9, 3) As String
Dim rowcount As Integer = 0
Dim colcount As Integer = 0
Dim upperbound As Integer
Dim counter As Integer

Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click

Dim rowpos As Integer
Dim colpos As Integer



Dim passenger As String

passenger = txtPassName.Text
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))

If btnSeatResv(rowpos - 1, colpos - 1).Enabled = False Then
Do Until btnSeatResv(rowpos - 1, colpos - 1).Enabled = True
MsgBox("Seat already taken. Please choose another seat")
rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
Loop
Else
SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
btnSeatResv(rowpos - 1, colpos - 1).Text = "."

btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
btnSeatResv(rowpos - 1, colpos - 1).Enabled = False

End If
txtPassName.Clear()
txtPassName.Focus()

End Sub

Sub AddButton(ByRef row As Integer, ByRef col As Integer, ByRef x As Integer, ByRef y As Integer)
lblrow = New Label
lblrow.Text = "Row"
lblrow.Name = "lblrow"
lblrow.Width = 50
lblrow.Height = 20
lblrow.TextAlign = ContentAlignment.MiddleCenter
lblrow.Location = New Point(156, 128)
Controls.Add(lblrow)


For row = 0 To 9
For col = 0 To 3
btnSeatResv(row, col) = New Button
btnSeatResv(row, col).Name = "btnSeatResv" & row & col
btnSeatResv(row, col).Text = ""
btnSeatResv(row, col).Location = New Point(x, y)
btnSeatResv(row, col).Enabled = True
btnSeatResv(row, col).Width = 15
btnSeatResv(row, col).Height = 20
btnSeatResv(row, col).BackColor = Color.Green
Controls.Add(btnSeatResv(row, col))
x += 44

Next
x = 221
y += 25
Next
End Sub
Sub AddSeatLabel(ByRef col2 As Integer, ByRef c As Integer, ByRef d As Integer)

For col2 = 1 To 4
c += 48
lblSeatNum(col2) = New Label
lblSeatNum(col2).Name = "txtSeatResv" & col2
lblSeatNum(col2).Text = "Seat " & col2
lblSeatNum(col2).Width = 50
lblSeatNum(col2).Height = 15
lblSeatNum(col2).TextAlign = ContentAlignment.MiddleCenter
lblSeatNum(col2).Location = New Point(c, d)
Controls.Add(lblSeatNum(col2))

Next
End Sub
Sub AddRowNum(ByRef row2 As Integer, ByVal a As Integer, ByVal b As Integer)
For row2 = 1 To 10
a += 44
lblRowNum(row2) = New Label
lblRowNum(row2).Name = "txtSeatResv" & row2
lblRowNum(row2).Text = row2 & " :"
lblRowNum(row2).Width = 25
lblRowNum(row2).Height = 15
lblRowNum(row2).TextAlign = ContentAlignment.MiddleRight
lblRowNum(row2).Location = New Point(a, b)
Controls.Add(lblRowNum(row2))
a = 128
b += 25

Next
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim row, row2 As Integer
Dim col, col2 As Integer
Dim x As Integer = 221
Dim y As Integer = 150
Dim a As Integer = 128
Dim b As Integer = 153
Dim c As Integer = 150
Dim d As Integer = 130
AddButton(row, col, x, y)
AddSeatLabel(col2, c, d)
AddRowNum(row2, a, b)
End Sub
End Class
Nov 8 '06 #3

Expert 5K+
P: 8,434
Here's something you can try (very tiny change)...
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPass.Click
  2.  
  3. Dim rowpos As Integer
  4. Dim colpos As Integer
  5.  
  6. Dim passenger As String
  7.  
  8. passenger = txtPassName.Text
  9. rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
  10. colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
  11.  
  12. If btnSeatResv(rowpos - 1, colpos - 1).Enabled = False Then
  13. Do Until btnSeatResv(rowpos - 1, colpos - 1).Enabled = True
  14.   MsgBox("Seat already taken. Please choose another seat")
  15.   rowpos = CInt(InputBox("Please enter row passenger wants to seat in:"))
  16.   colpos = CInt(InputBox("Please enter seat passenger wants to seat in:"))
  17. Loop
  18. End If
  19. SeatingChart(rowpos - 1, colpos - 1) = passenger & " Row " & rowpos & " " & "Seat " & colpos
  20. btnSeatResv(rowpos - 1, colpos - 1).Text = "."
  21.  
  22. btnSeatResv(rowpos - 1, colpos - 1).BackColor = Color.Red
  23. btnSeatResv(rowpos - 1, colpos - 1).Enabled = False
  24.  
  25. 'End If
  26. txtPassName.Clear()
  27. txtPassName.Focus()
  28.  
  29. End Sub
  30.  
Also, I'd recommend indenting your code - makes it much easier to read. Or is it just messed up from pasting it here?
Nov 8 '06 #4

Expert 5K+
P: 8,434
Hi Meena76.

(Hope you didn't miss my posting from a couple of minutes ago because of this one. I just wanted to make another comment.)

While I realise this is kind of a "quick and dirty" application, and put together in a hurry, I'd suggest you validate (check) the input values before using them as indexes for your array. Otherwise, if the user enters a value outside of the proper range they'll crash your code.

For example, what if the user accidentally requests row 5, seat number 33333 because their finger slipped?
Nov 8 '06 #5

P: 9
thank you for the suggesting, it is due to copy & pasteing code.

yes this worked I new it was very simple thank you very much.

I have another question.

Before I added a click event to the buttons created in the control array.

Is there a way to assign the positon based on what btn is clicked on to another array.

in other words if user clicks btn located at btnresvseat(1,1) I want to then assign that same location to the seating chart(1,1). Is there a way for VB to refer to the row, col of a two dimensional array. If this is not clear I will try to explain better.

thank you for your assistance
Nov 8 '06 #6

P: 9
yes i agree that is actually my next step and goal. wish me look I'm sure I will be back for more suggestions.
Nov 8 '06 #7

Expert 5K+
P: 8,434
yes this worked I new it was very simple thank you very much.
That's good news. :)

I have another question.

Before I added a click event to the buttons created in the control array.

Is there a way to assign the positon based on what btn is clicked on to another array.

in other words if user clicks btn located at btnresvseat(1,1) I want to then assign that same location to the seating chart(1,1). Is there a way for VB to refer to the row, col of a two dimensional array. If this is not clear I will try to explain better.

thank you for your assistance
Hm...

I'm going to assume that the array of buttons actually has only one dimensional, because I don't think it's possible to create a two-dimensional control array. Please tell me if I'm incorrect on this point - I certainly don't know everything about them.

For the sake of argument, let's assume you have an array of command buttons called Button arranged in a 5 x 5 grid on the form. As a control array, the index is passed to the Click event. So, here's a quick sample I've just tested in VB6 and pasted over here.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button_Click(Index As Integer)
  2.   Const MaxRows As Long = 5
  3.   Const MaxSeats As Long = 5
  4.   Dim TheColumn As Long, TheRow As Long
  5.   TheRow = Int(Index / MaxRows)
  6.   TheColumn = Index - MaxRows * TheRow + 1
  7.   TheRow = TheRow + 1
  8.   Debug.Print "Row:"; TheRow, "Seat:"; TheColumn
  9. End Sub
My button array started at zero, of course, which is why I'm adding 1.
Nov 8 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.