I've updated the code and now it says that index was outside of the bounds of the array and also I'm in cs1 in high school, and accidentally made this account using my school google account.
I have included the code specific to moving the bricks down (by just a little), the code for the array, and the code for the entire timer block which as you can see is very organized with regions
Dim newbrickx, newbricky As Integer
newbrickx = bricks(i).Location.X
newbricky = bricks(i).Location.Y + 10
'left wall
If (lblball.Location.X >= Me.ClientSize.Width - lblball.Width) Then
bricks(i).Location = New Point(newbrickx, newbricky)
End If
'right wall
If (lblball.Location.X = 0) Then
bricks(i).Location = New Point(newbrickx, newbricky)
End If
the array (in the form load event)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bricks = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8}
End Sub
I don't have the reputation to write comments yet, so I do it this way.
I assume bricks is an array containing your pictureboxes. So, inside the conditional block you can assign the new position like this:
bricks(i).Location = New Point(newbrickx, newbricky)
And as commented before you should probably also check for the y coordinates to make sure it is not out of bounds.
Related
The Volume property from MediaPlayer.MediaPlayer is really confusing me. I googled the definition and found this which I think is the right class. It says the Volume property is a Double that excepts any number between 0 and 1, and it's default is 0.5. However, when I set it to 0.5 or any other number within 0 and 1, it says it does not fall within the expected range.
Here is how it is defined and how I tried to adjust the volume. I have the event as MouseUp because I could not get ValueChanged or Scroll to work properly. Any tips on that would be greatly appreciated.
Dim Player1 As MediaPlayer.MediaPlayer = New MediaPlayer.MediaPlayer
Private Sub SndMasterSlider_MouseUp(sender As Object, e As EventArgs) Handles SndMasterSlider.MouseUp
Player1.Volume = SndMasterSlider.Value / 100
Debug.Print(Player1.Volume)
End Sub
I have been working on an app lately that displays three random photos. The form consists of three pictureboxes and a button. When the user clicks a button, three different images are shown. The problem is, however, these three images are not always unique, most of the time there will be doubles and often triples too. I tried to implement a function to catch this but all it succeeded at was lowering the chances of identical images. There are over 50 images to choose from, so it's not like there isn't enough. Here is the code for the failed solution I came up with:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RandomImageOne()
RandomImageTwo()
RandomImageThree()
If imagenumber1.Text or imagenumber2.Text = imagenumber3.Text Then
RandomImageThree()
End If
If imagenumber1.Text or imagenumber3.Text = imagenumber2.Text Then
RandomImageTwo()
End If
If imagenumber3.Text or imagenumber2.Text = imagenumber1.Text Then
RandomImageOne()
End If
End Sub
The 'RandomImage' functions generate a random number in a label (eg. imagenumber1), these numbers correlate with the number of one of the 50 images. I realize that this is probably not the smartest method, but I am not familiar with any other way.
I need to be able to either generate three unique numbers, so that I don't have to worry about programming in something to stop double and triple images, or create a solution that works 100% of the time to catch the double or triple images.
Any help would be very much appreciated, especially if it were explained simply. Thank you.
I would generate random image 1 and image 2, testing that image 2 is distinct using a while loop. Only once that that is done would I then move on to generating image three.
Information on the while loop is here.
So in rough code (it's been a while since I used VBA properly):
RandomImageOne()
RandomImageTwo()
do while imagenumber1.text = imagenumber2.text
RandomImageTwo()
loop
RandomImageThree()
do while imagenumber3.text = imagenumber2.text or imagenumber3.text = imagenumber1.text
RandomImageThree()
loop
It might not be the most efficient way but it works...
First create a function that returns a list of three items:
Public Function ProvideUniqueNumbers(NoList As List(Of Integer), _
HowManyToReturn As Integer) As List(Of Integer)
Dim Generator As System.Random = New System.Random()
Dim n As Integer = NoList.Count
Dim index As Integer = Generator.Next(1, n)
Dim ReturnList As List(Of Integer) = New List(Of Integer)
For i = 1 To HowManyToReturn
n = NoList.Count
index = Generator.Next(1, n)
ReturnList.Add(NoList(index))
NoList.RemoveAt(index)
'NoList.Dump()
Next
Return ReturnList
End Function
Then create a list of integers for your collection. For instance:
List(Of Integer) MyList = New List(Of Integer)
For i As Integer = 0 To YourImageArray.Count - 1
MyList.Add(i)
Next
Finally Call the function and distribute the results:
Dim result As List(Of Integer) = ProvideUniqueNumbers(MyList,3)
image1 = YourImageArray(result(0))
image2 = YourImageArray(result(1))
image3 = YourImageArray(result(2))
This isn't a solution but a note.
This doesn't do what you think it does
If imagenumber1.Text or imagenumber2.Text = imagenumber3.Text Then
RandomImageThree()
End If
You have to compare each element
If imagenumber1.Text = imagenumber3.Text or imagenumber2.Text = imagenumber3.Text Then
RandomImageThree()
End If
I have several label boxes on my design form that all share the naming convention lbl_#.text where # ranges from 1 to 60. I want to make a loop that iterates through each lbl_#.text adding some incremental value, let's say multiples of 2 for this question's theoretical purpose.
Something such that the end result would amount to the following:
lbl_1.text = "2"
lbl_2.text = "4"
lbl_3.text = "6"
...
lbl_60.text = "120"
I'm not sure how to access each of these labels through the coding side, I only know how to explicitly mention each label and assign a value :/
There are a few options here.
In this situation the labels will often have a common container, such as panel or groupbox control. In that case:
Dim formLabels = myContainerControl.Controls.OfType(Of Label)()
For Each formLabel As Label In formLabels
'...
Next formLabel
Of course, this mixes logical groups with visual groupings. Those two things don't always align well, so you can also...
Add them all to a Label array (or List(Of Label) or any other enumerable):
Dim formLabels(60) As Label = {lbl_1, lbl_2, lbl_3 .... }
For Each formLabel As Label in formLabels
'...
Next formLabel
But sometimes that's more trouble than it's worth, even if you use a loop to create the collection, and so you can also
Use the .Name property (in conjunction with a naming convention to identify your desired controls):
Dim formLabels = Controls.Where(Function(c) c.Name.StartsWith("lbl_"))
For Each formLabel As Label In formLabels
'...
Next formLabel
Some combination of the above (for example, code in the form load event to create a list based on the name property).
Notice the actual For Each loop is exactly the same in all of those options. No matter what you do, get to the point where you can write a single expression to identify the label control, and then run a simple loop over the expression result.
This points to a final strategy: think in terms of binding to a data source. With a data source, your labels are created as part of a DataGridView, FlowLayoutPanel, or similar control. Then you can iterate the rows in the grid or panel.
Use the Controls collection:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 3
Dim myLabel As Label = CType(Me.Controls("lbl_" & i), Label)
myLabel.Text = ...whatever value you want to put here
Next
End Sub
End Class
If you don't know how many labels there are, one option is to use a Do Loop.
Dim lblTarget As Label = Nothing
Dim intCursor As Integer = 1
Dim bolFirstIteration As Boolean = True
Do Until lblTarget Is Nothing AndAlso Not bolFirstIteration
If bolFirstIteration Then
bolFirstIteration = False
End If
lblTarget = CType(Me.Controls("lbl_" & intCursor.ToString()), Label)
If Not lblTarget Is Nothing Then
lblTarget.Text = (intCursor * 2).ToString()
End If
intCursor += 1
Loop
Below is some code that I'm using to create objects with Visual Basic:
For indexCounter As Integer = 1 To TotalParticipants Step 1
participantClock = New Label
participantClock.Size = New Size(100, 20)
participantClock.Name = "participantClock" & indexCounter
participantClock.Location = New Point(139, (5 + ((indexCounter - 1) * 26)))
participantClock.BorderStyle = BorderStyle.Fixed3D
participantClock.TextAlign = ContentAlignment.MiddleRight
CenterPanel.Controls.Add(participantClock)
participantStop = New Button
participantStop.Size = New Size(58, 20)
participantStop.Location = New Point(245, (5 + ((indexCounter - 1) * 26)))
participantStop.BackColor = Color.Red
participantStop.ForeColor = Color.White
participantStop.Font = New Font(participantStop.Font, FontStyle.Bold)
participantStop.Text = "Stop"
CenterPanel.Controls.Add(participantStop)
participantTimer = New Timer
participantTimer.Start()
participantTimer.Enabled = True
participantTimer.Interval = 1
participantStopwatch = New Stopwatch
participantStopwatch.Start()
Next
I'm creating a label, a button, Timer, and Stopwatch. (Though I have sinking feeling I don't need BOTH a timer and stopwatch since I'm counting time.)
What I would like to do, is create the label and set that label's text to be the value from the stopwatch. The button that will be created will stop THAT stopwatch.
The problem that I'm having is that I cannot call the stopwatch by name since it wasn't created yet and VB throws a hissy fit at me for it. (After all it wasn't really declared.)
So the question becomes, how do you call the most recently dynamically created control and assign events using that control. If it's not possible to do, I do not mind dumping the form and starting over creating 30 stopwatches instead (but I'd like to avoid that, if possible).
Thanks for any help.
I assume that you want the timer to update the label based on the value of the stopwatch. Is that right?
One thing that you might try that is a little hacky is this:
Define a storage class like so:
Public Class StopwatchStorage
Public Property Stopwatch as Stopwatch
Public Property Label as Label
Public Property Timer as Timer
End Class
at the top of your form define a private list:
Private _storage as new List(Of StopwatchStorage)
at the end of your for loop do this
Dim storage As New StopwatchStorage()
storage.Label = participantClock
storage.Timer = participantTimer
storage.Stopwatch = participantStopwatch
_storage.Add(storage)
AddHandler participantTimer.Tick, AddressOf Timer_Tick
The above code would give you access to the three objects that you need in your tick function. You will have to loop through the _storage list to find the right "set" of objects but it should work:
Private Sub Timer_Tick(sender As Object, args As EventArgs)
For Each storage As StopwatchStorage In _storage
If storage.Timer Is sender Then
storage.Label.Text = storage.Stopwatch.Elapsed
Exit Sub
End If
Next
End Sub
I didn't try to compile that code so I'm sure there are a few typos but I think that should give you an idea of how to refer to the object without needing to use the object's name.
This is a VB .NET application where we are showing the output of a SQL statement in a Datagrid view. I'm using .NET 2005.
We need to get the separators of the headers on the grid control to be the same colors as the GridColor on the form.
We've tried looking through all of the properties of the DataGridView control, and found some interesting things that looked promising such as the DataGridViewAdvancedHeaderStyle, and DataGridViewHeaderBorderStyle, but none of it seems to allow you to change the colors on it.
Does anyone know how to do this without remaking the entire thing with a GDI+ control?
Well, I never did find a property for this, so I ended up creating a custom component, and overloading the OnPaint event handler to draw a line over the existing one.
Here is the code for it if anyone else ever comes across this post looking for a solution:
Private Sub CustomDataGridView_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Me.GridColor)
Dim TWidth As Integer = 2
Dim HeaderWidth As Integer = 0
If Me.RowHeadersVisible Then
HeaderWidth = Me.RowHeadersWidth
End If
For Each column As DataGridViewColumn In Me.Columns
Dim x As Integer = HeaderWidth + TWidth - 1
TWidth += column.Width
Dim top As Integer = column.HeaderCell.ContentBounds.Top
Dim bottom As Integer = column.HeaderCell.ContentBounds.Bottom + 1
pen.Width = 2
g.DrawLine(pen, x, top, x, bottom)
Next column
End Sub
To change the backcolor of the Column Headers in a datagridview, choose False for EnableHeadersVisualStyles. Then open ColumnHeadersDefaultCellStyle and choose the background color.
I can't see the picture but what about playing with these?
DataGridView.ColumnBordersHeaderStyle
DataGridView.RowBordersHeaderStyle