I'm making an application where I need "enemies" to pace back and forth, how would I automatically move the picture from left to right and repeat?
Heres my current code.
Private Sub Timer1_Timer()
enemy1.Left = enemy1.Left - 5
End Sub
Option Explicit
Const nTwipsPerMove = 15
Private Sub Timer1_Timer()
Static strDirection As String
If strDirection = "" Then strDirection = "left"
If enemy.Left > 0 Then
If strDirection = "left" Then
enemy.Left = enemy.Left - nTwipsPerMove
ElseIf strDirection = "right" Then
enemy.Left = enemy.Left + nTwipsPerMove
End If
End If
If enemy.Left = 0 Then
If strDirection = "left" Then strDirection = "right"
enemy.Left = enemy.Left + nTwipsPerMove
End If
If enemy.Left + enemy.Width = Me.Width - nTwipsPerMove * 5 Then
If strDirection = "right" Then strDirection = "left"
enemy.Left = enemy.Left - nTwipsPerMove
End If
End Sub
nTwipsPerMove means how many twips to move in each loop
Timer1.Interval = 10
I'm admitting right now that my VB is rusty. Been about 10 years since I've used it. I don't have anyway to test this code, but I think this sample should get you close, or at least get you pointed in the right direction.
Private Sub Timer1_Timer()
Do
enemy1.Left = enemy1.Left - 5
If enemy1.Left = < 5 Then
Do
enemy1.Right = enemy1.Right + 5
Loop Until enemy1.Right = > 1000 'or whatever your size is
End If
Loop
End Sub
you can also use 1 variable for its speed, and make it negative when the enemy has to move the other way
'1 form with :
' 1 timer : name=Timer1
' 1 picturebox : name=Picture1
Option Explicit
Private msngStep As Single
Private Sub Form_Load()
Timer1.Interval = 200 'delay in milliseconds between steps
msngStep = ScaleWidth / 20 'step size, and direction
End Sub
Private Sub Timer1_Timer()
Dim sngX As Single
With Picture1 'the enemy
sngX = .Left + msngStep 'calculate new position
If (sngX < 0) Or (sngX > (ScaleWidth - .Width)) Then 'check boundary
msngStep = msngStep * -1 'adjust direction
sngX = sngX + 2 * msngStep 'keep enemy inside
End If
.Left = sngX 'move to new position
End With 'Picture1
End Sub
VB (like C#) should have a built in Timer object. Using the timer you should be able to create a event handler that is triggered after a set amount of time, in that function you can move the picture and restart the timer if you still need it.
Related
I have some problems accessing informations from a new created element.This is my code
Private Sub c1_Click()
Refresh
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
c1.Caption = "Stop"
Else
c1.Caption = "Start"
End If
a = l.X2 - l.X1
choice = a
End Sub
Private Sub Form_Load()
Dim l As Line
Set l = Controls.Add("VB.Line", "l", Me)
With l
.X1 = 2760
.Y1 = 3000
.X2 = 5640
.Y2 = 3000
.Visible = True
.BorderStyle = 1
.BorderWidth = 2
.BorderColor = vbRed
End With
End Sub
I get an error when i click the button:"Object required".On the form i can see the line but i can't access it from another sub.Where is the problem?
Your Line variable l is private to the Form_Load procedure and goes out of scope.
Move Dim l As Line from Form_Load to the top of your form's code module (outside of any method or function). This will make it available to any method or procedure on your form:
Dim l As Line
Private Sub c1_Click()
Refresh
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
c1.Caption = "Stop"
Else
c1.Caption = "Start"
End If
a = l.X2 - l.X1
choice = a
End Sub
Private Sub Form_Load()
Set l = Controls.Add("VB.Line", "l", Me)
With l
.X1 = 2760
.Y1 = 3000
.X2 = 5640
.Y2 = 3000
.Visible = True
.BorderStyle = 1
.BorderWidth = 2
.BorderColor = vbRed
End With
End Sub
I am making a flowchart program. I did all of them, but I faced to this problem.
How to draw a line( a connection line) dynamically between two buttons in visual basic through getting a position of a mouse!!
So here's the code of it. I did how to get a position of a cursor, but i cant move forward.
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Dim z As POINTAPI
Private Sub Form_Load()
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
GetCursorPos z
Label1 = "x: " & z.x
Label2 = "y: " & z.y
End Sub
Please help me guys to fix this problem!!
Thanks in advance
Why do you need the position of the mouse?
have a look at the following test project:
'1 form with:
' 2 command buttons: name=Command1 name=Command2
Option Explicit
Private Sub ConnectButtons(cmd1 As CommandButton, cmd2 As CommandButton)
Dim sngX1 As Single, sngX2 As Single
Dim sngY1 As Single, sngY2 As Single
With cmd1
sngX1 = .Left + .Width
sngY1 = .Top + .Height / 2
End With 'cmd1
With cmd2
sngX2 = .Left
sngY2 = .Top + .Height / 2
End With 'cmd2
Line (sngX1, sngY1)-(sngX2, sngY2)
End Sub
Private Sub Form_Click()
ConnectButtons Command1, Command2
End Sub
Private Sub Form_Resize()
Command1.Move 120, 120
Command2.Move ScaleWidth / 2, ScaleHeight / 2
End Sub
When you run it, it will show 2 command buttons on a form, when you click on the form it will draw the connecting line
Resize the form to change the position of Command2 and click the form again
Pay attention to the order of Command buttons you pass to the ConnectButtons sub
Inserted my progress bar and it didn't work as expected. When i logged in, it works, but it doesn't automatically loads the second form.
Here is my code for progress/timer
Private Sub Timer1_Timer()
If ProgressBar1.Value = 100 Then
ProgressBar1.Value = 0
Else
ProgressBar1.Value = Val(ProgressBar1.Value) + Val(20)
End If
Label3.Caption = ProgressBar1.Value
Label , to detect 100, variable prg to hold
Private Sub Label3_Change()
If (Label3.Caption = 100) Then
prg = 1
Timer1.Interval = 0
End If
End Sub
log in button
Private Sub cmdSign_Click()
currentTime = TimeValue(Now)
strCurrdate = Format(Date, "mmm d, YYYY")
rx.Open "Select * from login where username ='" & Text1 & "' and password='" & Text2 & "'", db, 3, 3
If (counter = 3) And (rx.EOF = True) Then
MsgBox "You guessed too many times! Intruder alert!"
End
Else
If rx.EOF = True Then
MsgBox "Invalid Username or Password"
counter = counter + 1
Text1 = ""
Text2 = ""
Else
user1 = Text1.Text
logTime = currentTime
rxd.Open "Select * from logHistory", db, 3, 3
With rxd
.AddNew
.Fields("username") = user1
.Fields("TimeDate") = strCurrdate
.Fields("TimeIn") = logTime
.Update
End With
Set rxd = Nothing
'problem might be here
ProgressBar1.Visible = True
Timer1.Interval = 100
Label3.Visible = True
Timer1.Enabled = True
If prg = 1 Then
MsgBox "Welcome " + Text1 + " to SPARTAN!"
mainmenu.Show
End If
End If
End If
Set rx = Nothing
End Sub
Any help on this?
Once the timer is started it runs asynchronously to the rest of your code. The way you have coded your login button you are expecting the timer to move the progressbar from 0 to 100, set your (I assume) module scoped variable prg to 1 and then continue. In reality your code enables the timer and moves on. When it reaches the If prg = 1 Then statement prg is still whatever it has been initialized at. One way to fix it is to check the prg variable in your timer event.
Private Sub Timer1_Timer()
If ProgressBar1.Value = 100 Then
Timer1.Enabled = False ' don't fire the timer event again
ProgressBar1.Value = 0
MsgBox "Welcome " & Text1 & " to SPARTAN!"
mainmenu.Show
Else
ProgressBar1.Value = Val(ProgressBar1.Value) + Val(20)
End If
Label3.Caption = ProgressBar1.Value
End Sub
I also changed your string concatenation + to &. The plus symbol "+" works, but is only included in VB versions after 3 for backward compatibility and it is considered bad form to use it for other than arithmetic. In VB version 4 and greater the ampersand "&" should be used for concatenation. MSDN reference here
I have a flexgrid with a grouping, and a .subtotal by that grouping. All columns except one are numeric, the one that isn't is in the format 'x/y' e.g. '1/5', i.e. 1 out of 5 items supplied.
if I do a .Subtotal with a flexSTSum it sums up the first number in the pair, i.e. in the above example it would sum up the 1 as a decimal and show 1.00 in the subtotal row
At first I tried to find a way to sum on another column, i.e. I could put individual values into separate columns, give them a .Width of 0 and sum these into the .Subtotal column of the first column, but I can't find a way to do that.
And even if I do find a way to do that I want to be able to custom format the .Subtotal, so it appears as '3/17', i.e. '1/5' and '2/12' subtotal to '3/17' in the subtotal row.
if I can't subtotal off another column I wondered if I could custom access the subtotal row and manually enter the subtotal value of '3/17', but even that seems unavailable.
My question is, is there a way to achieve this?
I assume you are using the VideoSoft FlexGrid which i never used, so I can't help you with the specific methods of that control.
You can do it easily with a standard MSFlexGrid control though, and you can probably do the same with the VideoSoft FlexGrid.
Have a look at the following sample project:
'1 form with :
' 1 msflexgrid control : name=MSFlexGrid1
Option Explicit
Private Sub Form_Load()
Dim lngRow As Long, lngCol As Long
With MSFlexGrid1
.Rows = 10
.Cols = 4
.FixedRows = 0
.FixedCols = 0
For lngRow = 0 To .Rows - 2
For lngCol = 0 To .Cols - 2
.TextMatrix(lngRow, lngCol) = CStr(100 * lngRow + lngCol)
Next lngCol
.TextMatrix(lngRow, .Cols - 1) = CStr(lngRow) & "/" & CStr(lngRow * lngRow)
Next lngRow
End With 'MSFlexGrid1
End Sub
Private Sub Form_Resize()
MSFlexGrid1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub MSFlexGrid1_Click()
Dim lngCol As Long
'calculate subtotals
With MSFlexGrid1
For lngCol = 0 To .Cols - 2
.TextMatrix(.Rows - 1, lngCol) = CStr(GetTotal(lngCol))
Next lngCol
.TextMatrix(.Rows - 1, .Cols - 1) = GetTotalSpecial(.Cols - 1)
End With 'MSFlexGrid1
End Sub
Private Function GetTotal(lngCol As Long) As Long
Dim lngRow As Long
Dim lngTotal As Long
With MSFlexGrid1
lngTotal = 0
For lngRow = 0 To .Rows - 2
lngTotal = lngTotal + Val(.TextMatrix(lngRow, lngCol))
Next lngRow
End With 'MSFlexGrid1
GetTotal = lngTotal
End Function
Private Function GetTotalSpecial(lngCol As Long) As String
Dim lngRow As Long
Dim lngTotal1 As Long, lngTotal2 As Long
Dim strPart() As String
With MSFlexGrid1
lngTotal1 = 0
lngTotal2 = 0
For lngRow = 0 To .Rows - 2
strPart = Split(.TextMatrix(lngRow, .Cols - 1), "/")
If UBound(strPart) = 1 Then
lngTotal1 = lngTotal1 + Val(strPart(0))
lngTotal2 = lngTotal2 + Val(strPart(1))
End If
Next lngRow
End With 'MSFlexGrid1
GetTotalSpecial = CStr(lngTotal1) & "/" & CStr(lngTotal2)
End Function
It will load a grid with some values, and when you click on the grid, the subtotals will be calculated and filled into the last row.
I am doing a project in vb6..i want to know how to keeping changing the background image of the MDI for as soon as it is loaded.
i tried to make an array of images and then set the timer
here is my code
Private Sub Timer1_Timer()
For i = 0 To 2
Picture1.Picture = LoadPicture(arr(i))
i = i + 1
If i = 3 Then
i = 0
End If
Next i
End Sub
Private Sub MDIForm_Load()
arr(0) = "images\Shop.jpg"
arr(1) = "images\Display1.jpg"
arr(2) = "images\Display2.jpg"
end sub
please help
thank you
Couple of issues with your code as written. First, you don't need to use a picturebox, the MDI form should have a Picture property you can set directly.
Second, as written you're cycling through all the images in your array each time the timer event fires. What you really want is one change per timer event and store/increment the array index at the end of each timer event, like so:
Private Sub Timer1_Timer()
MDIForm.Picture = LoadPicture(arr(arrIndex))
If arrIndex + 1 <= UBound(arr) Then
arrIndex = arrIndex + 1
Else
arrIndex = 0
End If
End Sub
Dim arrIndex as Integer
Private Sub MDIForm_Load()
arr(0) = "images\Shop.jpg"
arr(1) = "images\Display1.jpg"
arr(2) = "images\Display2.jpg"
arrIndex = 0
end sub
The Dim of arrIndex should be at the top of your MDIForm. This will change the background picture every time the timer event fires.