How to make a clean and short code in vb.net - vb.net

I'm pretty new here on Stack Overflow and this is my first time asking a question so please be kind to me if you find this question stupid or anything.
Does anyone know how can I make these codes short, I mean I would like to put all these codes into one line.. Please see the code below.
Private Sub PB_SearchP_MouseHover(sender As Object, e As EventArgs) Handles PB_SearchP.MouseHover
PB_SearchP.Image = My.Resources.search1
End Sub
Private Sub PB_SearchP_MouseLeave(sender As Object, e As EventArgs) Handles PB_SearchP.MouseLeave
PB_SearchP.Image = My.Resources.search
End Sub
Private Sub PB_AddP_MouseHover(sender As Object, e As EventArgs) Handles PB_AddP.MouseHover
PB_AddP.Image = My.Resources.add_1_iconhover
End Sub
Private Sub PB_AddP_MouseLeave(sender As Object, e As EventArgs) Handles PB_AddP.MouseLeave
PB_AddP.Image = My.Resources.add_1_icon
End Sub
Private Sub PB_New_MouseHover(sender As Object, e As EventArgs) Handles PB_New.MouseHover
PB_New.Image = My.Resources.newhover
End Sub
Private Sub PB_New_MouseLeave(sender As Object, e As EventArgs) Handles PB_New.MouseLeave
PB_New.Image = My.Resources.neww
End Sub
Private Sub PB_Save_MouseHover(sender As Object, e As EventArgs) Handles Btn_Save.MouseHover
Btn_Save.Image = My.Resources.savehover
End Sub
Private Sub PB_Save_MouseLeave(sender As Object, e As EventArgs) Handles Btn_Save.MouseLeave
Btn_Save.Image = My.Resources.save
End Sub
Private Sub PB_Update_MouseHover(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseHover
BTN_QUpdate.Image = My.Resources.edithover
End Sub
Private Sub PB_Update_MouseLeave(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseLeave
BTN_QUpdate.Image = My.Resources.edit
End Sub

A simpler solution is to use a function that sets-up lamba functions as event-handlers:
Public Shared Sub SetUpButton(btn As Button, normalImage As Image, hoverImage as Image)
AddHandler btn.MouseLeave, Sub(o, e) btn.Image = normalImage
AddHandler btn.MouseHover, Sub(o, e) btn.Image = hoverImage
End Sub
Called like so:
SetUpButton( Btn_Save, My.Resources.save, My.Resources.savehover )
SetUpButton( Btn_Update, My.Resources.update, My.Resources.updatehover )
SetUpButton( Btn_New, My.Resources.new, My.Resources.newhover )
and so on...
In C#, for a syntax comparison:
public static void SetUpButton(Button btn, Image normalImage, Image hoverImage) {
btn.MouseLeave += (o,e) => btn.Image = normalImage;
btn.MouseHover += (o,e) => btn.Image = hoverImage;
}

Related

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

Simplifying multiple similar 'private subs' in VB

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

Enable a counter at a specific time everyday

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

Covering Panel with another

Private Sub frmitem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pnldeposit.Visible = False
pnlwithdraw.Visible = False
End Sub
Private Sub cmdideposit_Click(sender As Object, e As EventArgs) Handles cmdideposit.Click
pnldeposit.Visible = True
pnlwithdraw.Visible = False
End Sub
Private Sub cmdiwithdraw_Click(sender As Object, e As EventArgs) Handles cmdiwithdraw.Click
pnlwithdraw.Visible = True
pnldeposit.Visible = False
End Sub
//i am having problem with this form. i want to show the first panel which is successful, but the problem is showing the second panel. it is not working, im using buttons btw. help me. Thank you in advance :D
Here is a quick example using BringToFront. With both panels having the same Location and being the same Size.
Public Class Form1
Private switchPanels As Boolean
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
switchPanels = Not switchPanels
If switchPanels Then
Panel1.BringToFront()
Else
Panel2.BringToFront()
End If
End Sub
End Class
Private Sub cmdideposit_Click(sender As Object, e As EventArgs) Handles cmdideposit.Click
pnldeposit.Visible = True
pnlwithdraw.Visible = False
pnlreport.Visible = False
End Sub
Private Sub cmddcancel_Click(sender As Object, e As EventArgs)
pnldeposit.Hide()
End Sub
Private Sub cmdiwithdraw_Click(sender As Object, e As EventArgs) Handles cmdiwithdraw.Click
pnlwithdraw.Visible = True
pnlreport.Visible = False
End Sub
Private Sub cmdwclear_Click(sender As Object, e As EventArgs) Handles cmdwclear.Click
pnlwithdraw.Visible = False
End Sub
Private Sub cmdireport_Click(sender As Object, e As EventArgs) Handles cmdireport.Click
pnlreport.Visible = True
End Sub
//thank you for sharing your idea! i found how it works. thanks! :D

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