Simplifying multiple similar 'private subs' in VB - vba

I'm writing a VB program that has multiple positions where when the user hovers over an item, the same image appears.
Currently I have:
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
PictureBox1.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
PictureBox2.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox3_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox3.MouseMove
PictureBox3.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox4_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox4.MouseMove
PictureBox4.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox5_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox5.MouseMove
PictureBox5.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox6_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox6.MouseMove
PictureBox6.Image = Image.FromFile(picturePath)
End Sub
Private Sub PictureBox7_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox7.MouseMove
PictureBox7.Image = Image.FromFile(picturePath)
End Sub
This works fine, however, I am looking to condense my code. Surely there is a way of making this into just 1 or 2 private subs.
Note that this is just a snippet of my code.
The names of the objects are easily named PictureBox1, PictureBox2 etc.
Regards
Hugo.

Private Sub PictureBox_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove, PictureBox4.MouseMove, PictureBox5.MouseMove, PictureBox6.MouseMove, PictureBox7.MouseMove
DirectCast(sender, PictureBox).Image = Image.FromFile(picturePath)
End Sub

Related

How can I use the VB.NET Key press

I created a mouse position program that can be used to save your mouse position {X, Y}
I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position
The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn.
Can anyone help? I would be very grateful
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
TimeCo.Start()
End Sub
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
LabelX.Text = "X"
LabelY.Text = "Y"
X2.Text = "X2"
Y2.Text = "Y2"
End Sub
Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
LabelX.Text = Cursor.Position.X
LabelY.Text = Cursor.Position.Y
End Sub
Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
X2.Text = Cursor.Position.X
Y2.Text = Cursor.Position.Y
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
End Sub
End Class
If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.
If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "5" Then
Console.WriteLine("5")
End If
End Sub

hello! there is someone who can help me on my project? this is my code

Public Class frmColor
Dim red, green, yellow, blue, orange As New frmChanger
Private Sub BtnRed_Click(sender As Object, e As EventArgs) Handles BtnRed.Click
frmChanger.Show(red)
End Sub
Private Sub BtnGreen_Click(sender As Object, e As EventArgs) Handles BtnGreen.Click
frmChanger.Show(green)
End Sub
Private Sub BtnYellow_Click(sender As Object, e As EventArgs) Handles BtnYellow.Click
frmChanger.Show(yellow)
End Sub
Private Sub BtnBlue_Click(sender As Object, e As EventArgs) Handles BtnBlue.Click
frmChanger.Show(blue)
End Sub
Private Sub BtnOrange_Click(sender As Object, e As EventArgs) Handles BtnOrange.Click
frmChanger.Show(orange)
End Sub
End Class
that is my code on form1....
Public Class frmChanger
Private Sub PnlRed_Paint(sender As Object, e As PaintEventArgs) Handles PnlRed.Paint
PnlRed.BackColor = Color.Red
End Sub
Private Sub PnlGreen_Paint(sender As Object, e As PaintEventArgs) Handles PnlGreen.Paint
PnlGreen.BackColor = Color.Green
End Sub
Private Sub PnlYellow_Paint(sender As Object, e As PaintEventArgs) Handles PnlYellow.Paint
PnlYellow.BackColor = Color.Yellow
End Sub
Private Sub PnlBlue_Paint(sender As Object, e As PaintEventArgs) Handles PnlBlue.Paint
PnlBlue.BackColor = Color.Blue
End Sub
Private Sub PnlOrange_Paint(sender As Object, e As PaintEventArgs) Handles PnlOrange.Paint
PnlOrange.BackColor = Color.Orange
End Sub
End Class
and this is my code on form2....
what i need is, when i clicked the btnRed on form1, on form2 only the PnlRed show..
when the BtnGreen is clicked, Pnlgreen on form2..
my problem is both panel colors show when i click only one button.. what should i do? can someone help me please.
Add a Form level variable to frmChanger with its datatype as Color and its scope as Friend (so it can be seen from frmColor). This code is using the default instance of the form. You don't really need a separate form for each color. You only want to have a different color shown. How about a single panel that will have different colors instead of multiple panels.
In the Form1 code (your frmColor)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.panelColor = Color.Red
Form2.Show()
End Sub
Repeat for the other buttons with = Color.Green etc.
In Form2 (your frmChanger)
Friend panelColor As Color
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.BackColor = panelColor
End Sub

vb.net button visibility based on checkbox

So i have a form as below:
Public Class IPADSOFT
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
IPADSOFTTS.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
Me.Hide()
End Sub
End Class
which has 3 checkboxes labeled IPADSOFTBOX1, IPADSOFTBOX2, IPADSOFTBOX3
So... i have another form as follows:
Public Class IPADSOFTTS
Private Sub onload()
If IPADSOFT.IPADSOFTBOX1.Checked Then
Button1.Visible = True
Button3.Visible = True
Button5.Visible = True
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
IPADSOFT.Hide()
Me.Hide()
End Sub
End Class
Now the idea is that all the buttons on that second form are set to visible-false and i want the page to check which checkboxes are checked on the last form and then make the required buttons on this form visible... but it isnt working
What am i doing wrong?? i apologise im very very new to vb.net
Open the second form with this
Dim newForm As New IPADSOFTTS With
{.MainForm = Me}
newForm .Show()
Set Public MainForm As IPADSOFT below the Public Class of second form
Then use in the Load event
if MainForm.IPADSOFTBOX1.Checked = true then
'Do whatever
End if

WebBrowser1.Navigate loop request

How can i make this code to loop urls continuously in Webbrowser1, after run trough these links one time.
This is the code i have so far, but when i run this, the Webbrowser stop with last url, wich is google.com. What i want to do is when the last url is reached i want it to start from top again, and run trough links again and again, until i exit the program.
Public Class Form1
Private WithEvents backgroundWorker1 As System.ComponentModel.BackgroundWorker
Dim l As New List(Of String)
Dim flagDC As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Hide()
Button3.Show()
l = New List(Of String)
l.Add("http://bbc.com/sipeu/php/zq.php")
l.Add("http://google.com")
l.Add("http://DCWC.com/sipeu/us.php")
l.Add("http://google.com")
l.Add("http://example.com/sipeu/v.php")
l.Add("http://google.com")
backgroundWorker1 = New System.ComponentModel.BackgroundWorker
backgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles backgroundWorker1.DoWork
For Each www In l
flagDC = False
WebBrowser1.Navigate(www)
Do
Loop While Not flagDC
Next
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
System.Threading.Thread.Sleep(1000)
flagDC = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button3.Hide()
ChangeUserAgent("troll001_v2-agent34")
WebBrowser1.Navigate("http://example.com/welcome.php")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub

why wont this visual basic script work?

i am getting really annoyed by this piece of code for visual basic
please can you help. i have looked on YouTube and everywhere else even the vb website!!! i would really appreciate it if someone could help me out
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Hi!!")
Timer1.Start()
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = 100 Then
Timer1.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
End Sub
Private Function GetNumericUpDown1(v As Integer) As Boolean
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ( : ( ) Then
MessageBox.Show("Well Done!")
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MessageBox.Show("!!Stopped!!")
Timer2.Stop()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ProgressBar2.Increment(0.6)
If ProgressBar2.Value = 100 Then
Timer2.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("!!Started!!")
Timer2.Start()
End Sub
Private Sub SplitContainer1_Panel1_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer1.Panel1.Paint
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs)
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
ProgressBar3.Increment(1)
If ProgressBar3.Value = 100 Then
Timer3.Stop()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer3.Start()
End Sub
Private Sub ProgressBar3_Click(sender As Object, e As EventArgs) Handles ProgressBar3.Click
End Sub
End Class
BTW:this is my first post so it is probably rubbish!!
I don't have enough points to comment.
First off, as others have said we can't recreate this since we have no idea what your Form looks like or what you are expecting it to do.
Secondly, you should turn on Option Strict in the Compile section of Project properties to avoid technical errors like ProgressBar2.Increment(0.6) since 0.6 isn't a valid integer.
I threw together a TabControl (which you never mentioned in the OP) with 3 tabs and the various buttons and progress bars you list in what seemed like a logical fashion to me and it ran just fine for what code you have. Clicked the button on each tab and each progress bar eventually got to 100%. I have no idea what else you were expecting.
If you remove all the empty subs and action listeners, it would be easier to detect the problem