How to randomize a picture box within a certain area - vb.net

So,I am trying to make a game that is based on collecting items to earn points and the objective is to always try and beat your high score. I have make it so the crate randomises at a random position anywhere on the form, I have a background of a city in my form, so I now want it to randomise only on the roads instead of anywhere on the form, I have also created a picture box for each of the roads and had put it into an array,but I don't know if that would be of any use, so i am kinda stuck now. Can anyone help me with this problem?
Thx

This one works but try it first on a seperate program and
after you try it. You will be given a chance on how you will
do the randomize on your own.
Create a program
Add a new form (Form1)
Add a picturebox and a button
Import your moms picture inside the picturebox (Remember : it will not work if you dont do it XD)
Inside your form add this piece of code:
Public Class Form1
Dim RandomClass As New Random()
Dim Y As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For pos = 1 To 100
Y = RandomClass.Next(300)
PictureBox1.Location = New Point(100, Y)
Next pos
End Sub
End Class
The output of that is
The maximum location of the picture of your mother will be at the max of 300 with random location between 0 to 300 just adjust anywhere you want.
Thats all :)

Related

Button collision event vb.net

I'm trying to make some buttons move around in a rapid manner. Each click of a specific button should "reward" the player with a certain quantity of points (both positive or negative). This is a similar idea to some of the aspects in some of the "idiot test games" you see online.
How can I perform collision checks with the buttons?
I know it's possible with picture boxes to perform event collisions, with the following code picObject1.bounds.intersectsWith(picObject2.bounds).
However, when I tried using that function for buttons, they didn't register as a collision. I do not know if that is because buttons don't have bounds (though that doesn't sound right) or due to some other hidden detail that I have missed.
Any pointers in the right direction would be extremely useful!
Handle the Click event for the buttons. The code might look something like this:
Public Class Form1
Private PlayerScore As Integer = 0
Private Buttons() As Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Buttons = Enumerable.Range(0,10).
Select(Function(i)
i = New Button()
AddHandler i.Click, AddressOf ScoreClick
Me.Controls.Add(i)
Return i
End Function)
End Sub
Dim rnd As New Random()
Private Sub ScoreClick(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = DirectCast(sender, Button)
PlayerScore += rnd.Next(-10, 10)
End Sub
End Class
Now just add a timer to to the form to move the buttons around.

How to make a mousemove event for multiple labels with one sub procedure?

I have the following code for a project I'm working in class and I need to copy it about 9 times in total. the only thing that will change each time is the category number and the label name by 1. Do I HAVE to create a mousemove event inidividually for each or is there a way to do it in one sub procedure? I don't care if it is harder, or less efficient I would just like to see if it is possible and how. Thank you.
Private Sub lbl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl1.MouseMove
category = 1
scoreRoll()
lbl1.Text = score
End Sub
It has been said this may be a duplicate. If someone could tell me how to do that same thing with mousemove instead of click that would be perfect. Thank you.
Multiple controls can share the same event. You just need to check the sender variable to see which one it was and process appropriately:
Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl1.MouseMove, lbl2.MouseMove, lbl3.MouseMove, lbl4.MouseMove, lbl5.MouseMove, lbl6.MouseMove, lbl7.MouseMove, lbl8.MouseMove, lbl9.MouseMove
Dim lbl As Label = CType(sender, Label)
category = CInt(lbl.Name.Replace("lbl", ""))
scoreRoll()
lbl.Text = score
End Sub

Visual Basic 2010 - Multiple Clicks on button cycles throught different background images

I am trying to make a button cycle through different local background images in Vb 2010
However I am a complete novice at VB and cant figure out how to do this. I want the program to change its background image (cycle through the images in the folder), each time I click the button.
It seems like it should be easy though for the life of me I can't figure it out.
The code below will change it 'once'
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
BackgroundImage = Image.FromFile("C:ProgramIcons\2.jpg")
End Sub
End Class
You could declare a global variable near the top of your form like
Dim imageCounter as Integer = 0
Then in your Button Click you increment the imageCounter and then convert it as String using ToString() method in order it could be concatenated with File Location and File Extensions which are both are strings.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
imageCounter += 1 ' Increment Image Counter variable
if (imageCounter> 5) Then ' greater than 5
imageCounter = 1 ' Reset counter to 1
End If
'Convert to string and then concatenate with other string
BackgroundImage = Image.FromFile("C:ProgramIcons\"+imageCounter.ToString()+".jpg")
End Sub
Now, this assumes that you have five (5) images thus the comparison if (imageCounter > 5)

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub

Text is selected in the text box

When I load the form where some text has been given to text box. All the text in that textbox is highlighted. I want vb not to load it this way.
How to fix it.
Thanks
Furqna
You could set the tab index on your textbox to something else so that it's not the lowest index.
You could set the TextBox1.SelectionLength = 0 in the form.activated event.
I don't like this as much because if the user had the text hilited and minized the application then they will lose the hilite, but is fairly easy to do. I guess you could use a flag to make sure it only did it on the first activate.
You could set a timer event in the load to clear it immediately after the load event, but that seems like overkill. I have worked at places where they had a standard function that happened on every form 100 ms after load because of problems such as this.
You could try this(it looks like a workaround):
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
It depends on the TabIndex of your TextBox, if it has the lowest TabIndex it gets focus and therefore it's Text is selected.
' VS.net 2013. Use the "Shown" event.
' GotFocus isn't soon enough.
Private Sub Form_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TB.SelectionLength = 0
End Sub
Type 1 Method
Dim speech = CreateObject("sapi.spvoice")
speech.speak(TextBox1.Text)
Type 2 Method
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
'Set the voice type male or female and etc
oVoice.Voice = oVoice.GetVoices.Item(0)
'Set the voice volume
oVoice.Volume = 100
'Set the text that will be read by computer
oVoice.Speak(TextBox1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
Type 3 Method
Imports System.Speech.Synthesis
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim spk As New SpeechSynthesizer
For Each voice As InstalledVoice In spk.GetInstalledVoices
ListBox1.Items.Add(voice.VoiceInfo.Name)
Next
ListBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim spk As New SpeechSynthesizer
spk.SelectVoice(ListBox1.SelectedItem.ToString)
spk.Speak(TextBox1.Text)
End Sub
End Class
This will also happen sometimes if The TextChanged or other similar Event is fired twice for the control.
When creating each form. Each object is indexed you can set the tab Index higher then the indexed object. Example: On the third form you put a text box in.
private void textBox1_TextChanged(object sender, EventArgs e)
This was the 12th object in the project, it would be indexed at 12. if you put the tab index higher then the indexed objects throughout the project. Tab index 1000 (problem solved.)
Have a great day.
Scooter