How to use array when declaring sub VB - vb.net

I was wondering is there was a way to use an array when declaring a sub.
Example:
Public Class Form1
Private PictureBox(2) as PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox(0) = PictureBox1
PictureBox(1) = PictureBox2
PictureBox(2) = PictureBox3
Private Sub PictureBox_mouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox(1).MouseDown
'do stuff here
end sub

This is the method Plutonix suggested in the comments applied to your code. Note that no array is needed.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is PictureBox Then
AddHandler DirectCast(ctrl, PictureBox).MouseDown, AddressOf PictureBox_MouseDown
End If
Next
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
'Code here will apply to every PictureBox in your form
End Sub
End Class
Also note that instead of DirectCast(ctrl, PictureBox).MouseDown, you could just use ctrl.MouseDown. Using DirectCast simply helps you avoid making mistakes by cluing IntelliSense and the compiler in on what you're doing.

Related

Change Backcolor for all inherited forms

i have a baseform and a few forms inherited from that.
Call them BaseForm and Form1, Form2
I want to select a color in Form1 and these color is set in all inherited forms.
That works, if the form is open after the color is changed.
Let me explain.
I open Form1
select the color and set a global variable(glColorVariable).
The Backgroundcolor in the Baseform.Designer.vb is set
me.backcolor=glColorVariable
The color in Form1 didn't change
If i now open form2 the color in form2 IS changed.
How can i ... repaint my Baseform in Form1 so the color is changed.
Thank you verry much!
Cornelia
You can't loop forms so that you can change a parameter on them.
I think the Assembly.GetExecutingAssembly().GetTypes() is dead end for this.
One solution, if you fill an list element with your forms.
Dim Forms As New List(Of Form)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Forms.Add(My.Forms.Form1)
Forms.Add(My.Forms.Form2)
Forms.Add(My.Forms.Form3)
End Sub
Sub SetBackColors(ByVal c As Color)
For Each f In Forms
f.BackColor = c
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SetBackColors(Color.Red)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Show()
Form3.Show()
End Sub
or what Jimi wrote, use DataBindings:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.DataBindings.Add("BackColor", Me, "BackColor", False, DataSourceUpdateMode.OnPropertyChanged)
Form3.DataBindings.Add("BackColor", Me, "BackColor", False, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.BackColor = Color.Red
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Show()
Form3.Show()
End Sub

How to hold reference to different forms with one variable

I have a Form (named Form1) which has a fixed size and some controls.
I created another Form (named Form2), which is a copy of Form1 with only difference being a different fixed size.
I created a Form "SharedForm", which holds subs and functions used by both forms (so I don't have to write them for each of them).
My problem is: I don't know how to keep reference for either form (only one at a time, ever).
If I declare FormRef variable as Form, I get an error that "label1 is not a member of form".
(Otherwise if I declare as Form1 or Form2 it works fine, but of course only for one form)
SharedForm looks like the following:
Public Class SharedForm
Public Shared FormRef As Form 'problem is here
Private Sub SharedForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormRef = New Form2
FormRef.Show()
End Sub
Public Shared Sub Button1_Click(sender As Object, e As EventArgs)
FormRef.Label1.Text = "test"
End Sub
End Class
Form1 and Form2 are as so.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Just a class, not a Form.
Public Class EventCode
Public Shared Sub Button1Click(Sender As Form)
Dim frm As Form = Nothing
If Sender.Name = "Form1" Then
frm = DirectCast(Sender, Form1)
ElseIf Sender.Name = "Form2" Then
frm = DirectCast(Sender, Form2)
End If
frm.Controls("Label1").Text = "Hello World"
End Sub
End Class
In Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form1 shows "Hello World"
In Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form2 shows "Hello World"

How to make a self replicating program in vb.net?

I'm trying to make a little prank program where a form keeps opening until it hits a certain number like 50 or something, I've tried this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Show()
End Sub
End Class
But that doesn't work, anyone willing to help? Thanks
Ditto what litelite said. If you want to show a new instance of the form, Show() won't cut it.
In fact, once you close that form, the timer you're using will be lost. So you'll need to handle the Form.Closing event as well. Since we're just having fun here, I'd suggest that you make that work like cutting off the head of a Hydra, two more replace it.
Try something like this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim _newForm as Form2 = new Form2()
_newForm.Show()
End Sub
Private Sub Me_Closing(sender As Object, e As EventArgs) Handles Form2.Closing
Dim _newForm as Form2 = new Form2()
_newForm.Show()
Dim _newForm2 as Form2 = new Form2()
_newForm2.Show()
End Sub
End Class

Loading pictures with code

Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?
You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub

Check if mouse pointer enteres panel components

How do I check if my mouse cursor enters any component in a panel .
I stared to write this code to check when the mouse enters any check box in my panel then I realised that I had no idea how to actually check if the mouse enters the components I the panel .
Private Sub GenCheck()
For Each CheckBox In datapanel1.Controls
Next
End Sub
how do i go about doing this ?
Edit
I have an Idea but i'm not too sure about it
I could say
Private Sub GenCheck()
Dim cb As CheckBox
For Each cb In datapanel1.Controls
AddHandler cb.MouseEnter, AddressOf cb_MouseEnter
AddHandler cb.MouseLeave, AddressOf cb_MouseLeave
Next
End Sub
Private Sub cb_MouseEnter(sender As Object, e As EventArgs)
End Sub
Private Sub cb_MouseLeave(sender As Object, e As EventArgs)
End Sub
You can use MouseHover to determine when the cursor hovers above the checkbox like this.
Private Sub CheckBox1_MouseHover(sender As Object, e As System.EventArgs) Handles CheckBox1.MouseHover
MsgBox("Mouse over!")
End Sub`
Edit:
I have put a panel on a form with two checkboxes to mimic your requirements, here is what you're looking for:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each pnlCheckBox As CheckBox In Panel1.Controls
AddHandler pnlCheckBox.MouseHover, AddressOf Me.CheckBoxMouseOver
Next
End Sub
Private Sub CheckBoxMouseOver()
MsgBox("Mouse over!")
End Sub
*PERFECT WAY TO ACCOMPLISH THIS *
Dim con As Control
For Each con In datapanel1.Controls
AddHandler con.MouseEnter, AddressOf con_MouseEnter
AddHandler con.MouseLeave, AddressOf con_MouseLeave
Next
End Sub
Private Sub con_MouseEnter(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Private Sub con_MouseLeave(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub