-
Nov 30th, 2011, 09:22 AM
#1
Thread Starter
New Member
making random picture boxes go on the form
hi
i am making a game in visual basic and when the picture box that you have to shoot resets, sometimes it resets off the form i use this code to make the picture box move randomly :
PbxMet.Top = Int(Rnd() * (Me.Height - PbxMet.Height))
key :
pbxmet = picture box you have to shoot
me = the form
anyone know how to keep it on the form
thanks in advance
-
Nov 30th, 2011, 10:11 AM
#2
Re: making random picture boxes go on the form
Blurgh! Don't use Rnd. Use the Random class and its Next method. Create one Random object and use it repeatedly:
vb.net Code:
myControl.Top = myRandom.Next(Me.Height - myControl.Height)
-
Nov 30th, 2011, 10:19 AM
#3
Thread Starter
New Member
Re: making random picture boxes go on the form
how do you create a random?
-
Nov 30th, 2011, 10:47 AM
#4
Thread Starter
New Member
Re: making random picture boxes go on the form
o found it
thats much better thanks
do you know how to keep it in the from thou because when i test it sometimes i have to wait ages for it to come back at the player.
maybe its my code does this look right to you?
If PbxMet.Width = 1 < 99 Or PbxMet.Left = -20 Then
PbxMet.Left = 1253
count -= 1
PbxMet.Top = rnd1.Next(Me.Height - PbxMet.Height)
PbxMet.Width = rnd2.Next(Me.Height - PbxMet.Height)
If PbxMet.Width >= 100 Then
PbxMet.Width = rnd2.Next(Me.Height - PbxMet.Height)
If PbxLazer.Width >= 100 Then
PbxMet.Width = 100
End If
End If
PbxMet.Height = PbxMet.Width
PbxMet.Visible = True
End If
-
Nov 30th, 2011, 12:54 PM
#5
Re: making random picture boxes go on the form
This isn't right:
If PbxMet.Width = 1 < 99
That should be written:
If PbxMet.Width = 1 AndAlso pbxMet.Width < 99 OrElse pbMet.Left = -20 Then
Actually, that's probably not right. I'm just guessing what you meant with 1<99. In any case, what you wrote will evaluate to the wrong thing. I believe that what you have will ALWAYS evaluate that first part to False, because 1<99 is always True, but the second part compares .Width to True, which is probably always False. It shouldn't even compile, though.
That may be the whole issue.
My usual boring signature: Nothing
-
Nov 30th, 2011, 01:48 PM
#6
Re: making random picture boxes go on the form
Originally Posted by samqweqwe3
do you know how to keep it in the from thou because when i test it sometimes i have to wait ages for it to come back at the player.
Moving and resizing controls is slow and not recommended, but if you want to do it that way try doing the math using variables first then set the control properties only once. On my PC the code in button1 took close to 300ms to complete while the code in button2 took less then 0.35ms...
Code:
Private rnd1 As New Random
Private rnd2 As New Random
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim sw As Stopwatch = Stopwatch.StartNew
' simulate 1000 calcs
For i As Integer = 1 To 1000
PbxMet.Top = rnd1.Next(Me.Height - PbxMet.Height)
PbxMet.Width = rnd2.Next(Me.Height - PbxMet.Height)
If PbxMet.Width >= 100 Then
PbxMet.Width = rnd2.Next(Me.Height - PbxMet.Height)
End If
PbxMet.Height = PbxMet.Width
Next
' show elapsed time
sw.Stop()
Debug.Print((sw.ElapsedTicks / Stopwatch.Frequency * 1000).ToString("0.00"))
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim sw As Stopwatch = Stopwatch.StartNew
' Save control props to variables
Dim pTop As Integer = PbxMet.Top
Dim pWidth As Integer = PbxMet.Width
' do the math, simulate 1000 calcs
For i As Integer = 1 To 1000
pTop = rnd1.Next(Me.Height - PbxMet.Height)
pWidth = rnd2.Next(Me.Height - PbxMet.Height)
If pWidth >= 100 Then
pWidth = rnd2.Next(Me.Height - PbxMet.Height)
End If
Next
' set control props
PbxMet.Top = pTop
PbxMet.Size = New Size(pWidth, pWidth)
' show elapsed time
sw.Stop()
Debug.Print((sw.ElapsedTicks / Stopwatch.Frequency * 1000).ToString("0.00"))
End Sub
-
Nov 30th, 2011, 02:52 PM
#7
Thread Starter
New Member
Re: making random picture boxes go on the form
thanks
that helps alot
-
Nov 22nd, 2012, 02:45 AM
#8
New Member
Re: making random picture boxes go on the form
Hello,
I am pretty new to programming but I have a project due for school and I am struggling with i believe syntax errors. my program is a simple windows forms app it has four picture boxes and I am trying to display random pictures in the picture boxes.
I have a background image and then 3 other images in my resources that I would like to create a random generating subroutine to call the three other pictures. the current code I have is :
Private Sub Elephant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Elephant.Click
Elephant.Image = My.Resources.ElephantCover
Const File1 As String = Elephant1
Const File2 As String = Elephant2
Const File3 As String = Elephant3
End Sub
Private Sub Giraffe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Giraffe.Click
Giraffe.Image = My.Resources.GiraffeCover
End Sub
Private Sub Lion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lion.Click
Lion.Image = My.Resources.LionCover
End Sub
Private Sub Monkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Monkey.Click
Monkey.Image = My.Resources.MonkeyCover
End Sub
End Class
where I have not began to work on the other private subs I want to make sure I get the first one to work properly. I have attempted several other suggestions of code from other posts but I believe many of them are using an older version of VB and the answer is just not coming to me.
any help would be mch appreciated.
Thank you,
-
Nov 22nd, 2012, 11:37 AM
#9
Re: making random picture boxes go on the form
Why did you append this to a thread that is more than a year old? This should be a new thread all on its own.
However, you might want to do a couple things:
1) Wrap the code in code tags so that it will format properly for display.
2) Ask a question.
Frankly, it isn't clear what it is you are looking for. You talk about wanting to make a random generator, you talk about syntax issues, then you talk about not wanting to do the other private subs until you get the first one working properly. So, it isn't clear what you are working on, whether it is the random generator, or if that is part of the set of "other private subs" that you don't want to deal with, yet. It isn't clear whether the syntax issues are resulting in error messages, and if so, what are they and where are they. Or, possibly you are asking about how to make a random generator, but from what you have shown, it isn't clear how you want that to work. So, overall, I have no idea what you are even looking for.
My usual boring signature: Nothing
-
Nov 22nd, 2012, 02:10 PM
#10
New Member
Re: making random picture boxes go on the form
As I stated I am a beginner thus I did not know I would need to wrap the code when posting to this forum. I posted to this thread because rather than making a new thread on
the same exact topic I tried to do my own research and I was lead here, where I asked for help. Is that not why these old threads are left on the forum so long? I apologize for not being clear in my original post. I am trying to create a random generator for a picture box. I believe I have accomplished it through trial and error. here is where I am at.
Private Sub Elephant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Elephant.Click
Elephant.Image = My.Resources.ElephantCover
Dim dice As New Random
Dim roll As Integer
roll = dice.Next(1, 5)
If roll = 1 Then
Elephant.Image = My.Resources.Elephant1
ElseIf roll = 2 Then
Elephant.Image = My.Resources.Elephant2
ElseIf roll = 3 Then
Elephant.Image = My.Resources.elephant3
Else
Elephant.Image = My.Resources.ElephantCover
End If
Did I wrap the code correctly? idk if thats right.
So this works though I have some more image editing to do (i know) when a user clicks on picturebox (ElephantCover) the image will randomly change to one of the other
three photos stored within my resources. After I complete the editing of the images and get this in all of the Private subs I will then try and add sound files to each of the
images, I have not even done my research on the sound part to be honest and I dont even know if it is something VB can do. Do you know if it can?
Now that I have figured this out and made it at least work, is this correct code? I imagine there are several ways to do this, but what do you think? any input? I have some
work to do on images and then I will get the rest of the code for the other three private subs and then I will do some research on adding sounds. Do you know of anywhere I
can even get sounds?
This is something I am working on for a school project. I attempted making this in a Windows 8 store app but I was having wierd
issues with VMFusion and VMware so I decided I would just do it in a windows forms application.
Thank you for your response and input any help is always appreciated.
Ken
-
Nov 22nd, 2012, 02:19 PM
#11
Re: making random picture boxes go on the form
vb.net Code:
Private Sub Elephant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Elephant.Click
Elephant.Image = My.Resources.ElephantCover
Dim dice As New Random ' this should be moved to the head of the form so that it can be used in all subs
Dim roll As Integer
roll = dice.Next(1, 5)
If roll = 1 Then ' this would be a good place to use a Select Case rather than If but don't worry about it now
Elephant.Image = My.Resources.Elephant1
ElseIf roll = 2 Then
Elephant.Image = My.Resources.Elephant2
ElseIf roll = 3 Then
Elephant.Image = My.Resources.elephant3
Else
Elephant.Image = My.Resources.ElephantCover ' not strictly necessary as you set this at the beginning of the sub
End If
End Sub
Sound is easy if you have it in .wav files, a little more complicated otherwise. Google free .wav files animal sounds and you'll be drowning in them!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Nov 22nd, 2012, 02:27 PM
#12
New Member
Re: making random picture boxes go on the form
OK so that is what I have in there basically.is there a way I can make the image go full screen once it is clicked?
-
Nov 22nd, 2012, 02:32 PM
#13
Re: making random picture boxes go on the form
Put a docked picture box on a separate form and show it maximised?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Nov 23rd, 2012, 03:52 PM
#14
New Member
Re: making random picture boxes go on the form
what do you mean "Put a docked picture box on a separate form and show it maximised?" I have formatted the pictures that will be randomly selected when a user clicks the picture box to 1024x576 I want to make the picture box go full screen when it is displaying the
random picture.
thanks
-
Nov 23rd, 2012, 05:37 PM
#15
Re: making random picture boxes go on the form
Originally Posted by kfritts
As I stated I am a beginner thus I did not know I would need to wrap the code when posting to this forum. I posted to this thread because rather than making a new thread on
the same exact topic I tried to do my own research and I was lead here, where I asked for help. Is that not why these old threads are left on the forum so long? I apologize for not being clear in my original post. I am trying to create a random generator for a picture box. I believe I have accomplished it through trial and error. here is where I am at.
I applaud your reasoning. The code tags were easier before the forum upgrade, and are easier to miss now.
As a general rule, when you ask a question here you want the maximum number of eyeballs on the question that you can get. For this reason, you are better off starting a new thread for a new question, even if the question is closely related to an existing, though older, thread. If you started the original thread, then this might not be the case, but even then it's a matter of opinion. I think that people are more likely to look at a thread with few or no replies rather than one with lots of replies, so you get to decide. You get more views with a new thread, but if the question is clearly a continuation of a question you had asked in a different thread, then it might annoy some people since you have lost the context of the previous question.
Technically, bumping a thread that has fallen off the first page would be justified, since I would expect that most people don't go more than one or two pages at most. However, bumping a thread forward too often is considered bad form. Still, you'd certainly be ok bumping a thread that had fallen off the second page.
Now to get back to the actual question:
What dunfiddlin was suggesting was that a picture is generally shown in a picturebox, so he was suggesting that rather than making it go full screen, you would put the image into a picturebox that took up all of a different form, then show that other form over the one you have. That may or may not work the way you want. An alternative would be to show the image in a picturebox on the current form and set the position and size of the picturebox to take up the whole screen. The concern there is that the image might end up badly distorted, since you may be changing the height to width ratio for the image. It might also end up really nasty looking if the image was small enough that it didn't stretch well.
One other point is that you want to get that declaration for dice out of the click event. As a rule, you should declare Random objects only at the highest level you can. Having one at form scope (not inside any method) is the best option in this case. The reason for this is that when you create the Random object (with New), the random number generator gets seeded with the current system time. Any two generators that get the same seed will produce the same sequence of numbers. Therefore, if you are creating Random objects inside a tight loop, a whole series would get seeded with the same time because the time wouldn't have advanced between calls, in which case the objects will produce highly non-random numbers. Using just one Random object at form scope solves that, though you technically wouldn't have that problem in your current code, since click events just can't happen fast enough.
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|