I am a beginner in VB, and I tried making a progress bar from two panels, but the front one isn't moving. Code:
Public Class Startup
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Front.Width < Me.Width Then
Front.Width = Front.Width + 10
End If
If Front.Width = "1366" Then
Timer1.Stop()
Login.Show()
Me.Close()
End If
End Sub
What INDEX said is absolutely correct!
You should be testing against a number not a string
If Front.Width = 1366 Then
Then note his/her second point!
You are incrementing in steps of 10, you will pass by 1366 without hitting it!
Therefore your should change the IF statement to
If Front.Width >= 1366 Then
Related
Here is where I have tried to implement the code to change the location of the picture box but it doesn't seem to be working, I want the picture to move to the right:
Public Class Form1
Dim mypicturebox As New PictureBox
Dim randval As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Label1.Text = "hello " & TextBox1.Text
Timer1.Enabled = True
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 2000
Timer1.Enabled = True
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y + 5)
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size +
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y)
End Sub
End Class
Your current Timer handler - if the Timer is actually firing - is just going to drift your PictureBox off to the right by 5px each Timer period.
If you want a random position you need a way to get a random number. Random is your best bet.
You also need to ensure that the generated random position is "valid" for both the size of your screen and the size of the PictureBox otherwise the PictureBox might be either entirely invisible (off screen) or partially visible.
Here's some basic code to get two random numbers between -100 and 100 to get you started:
Dim myRand As Random = New Random(Now.Second)
Dim newX As Integer = myRand.Next(-100, 100)
Dim newY As Integer = myRand.Next(-100, 100)
(Now.Second seeds the generation of myRandom so that it should generate different starting numbers each time.)
Then you can use these as your PictureBox location points:
mypicturebox.Location = New Point(newX, newY)
Play around with limiting your locations and you should get what you want.
UPDATE
Ok, scrap all that as you've changed your requirement description ...
Just add whatever offset you need to your X position and you're done, provided your Timer is firing. You still need to watch your screen limits.
I have an assignment for school where I have an animated character that runs. I need to be able make him jump and return to the spot he started the jump after a button press. I have a timer that switches images to animate the character. There are also buttons to speed up, slow down, start, and stop the character. I'm having trouble because I think I have to integrate the timer into the button press and I'm not too sure how to to that. There is a space at the bottom of the code where the jump button is. I'm able to make him go up but can't figure out how to make him come back down.
Here is my code so far:
Private Sub tmrSpeed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpeed.Tick
Static Dim intCounter As Integer
picRunner.Location = New Point(picRunner.Location.X + 15, picRunner.Location.Y)
If picRunner.Location.X >= 523 Then
picRunner.Location = New Point(-25, picRunner.Location.Y)
End If
Select Case intCounter
Case 0
Me.picRunner.Image = My.Resources.r0
Case 1
Me.picRunner.Image = My.Resources.r1
Case 2
Me.picRunner.Image = My.Resources.r2
Case 3
Me.picRunner.Image = My.Resources.r3
Case 4
Me.picRunner.Image = My.Resources.r4
Case 5
Me.picRunner.Image = My.Resources.r5
Case 6
Me.picRunner.Image = My.Resources.r6
Case 7
Me.picRunner.Image = My.Resources.r7
Case 8
Me.picRunner.Image = My.Resources.r8
Case 9
Me.picRunner.Image = My.Resources.r9
End Select
intCounter += 1
If intCounter >= 10 Then
intCounter = 0
End If
End If
End Sub
Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
Try
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval - 10
Catch x As Exception
MessageBox.Show("He can't run any faster!")
End Try
End Sub
Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval + 10
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.tmrSpeed.Stop()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
Me.tmrSpeed.Start()
End Sub
Private Sub btnJump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJump.Click
End Sub
End Class
How would I go about implementing the jump? My teacher says I have to use a loop, obviously to check when the picturebox reaches a certain hot but I can't figure out how to use this with the timer. Any help would be appreciated.
set a loop that calculates force.
also make a variable for the rate of decay for that force.
have the loop apply the force by subtracting it from the current y position
and after it apply force subtract the rate of decay from it to decrease the force so the next time the loop happens the force is lesser and it begins slowing down.
also have a global variable called gravity to create a constant pull on the object downwards so when the upward force decays it will go back down.
The rest you must figure out, if this is an assignment then it is very important for you to develop the logic to do what i described without someone giving you the answer. It will give you better understanding of the code.
I had the exact same assignment, and I came to this following code as my solution. I set the initial interval of the timer to 999999 so that the form wouldn't load with the animation jumping.
Private Sub btnJump_Click(sender As Object, e As EventArgs) Handles btnJump.Click
Me.tmrJump.Interval = 100
Me.tmrJump.Start()
End Sub
Private Sub tmrJump_Tick(sender As Object, e As EventArgs) Handles tmrJump.Tick
Static intOldTop As Integer
Static intCounter As Integer
Dim intStep As Integer = 5
If intCounter = 0 Then intOldTop = picSpaceRunner.Top
intCounter += 1
Select Case intCounter
Case 1 To 15
picSpaceRunner.Top -= intStep
Case 16 To 30
picSpaceRunner.Top += intStep
Case Else
picSpaceRunner.Top = intOldTop
intCounter = 0
tmrJump.Stop()
End Select
End Sub
End Class
I'm making a simple progress bar to be used as a splash screen within my application but when the code is executed, the loading bar does not reach the end of the progress bar.
I've used the code:
splashprogressbar.Increment(1)
If splashprogressbar.Value = 100 Then
Main_Menu.Show()
Me.Hide()
End If
to open a form when the progress bar reaches 100, which has been set as the maximum value.
The issue is more so related to appearance rather than functionality but i would still like to understand why this occurs and hopefully get a fix.
To clarify, the form Main_Menu, opens when the bar is about 3/4 of the way completed and i can't get my head around why this occurs. Any ideas?
I couldn't imagine that VB has such a bug. Probably you are running an old or incompatible ".Net Framework" that causes that.
You can try to set your Progressbars maximum programatically while running the program.
Use this code in the Form1_Load event:
ProgressBar1.Maximum = 100
It should work. But if it doesn't, maybe it's just code related. Try to fill your progressbar this way (It must be in a timer):
If splashprogressbar.Value = splashprogressbar.Maximum Then
Main_Menu.Show()
Me.Hide()
Else
splashprogressbar.Value += 100
End If
Just a friendly advice: It seems you're simulating a fake loading bar which fills and then, only then, open the app (Potentially wasting the users time). Don't do that.
You could also try taking a look at the following link. This may help with your approach;
Trigger Background Worker
The below was copied from the link above...
Dim WithEvents bgWorker As New BackgroundWorker With { _
.WorkerReportsProgress = True, _
.WorkerSupportsCancellation = True}
Private Sub bgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
For i As Integer = 0 To 100
'Threw in the thread.sleep to illustrate what's going on. Otherwise, it happens too fast.
Threading.Thread.Sleep(250)
bgWorker.ReportProgress(i)
Next
End Sub
Private Sub bgWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged
If e.ProgressPercentage Mod 10 = 0 Then
MsgBox(e.ProgressPercentage.ToString)
End If
End Sub
Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
MsgBox("Done")
End Sub
you could try something like this;
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Show()
Dim i As Integer
splashprogressbar.Minimum = 0
splashprogressbar.Maximum = 100
If splashprogressbar.Value < splashprogressbar.Maximum Then
For i = 0 To 100
splashprogressbar.Value = i
Application.DoEvents()
System.Threading.Thread.Sleep(100)
Next
End If
Me.Hide()
MsgBox("Here I am") 'Use your "Main_Menu.Show" here
End Sub
Is there some kind of update feature like in C# for Visual Studio 2012 in vb.net? (so that it will continuously check to see if an if statement is fulfilled)
Try something like this:
Dim iClicks As Integer = 0
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
iClicks += 1
Label1.Text = iClicks.ToString()
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
It's better to have a integer counter than checking the string value and performing conversion each time. As you can see, the code checks the click counter each time you click the label.
If you want to check the value periodically, add a Timer and perform the check in its event:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
But consider the need to do this, because maybe it's not necessary and will decrease the performance comparing it with the other way.
There is probably a better way of doing this, but what I've done when I need a constant "update" function, is just use a Timer, then handle the Ticks.
Easiest way of implementing it is to go to the form designer, in the Toolbox, under Components, drag a timer onto the form. Double-click it to add a handle for its Tick event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If WhateverYoureLookingFor = True
'Do stuff
End If
End Sub
This is obviously VB, but easy to convert to C#.
I have 3 PictureBoxes on a form that are tiled on top of each other. the form has a minimum vaule of (502, 416) and a maximum of (502, 1080).
because the user can select from a MenuStrip to display '1', '2' 0r '3' PictureBoxes at once '3' being the bottom and '1' being the top PictureBox. What i need is when the user drags down the form it snaps to the next PictureBoxes position, so it goes down in blocks this is what i have so far which is pretty far from working.
If Me.Height <= (1079) Then
Me.Height = (732)
ElseIf Me.Height <= (732) Then
Me.Height = (424)
ElseIf
...
End If
I also thought i might be able to figure it out if i new how to create a variable like this
If Me.Height <= (1079 to 733) Then
Me.Height = (732)
I know that it isn't the correct syntax but it's kind of the idea
If you can make sense from my not so good description and point me in the right direction/code example i will be most grateful :)
Thank you for your help
You could try using a Select Statement:
Select Case Me.Height
Case 425 To 732
Me.Height = 424
Case 733 To 1079
Me.Height = 732
End Select
To answer your further question. If you are going to use the Form Resize event any animation you have is going to get interesting because as you change the Form Height it will retrigger the Event. Personally if I were you I would stay with your initial idea of snapping to the next height and if you are wanting to animate the Form Height I would seriously look into WPF. But here is the SubRoutine that I said I would show you, I have used three buttons to intiatiate the resizing. Be carefull if you put this in your Form Resize Event if you don't block the event from rerunning the Subroutine it will freeze your computer.
Public Class Form1
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
Me.Text = Me.Height
End Sub
Public Sub ChangeFormHeight(fromHeight As Integer, toHeight As Integer)
If fromHeight > toHeight Then
For newHeight As Integer = fromHeight To toHeight Step -1
Me.Height = newHeight
Next
Else
For newHeight As Integer = fromHeight To toHeight
Me.Height = newHeight
Next
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ChangeFormHeight(Me.Height, 424)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ChangeFormHeight(Me.Height, 733)
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
ChangeFormHeight(Me.Height, 1080)
End Sub
End Class