Public Class Form1
Dim i As Integer 'integer for spawning
Dim maxball As Integer = 50 'max ball able to be created
Dim RateX(maxball) As Integer 'rate of movement
Dim RateY(maxball) As Integer 'rate of movement
Dim ball(maxball) As PictureBox 'spawned ball is a picture box
Dim rnd As New Random 'random number generator
Dim rndLoc As Integer 'random locatino generator
Dim Loc As Point 'location is a point on the screen
Dim create As Integer 'integer to create new balls
Dim score As Integer = 0 'score is 0 but can increase
'move the ball
Private Sub moveball()
'For Each ball(ec) In ball
For i As Integer = 0 To create - 1
If ball(i).Left <= pbArena.Left Then 'bounce off left side
RateX(i) *= -1
End If
If ball(i).Right >= pbArena.Right Then 'bounce off right side
RateX(i) *= -1
End If
If ball(i).Top <= pbArena.Top Then 'bounce off top
RateY(i) *= -1
End If
If ball(i).Bottom >= pbArena.Bottom Then 'bounce off bottom
RateY(i) *= -1
End If
'====================================================================================================================================================
ball(i).Left += RateX(i) 'moves the ball horizontally
ball(i).Top += RateY(i) 'moves the ball vertically
'====================================================================================================================================================
Next
End Sub
'create the ball
Private Sub createball()
If create <= 50 Then '50 is max amount to
create += 1 'add 1 to create
ball(i) = New PictureBox 'ball is a picture box
ball(i).Size = New Size(45, 45) 'set size
ball(i).BackColor = Color.Red 'set color
'====================================================================================================================================================
ball(i).Top = rnd.Next(pbArena.Height - ball(i).Height) 'sets random y
ball(i).Left = rnd.Next(pbArena.Width - ball(i).Width) 'sets random x
'====================================================================================================================================================
RateX(i) = rnd.Next(-4, 4) 'random X direction/speed
RateY(i) = rnd.Next(-4, 4) 'random Y direction/speed
'====================================================================================================================================================
Me.Controls.Add(ball(i)) 'actually add teh ball
ball(i).BringToFront() 'bring to front so arena isn't in front
i += 1
End If
End Sub
'commands for when you touch black box
Private Sub pbTarget_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbTarget.MouseEnter
pbTarget.Top = rnd.Next(pbArena.Height - pbTarget.Height) 'sets random y
pbTarget.Left = rnd.Next(pbArena.Width - pbTarget.Width) 'sets random x
'====================================================================================================================================================
'scoring system
score = score + 1
lblScore.Text = score
createball() 'creates a new ball
End Sub
'what happens when the timer ticks
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
moveball() 'every timer tick the ball will move IF its created
ball(i) = New PictureBox 'ball is a picture box
End Sub
End Class
This is my code so far. Each time the mouse intersects with the target (which is a picture box) is moves. I am replicating this game. http://www.lewpen.com/game/ I have used an array to spawn red squares on the form. I want to be able to detect when my mouse enters them. I know how to do this with picture boxes, but these are all spawned objects called ball(i). Thanks for the help!
It sounds like you want to know how to add an event handler to a dynamically created control. these are all spawned objects called ball(i) but they are all just pictureboxes. You can add event handlers when you create the balls (pictureboxes)
Private Sub createball(BallIndex As Integer)
Dim ball As New PictureBox 'ball is a picture box
' give it a name so we can find it
' ball index is PASSED to avoid sloppy global vars
ball.Name = "Ball" & BallIndex.ToString
' etc
Me.Controls.Add(ball)
AddHandler ball.MouseEnter, AddressOf BallMouseEnter
Elsewhere, you'd add the code for the event:
Private Sub BallMouseEnter(sender As Object, e As EventArgs)
' your code here
End Sub
Since the balls exist as controls in the Controls collection there is really no reason to keep a reference to them in an array. If you name them "Ball1", "Ball2" you can make them move by referencing them by name:
Me.Controls(ballname)
Where BallName would be "Ball" & index.ToString and index would be the ball/picturebox to move (like the i variable). EDIT More info:
Private Sub moveballS()
Dim ball As PictureBox
' loop thru ballS
For n As Integer = 0 To BallCount
' current ball from name
ball = Me.Controls("Ball" & n.ToString)
' your code here
If ball.Left <= pbArena.Left Then
' etc
End If
' you CAN just reference them in controls(),
' but it is wordy:
If Controls("Ball" & n.ToString).Left <= pbArena.Left Then
' etc
End If
Next n
End Sub
Another way to track them is just a List(Of String) to store the name, but that is equally unneeded if you can get them by name from Controls() as above:
Dim ballList As New List(Of String)
' when you create a ball:
ballList.Add(ball.Name)
to get a control reference:
For n As Integer = 0 To ballList.Count - 1
ball = Me.Controls(ballList(n))
' etc
It can be a bad idea to create a separate reference to dynamically created controls (like an array) since that ref can prevent the object from being disposed of if/when you reset or start over and are deleting balls/pictureboxes.
Related
I was trying to remove this "brickn" when ball intersect with this brick
but i am facing problem that "brickn" is not declared any helps?
there is code
Dim brickWidth as Integer = 0
Public Function CreateBrick() As PictureBox
Dim Brickn As New PictureBox
Me.Controls.Add(Brickn)
Brickn.Size = Brick.Size
Brickn.Left = BrickWidth
Brickn.Top = 0
Brickn.Image = Brick.Image
Brickn.SizeMode = PictureBoxSizeMode.StretchImage
Brickn.BackColor = Color.Black
Return Brickn
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i= -75 To Me.Width
CreateBrick()
BrickWidth += 170 ' increasing brick.left on every new brick is created
i += 170 ' increasing looop count according to brick needed
Next
Private Sub Boll_control_Tick(sender As Object, e As EventArgs) Handles Boll_control.Tick
If Ball.Bounds.IntersectsWith(brickn.Bounds) Then
Me.Controls.Remove(brickn)
End If
End Sub
why this "brickn" is not saying not declared in "boll control tick " timer
You are instantiating brickn withing CreateBrick. You are returning it as the result of that function, but not assigning it to anything. So it's scope is limited to the CreateBrick fuction only, which is why it's not accessible from Boll_control_Tick.
Also you are then trying to create multiple instances of it with using a single object.
This code will allow you to create one or more. You will then need to rework your Boll_control_Tick to work out whether it intersects with any. You may want to create a list or array of PictureBox objects as Brickn instead of one.
Dim brickWidth as Integer = 0
Dim Brickn As PictureBox ' This may be better as a list or an array
Public Function CreateBrick() As PictureBox
Dim myBrickn As New PictureBox
'Note - no idea what Brick is here or where it comes from
Brickn.Size = Brick.Size
Brickn.Left = BrickWidth
Brickn.Top = 0
Brickn.Image = Brick.Image
Brickn.SizeMode = PictureBoxSizeMode.StretchImage
Brickn.BackColor = Color.Black
Return myBrickn
End Function
and then in your Form_Load method:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i= -75 To Me.Width
Brickn = CreateBrick() ' or add to your List/Array here
Me.Controls.Add(Brickn)
BrickWidth += 170 ' increasing brick.left on every new brick is created
i += 170 ' increasing looop count according to brick needed
Next
That will add multiple picture boxes to your controls and, if you want, create a list or array of them for easy access. You will need to loop through those in Boll_control_Tick to see if ball bounds intersects with any and then remove that specific one only
I am new to visual basics and was wondering how to do the following program: I have 9 picture boxes and a button "arrange". For my program, I would like that all picture boxes come together like a puzzle randomly to make a square that has a width and height of three picture boxes. The square made would have all nine picture boxes in one and every time you click the button "arrange" the picture boxes would change to a random location within the square. So far, I have written so that all the picture boxes become the same size but i don't know how to make them come together in a square. Thanks in advance.
Public Class frm1
Dim Placement As Integer
Private Sub btnArrange_Click(sender As Object, e As EventArgs) Handles btnArrange.Click
picDeux.Size = picgris.Size
picTrois.Size = picgris.Size
picQuatre.Size = picgris.Size
picCinq.Size = picgris.Size
picSix.Size = picgris.Size
picSept.Size = picgris.Size
picHuit.Size = picgris.Size
picNeuf.Size = picgris.Size
lstNum.Items.Clear()
For i = 1 To 3
For j = 1 To 3
Dim L As New Point(picgris.Width * j + 100, picgris.Height * i)
lstNum.Items.Add(L)
Next
Next
For i = 1 To 3
For j = 1 To 3
Placement = Int(Rnd() * (lstNum.Items.Count))
Next
Next
End Sub
End Class
I created nine pictures boxes at design time. You would assign a different image to each picture box. They are all square and the same size. Mine are 100 x 100 to make the arithmetic easy.
I made an array of points as a form level variable. These point will form a 300 x 300 square with the picture boxes. I also declared an array of PictureBox. In the Form.Load I added the pictures boxes to the array.
To reposition the picture boxes assigned the array to a list. Items in this list will be removed because we don't want to assign the same location to more the one picture box. This will not effect the original array.
Looping through the picture boxes we assign a random position to the box then remove that point from the list.
Public Class PictureSort
Private Rand As New Random()
Private PointArray As Point() = {New Point(100, 100), New Point(200, 100), New Point(300, 100), New Point(100, 200), New Point(200, 200), New Point(300, 200), New Point(100, 300), New Point(200, 300), New Point(300, 300)}
Private PictureBoxArray(8) As PictureBox
Private Sub PictureSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBoxArray = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9}
End Sub
Private Sub RepositionPictureBoxes()
Dim lst = PointArray.ToList
For Each pb In PictureBoxArray
Dim index = Rand.Next(0, lst.Count)
pb.Location = lst(index)
lst.RemoveAt(index)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RepositionPictureBoxes()
End Sub
End Class
My advice is to use a control array - you have an example here that should help: VB.NET Control Array- Declaration and Application.
You just need to initiate that array of controls once, this can be done at form load.
The next step is to sort that array in a random manner. Finally, loop on the array and every time your current loop index modulo 3 = 0, then you increase the Y coordinates and reset the X position.
Here is an example. You can see that each time you click on the button, the picture boxes are rearranged on the form in random order using an ad hoc function. For each picture box, a bitmap is generated on the fly to show the index of the control.. this is for demonstration purposes.
Public Class frmPics
Private pics As New List(Of PictureBox)
Private Const picture_width As Integer = 100, picture_height As Integer = 50
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' instantiate controls
Dim font As New Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel)
For i As Integer = 1 To 9
Dim pic As New PictureBox
pic.Visible = False
pic.Name = "pic" & i
pic.Text = i.ToString
Console.WriteLine("Create control: name: " & pic.Name)
' generate an ad-hoc bitmap image showing the index of the control
Dim bitmap As New Bitmap(picture_width, picture_height)
Using g As Graphics = Graphics.FromImage(bitmap)
Dim width As Integer = CInt(g.MeasureString(Text, font).Width)
Dim height As Integer = CInt(g.MeasureString(Text, font).Height)
End Using
Using g As Graphics = Graphics.FromImage(bitmap)
g.Clear(Color.Blue)
g.DrawString(i.ToString, font, New SolidBrush(Color.White), 0, 0)
End Using
pic.Image = bitmap
pics.Add(pic)
Me.Controls.Add(pic)
Next
End Sub
Private Sub btnShuffle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShuffle.Click
Dim x As Integer = 10, y As Integer = picture_height
Dim counter As Integer = 1
Dim rnd As New Random()
' show controls on form
Console.WriteLine("Show controls on form")
Me.SuspendLayout()
For Each item In pics.OrderBy(Function() rnd.Next)
item.Width = picture_width
item.Height = picture_height
item.Location = New Point(x, y)
item.BorderStyle = BorderStyle.FixedSingle
item.Visible = True
Console.WriteLine("counter: " & counter & " - control name" & item.Name & " - position: " & item.Location.X & "/" & item.Location.Y & " text: " & item.Text)
' reset X position every 3 iterations
If counter Mod 3 = 0 Then
x = 10
y += item.Height
Else
x += item.Width
End If
counter += 1
Next
Me.ResumeLayout()
End Sub
End Class
I can drag a PictureBox onto a Form Control, a Tab Control or a Panel Control, etc. And I can import an image into a PictureBox. However, I don't know how to group multiple PictureBoxes together in Visual Studio 2017. Looks like there is no such a function. I need this function because I want to generate a big picture based on the user's input. That big picture consists of multiple small pictures, the visibility of which is controlled by the user through multiple checkboxes.
In Excel, I could put multiple pictures in it, group them together, use the VBA to control the visibility of each picture, and finally copy that picture group into a Word file. I would do this in a VSTO Word Document project in Visual Studio 2017 using vb.net.
I added some pictures for demonstrate the expected function.
Picture 1 shows the small pictures to be used in a big picture. (Please ignore the .vslx file)
Picture 2 shows a possible result based on user's input.
You can make your own custom control. here is an example/suggestion how to do it with a User control that can be reused across your application. the user control is holding panels in a matrix, you can set a drag&drop Event to each Panel control and the user will be able to drop a picture box on each panel:
USER CONTROL:
Public Class UserControl1
Public NumberOfPanelsInRow As Integer
Sub New(ByVal height As Integer, width As Integer, Optional ByVal numberofPanelsInRow As Integer = 3)
' This call is required by the designer.'
InitializeComponent()
' Add any initialization after the InitializeComponent() call.'
Me.Height = height
Me.Width = width
Me.NumberOfPanelsInRow = numberofPanelsInRow
End Sub
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' grouped panels to hold picturebox you can drag & drop to them...'
Dim panelHeight As Integer = Me.Height / NumberOfPanelsInRow
Dim panelWidth As Integer = Me.Width / NumberOfPanelsInRow
Dim colors() As Color = {Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange,
Color.Red, Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange, Color.Red}
Dim total As Integer = NumberOfPanelsInRow * NumberOfPanelsInRow
Dim currentYlocation As Integer = 0
Dim currentXlocation As Integer = 0
Dim location As Point = New Point(0, currentYlocation)
Dim rowcounter As Integer = 0
Dim itemcounter As Integer = 0
For i = 1 To total
If rowcounter >= NumberOfPanelsInRow Then
rowcounter = 0
currentYlocation += panelHeight
currentXlocation = 0
End If
' to each one of this panel you can drag a picture box'
Dim p As New Panel
p.Size = New Size(panelWidth, panelHeight)
p.Location = New Point(currentXlocation, currentYlocation)
p.BackColor = colors(itemcounter)
Me.Controls.Add(p)
rowcounter += 1
itemcounter += 1
currentXlocation += panelWidth
Next
End Sub
End Class
CALLING THE USER CONTROL FROM FORM1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uc = New UserControl1(300, 300)
Me.Controls.Add(uc)
End Sub
End Class
GUI OUTPUT:
Hello i am trying to make a texas hold'em style game and im at the point where i filled an array with numbers 1-52 randomly assorted. I first pull the value from the first index of the array and have the corresponding card picture be set to a picturebox value. I have saved the 52 card .png files in my resources as well. These pictures are also saved with their names as 1.png, 2.png, 3.png.... etc depending the suit and value.
I am sorting the values as 1-13 spades (2-ace), 14-26 hearts, 27-39 diamonds, 40-52 clubs.
I also just saw i should probably use a global counter to keep track of the deck position.
Public Class Form1
Dim Deal As MsgBoxResult
Dim CardDeck As New Random
Dim Counter As Integer = 1
Dim CardCount(52) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Deal = MessageBox.Show("Would you like to start a Game?", "Texas Holde'em", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
ShuffleDeck()
CalculateFirst3Cards()
End Sub
Private Sub ShuffleDeck()
If Deal = MsgBoxResult.Yes Then
For num As Integer = 1 To CardCount.Length - 1
Dim DeckValue As Integer = CardDeck.Next(1, 52)
CardCount(Counter) = DeckValue
Counter += 1
Next
End If
End Sub
Private Sub CalculateFirst3Cards()
Dim counter As Integer = 1
For num As Integer = 1 To 3
Dim hold As Integer = CardCount(counter)
Dim hold1 As String = Convert.ToString(hold)
River1.Image = My.Resources.
counter += 1
Next
End Sub
End Class
I am doing a project using VB.net , where I generate an array of around 300 buttons (Number of buttons decided based on the available X&Y direction limits.) with the click of a button called START button. I defined a sub program to generate buttons using ADD Button and its successfully done.
Now I wanted to link all these buttons to same click handler automatically. I cant select the buttons and link them to a same click event because all these buttons are only generated at the time of code execution.
I am attaching my code below, It will be a great thing some one can suggest me a solution.
Public Class Form1
'Variable that holds the value of button size
Dim buttonwidth As Integer = 40
Dim buttonheight As Integer = 40
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Private Sub AddButton(ByVal xp As Integer, ByVal yp As Integer)
'This routine generate button automatically
'This routine need an inputs of its location
'defining b as a new BUTTON control "b = New Button"
Dim b As New Button
'defining location as a POINT variable, which have X & Y. Taking them from the argument
Dim location As New Point(xp, yp)
'Assigning that location for the button
b.Location = location
'Assiging the size of the button
b.Size = New Size(40, 40)
'Giving width value to button width variable
'buttonwidth = 40 'b.Size.Width
'Giving height value to buttonheight variable
'buttonheight = 40 ' b.Size.Height
'Assigning Random label for the button
Randomize() 'Initializing the random generator
Dim label As Integer = CInt(Int((9 * Rnd() + 0))) 'Create a random number for Label
b.Text = label 'Assign that number to label
'Assigning font for the button
b.Font = New Font(b.Font.FontFamily, 15)
'b.Handle = New (b.Handle, Button1_Click )
'Finally ADD the button to the form
Me.Controls.Add(b)
End Sub
Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
'This part of algorithm handles the generation and placement of buttons
'Here x & y are two indigers responsible for the x , y cordinates of buttons
'defining y value and it starts from cordinate 60. This can be changes to increase the spacing
'between top border of the form and buttons
'Here set the limits for the buttons
Dim x_min_limit, y_min_limit, x_max_limit, y_max_limit As Integer
x_min_limit = 60
x_max_limit = 1300
y_min_limit = 150
y_max_limit = 600
Dim y As Integer = y_min_limit 'assigning minimum limit value to y
'This loop is responsible for generating buttons in between the limits of x&y
'Here the for loop executes untill the x&y reaches up to its maximum limits
' here x is the integer looping between the limits with a step of button width (when ever button width changes , the step also will change)
'1.4 value deducted from step because we dont wanted to overlap 2 buttons in x axis
For x As Integer = x_min_limit To x_max_limit Step buttonwidth - 1.4
If x > x_max_limit - 50 Then 'This if loop makes sure that buttons are populated on y direction
x = x_min_limit 'If x direction population exceeds the xiven x limit, it increases the y value and restores x value to initial value
y = y + buttonheight - 1.4 'Y value increased with a step of button height and correction value is also given
If y > y_max_limit Then 'whenever the Y value reaches its maximum limit,
Exit For 'terminate the whole for loop
End If
End If
'Whenever the x and y coordinates get finalized ADD that button to form
'Call the function and pass the x&y value in to it
Call AddButton(x, y)
Next
Randomize() 'Initializing the random generator
Dim label As Integer = CInt(Int((9 * Rnd() + 0))) 'Create a random number for Label
Label1.Text = label
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttons_click
End Sub
End Class
I believe you are looking for the AddHandler method: http://msdn.microsoft.com/en-us/library/ms598898(v=vs.110).aspx
Yes, you have to create a Buttons_Click Sub without Handles keyword, just likes follow
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
And, you have to add event handler after creating the button objects,
'defining b as a new BUTTON control "b = New Button"
Dim b As New Button
AddHandler b.Click, AddressOf Buttons_Click
That's it.
For more references, as mention in the comments and other answers
Please go to
http://msdn.microsoft.com/en-us/library/7taxzxka.aspx
http://msdn.microsoft.com/en-us/library/ms598898%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1