number variable won't addition by 1 and display in a label everytime the timer counts 1000ms. - vb.net - vb.net

Here is my code:
number = 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
number += 1
lblFN2.Text = number
Else
lblFN2.Text = "0"
End If
End If
i have a checkbox, progressbar and a label.
when the progress bar is lower than 100 i want the number variable to + 1 every time the timer counts 1. I've tried "X = X + 1" - it worked to a point but i need to reset the variable to "0" when the progressbar hits 100% and when i click the stop button.
while I've been typing this i've also tried:
X = X + 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
lblFN2.Text = X + 1
ElseIf AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded
Then
X = 0
lblFN2.Text = "0"
Else
X = 0
lblFN2.Text = "0"
End If
End If
But when you start the timer again it just resumes from it's last number e.g. "13" and doesn't reset to "0".
Thanks for the help guys :)

Use your first code, but add in
number += 1
to the Else condition. You need to reset the variable or it just increments it again.

Related

How to setting textbox in userform for fractional value?

I have two problems about the fraction number in user form textbox.
How to retrieve a value from excel sheet and show it in the textbox with fraction. For example 0.5(sheet) will be shown as 1/2 (textbox). not all values are fraction, there are also value for integer
This is the code to retrieve the value from the sheet
Set ctlTXT = Me.SizeFrame.Controls.Add("Forms.TextBox.1")
ctlTXT.name = "OD" & counter
ctlTXT.value = Sheet2.Range("P" & findstart + counter - 1).value
ctlTXT.Left = 72
ctlTXT.Height = 15: ctlTXT.Width = 54
ctlTXT.Top = 45 + ((counter - 1) * 17 + 2)
How to return the fractional value inserted in the user form textbox using case select statement. From the code below, there is no value return when 1/2 or 3/4 inserted in textbox.
This is the code for case select
Select Case X
Case "1 / 2"
Y = 15
Case "3 / 4"
Y = 20
Case 2
Y = 40
End Select
Both of these code is not a complete code.
For the first one, you can try
ctlTXT.Value = WorksheetFunction.Text(Sheet2.Range("P" & findstart + counter - 1).Value, "#?/?")

How to Loop through a If condition untill the user enter a required value, in Visual Basic

Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need

VBA Excel automatic colour and value change

I am trying to set up a personal management spreadsheet for work. I have a list of tasks with varying priority.
What I am trying to do here is if the number of tasks * priority goes hits certain thresholds the colour of the availability cells changes and the description cell value changes, eg "busy"
here is the code I have so far, how do I implement it to change automatically when I change the value of the task list
Sub Avail_flag()
TasksRange = ActiveSheet.Range("P3:P6")
availcells = Range("M8,N8")
busyflag = 0
medBusyFlag = 0
highBusyFlag = 0
imedBusyFlag = 0
If Range("p4") > 0 Then
medBusyFlag = 1
ElseIf Range("p4") > 2 Then
medBusyFlag = 2
ElseIf Range("p5") > 0 Then
highBusyFlag = 1
ElseIf Range("p5") > 2 Then
highBusyFlag = 2
ElseIf Range("p6") > 0 Then
imedBusyFlag = 1
End If
For Each sell In lRange
busyflag = (medBusyFlag + (highBusyFlagI * 2) + (imedBusyFlag * 3))
If busyflag > 0 Then
For Each cell In Range(availcells)
cell.Color = green
Next
cell("N8").Value = "Occupied"
ElseIf busyflag > 3 Then
For Each cell In Range(availcells)
cell.Color = orange
Next
cell("N8").Value = "Busy"
ElseIf busyflag > 5 Then
For Each cell In Range(availcells)
cell.Color = red
Next
cell("N8").Value = "Unavailable"
Else
For Each cell In Range(availcells)
cell.Color = white
End If
End Sub
here is a capture of the spreadsheet if that helps, the highlighted grey part is where all the magic happens
You can use the Change event for the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
I went for conditional formatting, something I hadn't heard of before. After looking it up and learning how to use it it seem to be by far the best option. Thank you #mehow for the usggestion

Issues with For Loop in VB

For i = 1 To 5
If i = 0 Then
i = i + 1
ElseIf i Mod 2 = 0 Then
LabelEvens.Text = i
i = i + 1
Else
LabelOdds.Text = i
i = i + 1
End If
Next i
I'm making a program in VB where I have to use a for loop to sort between 2 numbers(loop limit 1 and 2) and find if they are even or odd, Then output the results to 2 labels. This loop makes sense to me, but for example when I put in 1 and 4 all it outputs is a 5 in the odd label. I guess my question is can anyone see the issue with my loop?
You don't need to add 1 to your loop variable i manually, the for loop itself does that for you behind the scenes:
For i = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = i
Else
LabelOdds.Text = i
End If
Next i
You'll noticed I've also removed the If i = 0 bit since i can never be zero within that loop. It ranges from one to five inclusive.
One other thing you'll need to do is to append the value to your text box. What you have at the moment is a replacement so that it'll only be set to the last value processed. Something like this should suffice:
' Initialise to empty strings '
LabelEvens.Text = ""
LabelOdds.Text = ""
' Append the values '
For i = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = LabelEvens.Text & "," & CStr(i)
Else
LabelOdds.Text = LabelOdds.Text & "," & CStr(i)
End If
Next i
' Remove initial comma from both '
LabelEvens.Text = Mid(LabelEvens.Text,2)
LabelOdds.Text = Mid(LabelOdds.Text,2)
Some issues in your code:
For i = 1 To 5
If i = 0 Then <-- 'I' will never be 0 since you start from 1
i = i + 1 <-- Don't manually increment since you are using a for
ElseIf i Mod 2 = 0 Then
LabelEvens.Text = i
i = i + 1 <-- Don't manually increment since you are using a for
Else
LabelOdds.Text = i
i = i + 1 <-- Don't manually increment since you are using a for
End If
Next i
Another issue you have is that if you have more than one odd number in the for range (say in a range of 1 to 10) you will only get the last number. What do you want to do in this case? Concatenate all odd numbers in a string or stop after the first one is found? Do you really need a FOR loop at all?
you can Also state
LabelEvens.Text="" 'Clear contents of the label before assigning new values
LabelOdds.Text=""
For i As Integer = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = LabelEvens.Text & i
Else
LabelOdds.Text = LabelOdds.Text & i
End If
Next
From Above you can replace '&' with '+' if you want the Total.

Listview item or subitem onchange event

When I have a listview with like 46 items, where each item has 4 subitems, the values of the subitems are changing with time, I use the values of subitems to draw a pie chart when an item in the list view is selected using this code:
Chart1.Series("Series1").ChartType = SeriesChartType.Pie
Chart1.Series("Series1").Points.Clear()
If ListView3.FocusedItem.SubItems(1).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(1).Text)
End If
If ListView3.FocusedItem.SubItems(2).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(2).Text)
End If
If ListView3.FocusedItem.SubItems(3).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(3).Text)
End If
If ListView3.FocusedItem.SubItems(4).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(4).Text)
End If
Is there a way to detect a change of a subitem's value? Like onchange event in textbox, but for items or subitems, because I want the pie chart to update when a subitem value changes.
this code changes the sub items
For xx As Integer = 0 To ListView3.Items.Count - 1
If ListView3.Items(xx).SubItems(0).Text = googleXMLdocument...<s:name>(j).Value Then
If j + 1 = 1 Then
ListView3.Items(xx).SubItems(1).Text += 1
End If
If j + 1 = 2 Then
ListView3.Items(xx).SubItems(2).Text += 1
End If
If j + 1 = 3 Then
ListView3.Items(xx).SubItems(3).Text += 1
End If
If j + 1 > 4 Then
ListView3.Items(xx).SubItems(4).Text += 1
End If
End If
Next
Try this code. This way the chart will automatically get updated if there is any change in the subitems. You will not require a timer.
For xx As Integer = 0 To ListView3.Items.Count - 1
If ListView3.Items(xx).SubItems(0).Text = googleXMLdocument...<s:name>(j).Value Then
If j + 1 = 1 Then
ListView3.Items(xx).SubItems(1).Text += 1
End If
If j + 1 = 2 Then
ListView3.Items(xx).SubItems(2).Text += 1
End If
If j + 1 = 3 Then
ListView3.Items(xx).SubItems(3).Text += 1
End If
If j + 1 > 4 Then
ListView3.Items(xx).SubItems(4).Text += 1
End If
End If
Next
Put the below code after the above code in a loop. You can also do away with the FocusedItem
Chart1.Series("Series1").Points.Clear()
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(1).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(2).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(3).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(4).Text)