Thank God i found this site.
my first day in VB and i was given an assignment to simulate how a lift runs in VB with 4 floors. pls can any one help me so fa i have been able to design the interface. please i need help in writing the code...please a response will be HIGHLY appreciated. im good in oracle database but im firnding VB quite challenging. pls help deadline is fast approaching
tol 0 Newbie Poster
Teme64 215 Veteran Poster
Here are the things to consider:
- what does an elevator do?
- how do you represent 4 floors in VB (a clue: you need a data structure)
jireh 3 Posting Whiz
Welcome to DANIWEB tol...
Can you post your code here, then we'll give you back-up!
tol 0 Newbie Poster
hi all, this is a copy of my code.
Though it still has some problems as sometimes its still hanging as some floors still has some problems. and my tutor was talking about using a boolean variable to relate it to the floors i.e buttons G,1,2,3 buts i dont really get what he means thats why i came to daniweb. please can anyone help me to make my even code better and time is of the essence. sometimes i wished I just stayed with Database Administration as i am good in DB programming.
I am determined to learn.
Public Class Form1
Dim floor, liftposition As Integer
Private Sub tmropen_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmropen.Tick
pnllift.Width = pnllift.Width - 1
If pnllift.Width = 0 Then
tmropen.Stop()
End If
End Sub
Private Sub tmrup_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrup.Tick
Select Case floor
Case 0
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top - 1
If pnllift.Top = pnlgroundfloor.Top Then
tmrup.Stop()
tmropen.Start()
liftposition = 0
End If
End If
Case 1
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top - 1
If pnllift.Top = pnlfloor1.Top Then
tmrup.Stop()
tmropen.Start()
liftposition = 1
End If
End If
Case 2
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top - 1
If pnllift.Top = pnlfloor2.Top Then
tmrup.Stop()
tmropen.Start()
liftposition = 2
End If
End If
Case 3
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top - 1
If pnllift.Top = pnlfloor3.Top Then
tmrup.Stop()
tmropen.Start()
liftposition = 3
End If
End If
End Select
End Sub
Private Sub btn2up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2up.Click
'Up floor 1 button
floor = 1
btn2up.BackColor = Color.Black
tmrup.Start()
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub btn1up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1up.Click
'Up floor 1 button
floor = 0
btn1up.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub btn2down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2down.Click
floor = 1
btn2down.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub btn3up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3up.Click
floor = 2
btn3up.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub btn4down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4down.Click
floor = 3
btn4down.BackColor = Color.Black
If liftposition > floor Then
if pnllift.Width <>79 then tmrclose.Start
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub tmrclose_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrclose.Tick
pnllift.Width = pnllift.Width + 1
If pnllift.Width = 79 Then
tmrclose.Stop()
End If
End Sub
Private Sub tmrdown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrdown.Tick
Select Case floor
Case 0
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top + 1
If pnllift.Top = pnlgroundfloor.Top Then
tmrdown.Stop()
tmropen.Start()
liftposition = 0
End If
End If
Case 1
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top + 1
If pnllift.Top = pnlfloor1.Top Then
tmrdown.Stop()
tmropen.Start()
liftposition = 1
End If
End If
Case 2
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top + 1
If pnllift.Top = pnlfloor2.Top Then
tmrdown.Stop()
tmropen.Start()
liftposition = 2
End If
End If
Case 3
If pnllift.Width = 79 Then
pnllift.Top = pnllift.Top + 1
If pnllift.Top = pnlfloor3.Top Then
tmrdown.Stop()
tmropen.Start()
liftposition = 3
End If
End If
End Select
End Sub
Private Sub btn3down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3down.Click
floor = 2
btn3down.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''pnllift.Height = 15
''pnllift.Width = 15
''pnlgroundfloor.t()
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfloor1.Click
floor = 1
btn2down.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
Private Sub btnGroundfloor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGroundfloor.Click
'Up floor 1 button
floor = 0
btn1up.BackColor = Color.Black
If liftposition > floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrdown.Start()
ElseIf liftposition < floor Then
If pnllift.Width <> 79 Then tmrclose.Start()
tmrup.Start()
Else
tmropen.Start()
End If
End Sub
End Class
Teme64 215 Veteran Poster
First, this is the VB 4/5/6 forum. You're coding with VB.NET which is a different forum in DaniWeb.
There's not a single, right solution. Here's one solution without any fancy UI, this more about the "logic" of an elevator.
First, you have 4 floors and an elevator can be only at the one floor at the time. Second, elevator can either move up or down (or stay in the same floor). So use a boolean array to represent floors and the elevator. Then you need methods to move elevator (up or down).
Public Class Form1
' Here's your floors and elevator
Private ElevatorAtFloor(3) As Boolean
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Initialize our elevator to G floor
ElevatorAtFloor(0) = True
ElevatorAtFloor(1) = False
ElevatorAtFloor(2) = False
ElevatorAtFloor(3) = False
End Sub
Private Sub MoveUp(ByVal NumberOfFloors As Integer)
' If this is not the top floor, move elevator up
Dim i As Integer
' First locate where the elevator is
For i = 0 To ElevatorAtFloor.GetUpperBound(0)
If ElevatorAtFloor(i) Then
' Elevator is at the floor i, can we move up NumberOfFloors
If i + NumberOfFloors <= ElevatorAtFloor.GetUpperBound(0) Then
' Yes we can
ElevatorAtFloor(i) = False
ElevatorAtFloor(i + NumberOfFloors) = True
Else
' No. Move to top floor
ElevatorAtFloor(i) = False
ElevatorAtFloor(ElevatorAtFloor.GetUpperBound(0)) = True
End If
Exit Sub ' We moved up and now exit sub
End If
Next i
End Sub
Private Sub MoveDown(ByVal NumberOfFloors As Integer)
' If this is not the G floor, move elevator down
Dim i As Integer
' First locate where the elevator is
For i = 0 To ElevatorAtFloor.GetUpperBound(0)
If ElevatorAtFloor(i) Then
' Elevator is at the floor i, can we move down NumberOfFloors
If i - NumberOfFloors >= 0 Then
' Yes we can
ElevatorAtFloor(i) = False
ElevatorAtFloor(i - NumberOfFloors) = True
Else
' No. Move to G floor
ElevatorAtFloor(i) = False
ElevatorAtFloor(0) = True
End If
Exit Sub ' We moved down and now exit sub
End If
Next i
End Sub
Private Sub MoveToFloor(ByVal ThisFloor As Integer)
' Move the elevator at the floor number ThisFloor
Dim i As Integer
' Check that ThisFloor is a valid value
If ThisFloor < 0 OrElse ThisFloor > ElevatorAtFloor.GetUpperBound(0) Then
' Not a valid floor. Exit
Exit Sub
End If
' First locate where the elevator is
For i = 0 To ElevatorAtFloor.GetUpperBound(0)
If ElevatorAtFloor(i) Then
' Now we know where the elevator is
' Determine if this is the right floor or do we have to move elavator up or down
If i < ThisFloor Then
' Move elevator up
MoveUp(ThisFloor - i) ' ThisFloor - i is the number of the floors we need to move up
ElseIf i > ThisFloor Then
' Move elevator down
MoveDown(i - ThisFloor) ' i - ThisFloor is the number of the floors we need to move down
'Else
' ' This is the right floor: i = ThisFloor
End If
Exit Sub ' We moved the elevator to ThisFloor and now exit sub
End If
Next i
End Sub
Private Sub ShowFloor()
' Show the floor number (G, 1-3) in a Label control
Dim i As Integer
' First locate where the elevator is
For i = 0 To ElevatorAtFloor.GetUpperBound(0)
If ElevatorAtFloor(i) Then
If i = 0 Then
Label1.Text = "G"
Else
Label1.Text = i.ToString
End If
End If
Next i
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' Get the floor number from the TextBox1
Select Case TextBox1.Text.ToUpper
Case "G"
MoveToFloor(0)
Case "1"
MoveToFloor(1)
Case "2"
MoveToFloor(2)
Case "3"
MoveToFloor(3)
End Select
ShowFloor()
End Sub
End Class
Like I said, this not the only solution but has the basic "logic" and its implementation. That's what your prof/teacher is looking for. You may be a bit carried away with fancy UI instead of concentrating in the problem solution in itself in you code. But we are all learning all the time :)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.