Take Text from Textbox and used it in a C - vb.net

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.

Related

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

Formatting and Computing Numbers in Textbox VB.Net

Hello Everyone Good Afternoon.
I Hope Someone Helps me with my Problem.
I have 3 Textboxes and they are:
GrandTotal.Text
VatAmount.Text
TotalAmount.Text
and 1 NumericUpdown1.Value
Here is the Scenario, As the system goes, there is a code that will trigger and Will put a Number value in GrandTotal.Text and after that, The User will press NumericUpdown1.Value. Every time the user press it A computation will be triggered and a Number value will be Displayed in TotalAmount.Text and VatAmount.Text
To Make it more specific it is like a computation form that will include VAT. For Example.
Grandtotal.text = 2000
and if I press the NumericUpDown to + 1
VatAmount.Text = 20 and TotalAmount.Text = 2020
I hope you get what I mean
and Here is my code for It:
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
VatAmount.Text = Val(Grandtotal.text) * NumericUpDown1.Value * 0.01
End Sub
Private Sub VatAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VatAmount.TextChanged
TotalAmount.Text = Val(VatAmount.Text) + Val(TextBox14.Text)
End Sub
Now I`m done Explaining it here is My Question.
How to put Commas on that Textboxes and Compute It? My Prof. asked that He wants to put commas on the numbers that will bi inputted? No need to literally put Commas. Put it Automatically when value is greater that 3 Digits
How can I put commas in Textboxes and Compute it using the NumericUpdown?
TYSM
This is roughly what you need:
Private Sub GrandTotal_TextChanged(sender As Object, e As EventArgs) Handles GrandTotal.TextChanged
Dim input As Double = Double.Parse(GrandTotal.Text)
Dim inputStringWithCommas As String = input.ToString("N0", CultureInfo.InvariantCulture)
GrandTotal.Text = inputStringWithCommas
Dim vat As Double = Double.Parse(GrandTotal.Text) * NumericUpDown1.Value * 0.01
VatAmount.Text = vat.ToString()
TotalAmount.Text = (Double.Parse(GrandTotal.Text) + vat).ToString("N0", CultureInfo.InvariantCulture)
End Sub
It is similar to what you have, so you should be able to make it work for the NumericUpDown event as well.

vb.net matching textbox.text with contents of listbox data

What I want to do is type a number in textbox1 and then match the word that corresponds to it for example
if textbox1.text = 6
then textbox2.text = searchable
but this list could be 10000 items long, so don't want to hard code it.
listbox contains the following data.I am open to changing the layout slightly if needed.
1 example
2 word
3 to
4 find
5 by
6 searchable
7 numbers
then upon button2 click my textbox2 would contain searchable (but not the number)
Thanks
Add this code in your button's click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) <= ListBox1.Items.Count - 1 Then '<--- check the textbox contains a number less than the item count of the list box
TextBox2.Text = ListBox1.Items(TextBox1.Text) '<---- replace the number with carasponding item in the listbox
End If
End Sub
Maybe use Listbox.Findstring(textbox1.text.trim & " ") or Listbox.FindString(Val(TextBox1.Text))
These return the index to the item you are looking for...

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)

[VB.NET]How to add a string item, returned from another form, to a list box at a specific spot in place of another?

The idea is that there's a list-box with items, and say you want to modify an item in the middle of the list. You select that item and click "Modify" button and a new form appears with the previously selected item data from first form ready to be modified in a text-box. After modifying and clicking Ok the second form suppose to return that modified string to the first form and insert the modified string into the same spot instead of the originally selected item, so it looks like it was edited to the user.
Edit:
Translated the pseudo code to actual VB.NET code to refresh my own memory :D
string = InputBox("Enter text")
// Do whatever you want with the string
x = listBox.SelectedIndex
listBox.Items(x) = string
You can try Content in place of Text too.
Make sure that the form that pops up is Modal. Here is a simple example of what you can do. (This assumes your listbox items are strings and is an example for editing up to three listbox items only. If the list is going to be much larger you will want to pursue a different architecture.)
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
TextBox1.Text = Form1.ListBox1.Items(i)
Case 1
TextBox2.Text = Form1.ListBox1.Items(i)
Case 3
TextBox3.Text = Form1.ListBox1.Items(i)
End Select
intTextboxCounter += 1
Next
End Sub
When this loads it will spin through the selected list items and put its value in a textbox. To update the values...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
Form1.ListBox1.Items(i) = TextBox1.Text
Case 1
Form1.ListBox1.Items(i) = TextBox2.Text
Case 2
Form1.ListBox1.Items(i) = TextBox3.Text
End Select
Next
End Sub