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

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)

Related

Hi, I'm working on a Electronic Raffle Draw project

I'm working on a ELECTRONIC RAFFLE DRAW project using VISUAL STUDIO 2010. I have a list of name in my sql database, and displayed it to a listview, along with certain conditions on what names should be displayed. Now, what I want to do is randomly select names from this list view. And I'm trying to convert this items from listview and convert it to a string array. I also have timer where when it's enable=true it should display these string array to a label.
And when ticker is enable=false. It should stop and display one specific name. All my codes are working except I get an InvalidCastException was unhandled in this specific line of code
ReDim strng(Name_ListView.Items.Count - 1)
Name_ListView.Items.CopyTo(strng, 0)
Dim strng() As String
Private Sub Frm_Raffle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ReDim strng(Name_ListView.Items.Count - 1)
Name_ListView.Items.CopyTo(strng, 0)
End Sub
Public Sub myTimer_Tick(sender As System.Object, e As System.EventArgs) Handles myTimer.Tick
Dim index As Integer = rnd.Next(Name_ListView.Items.Count)
mylblResult.Text = strng(index)
End Sub

How to randomize a picture box within a certain area

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 :)

Take Text from Textbox and used it in a C

Visual Basics 2010:
Two numbers are generated automatically. For example if the Random numbers generated are 2 and 3, you should press the button in 2nd row and 3rd column. I have created the random number generator:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 5
TextBox1.Text = (Convert.ToString(random.Next(1, 5)))
TextBox2.Text = (Convert.ToString(random.Next(1, 5)))
Next
End Sub
But I can't use it to press the correct button. If the correct button is pressed the color changes. If not there are no changes. How can I do this. Please help me...
Note:
It is a board game. The game is played on a 4 by 4 grid To play the game, a player generates two random numbers and colors in an area on the grid indicated by the numbers. For example if the generated number is a 2 and a 3 the player clicks in the 2x3 square which results in the color of the square being changed. A player should only be able to click and change the color of the square represented by the random numbers only.
You should create a class-level variable for both the row and column chosen:
Private randomRow as Integer
Private randomCol as Integer
Then in your code, assign the values:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
randomCol = random.Next(1, 5)
randomRow = random.Next(1, 5)
TextBox1.Text = randomCol.ToString()
TextBox2.Text = randomRow.ToString()
End Sub
Now in your code the sees the user click a box, check to see if the box column and row are the same as the randomCol and randomRow variables.

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