How do I add all the variables in VB.net? I do what I was instructed to increment it by placing it outside. Now that I have done it whenever I press the button the output is only 95 and whenever I click multiple times on the picturebox with the incrementation the output is still the same from last time. Nothing is really happening
Private BibimbapQuantity = 1
Private BulgogiQuantity = 1
Private BibimbapPrice As Integer = 45 * BibimbapQuantity
Private BulgogiPrice As Integer = 50 * BulgogiQuantity
Private TotalPriceInt As Integer = BibimbapPrice + BulgogiPrice
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
Me.DataGridView2.Rows.Add("Bulgogi", "1", BulgogiPrice)
End Sub
Private Sub totalPrice_Click(sender As Object, e As EventArgs) Handles totalPrice.Click
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
totalPrice.Text = TotalPriceInt
End Sub
You might've written the formula at the top but it's only executed once. You need to execute the formula every time.
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity ' <------
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
You'll need to do the same with the total.
When you are done, put your code in https://codereview.stackexchange.com/ they will help you a lot with refactoring it. Only post on codereview when the code is actually working. They will help you find better way of writing your logic and find possible problems. Great learning opportunity.
Related
I am using for, while & do while loop in vb.net to add some values in list box. I want to use a timer to delay in adding values to list box. Please tell the syntax, what should I use & Following is my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer
For a = 1 To 10
ListBox1.Items.Add(a)
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim b As Integer
b = 4
If b < 5 Then
MessageBox.Show("eRROR")
End If
While b <= 3
ListBox2.Items.Add(b)
b = b + 1
End While
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim c As Integer
c = 20
Do
c = c + 1
ListBox3.Items.Add(c)
Loop While c <= 30
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
A timer executes once every n milliseconds, you take advantage of this by handling the timer's Tick event. If you wanted to keep track of what increment you're on, either declare a private variable outside of the scope (see here) of the Tick event or declare a static variable inside the Tick event.
Private Variable:
Private counter As Integer = 0
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
counter += 1
If (counter < 31) Then
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub
Static Variable:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Static counter As Integer = 0
If (counter < 31) Then
counter += 1
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub
I am currently enrolled in a visual basic class through my university and I am having trouble with one of the exercises assigned to me. I need to display the cost of an item for a company and I cannot get anything to display in a label.
This is the code
Option Explicit On
Option Strict On
Option Infer Off
Public Class MainForm
Private Sub BtnCalc_Click(sender As Object, e As EventArgs) Handles BtnCalc.Click
Dim intCost As Double
If RadTwin.Checked Then
intCost = 39.99
ElseIf RadFull.Checked Then
intCost = 49.99
ElseIf RadQueen.Checked Then
intCost = 49.99
ElseIf RadKing.Checked Then
intCost = 69.99
End If
lblCost.Text = intCost.ToString("n")
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub RadTwin_CheckedChanged(sender As Object, e As EventArgs) Handles RadTwin.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadFull_CheckedChanged(sender As Object, e As EventArgs) Handles RadFull.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadQueen_CheckedChanged(sender As Object, e As EventArgs) Handles RadQueen.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadKing_CheckedChanged(sender As Object, e As EventArgs) Handles RadKing.CheckedChanged
lblCost.Text = String.Empty
End Sub
End Class
I need to display the cost of each item in the lblCost when the radio button is chosen. I have worked at getting this to work for hours but I am not knowledgeable enough to know what to do. If anyone has any ideas on how to make this code work I would greatly appreciate it. It is likely something stupid simple that I am missing but I want to learn how to solve this problem if it happens again in the future.
I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it
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 :)
I would like to do a For Loop inside a timer. Since I want to check an array with element row continuously and I cant continue since the loop seems to not move from 0 on tick, is there a way around this?
I first tried this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For row = 0 To 9
msgbox(row)
Next
And then I tried another approach, as suggested in an answer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
row+=1
msgbox(row)
if row = 10 then
timer1.stop()
end if
Next
MsgBox will now output 0++ on tick but does not stop at 10.
Output Picture
Dim row As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshData()
Timer1.Enabled = True
initialize()
End sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
increment()
MsgBox(row)
End sub
Private Sub increment()
If row > 8 Then
row = 0
Else
row = row + 1
End If
'my if else statement for checking array variable(row)
End Sub
With this code msgbox was able to output 0 to 9 and repeat the process since i need it to continuously monitor the array. For some reason though when msgbox is situated at the first line of increment method or on timer tick before increment is called the output stays -1 the whole time dunno why. Anyways thanks for all the input as im still new to visual basic 2015
Instead of using for loop use a global variable and increment it inside the tick function
Dim ctr as integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ctr=ctr+1
row(ctr)
msgbox(row)
if ctr == 10 then
ctr = 0
timer1.stop
end if