Here is my code in vb.net for automatically changing image using timer but code doesn't work...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.BackgroundImage = Image.FromFile("C:\images\\" + Label1.Text + "1.jpg")
Dim i As Integer = Convert.ToInt32(Label1.Text)
i += 1
If (i > 4) Then
i = 1
End If
Label1.Text = i.ToString()
End Sub
Move the counter out to class level. Incorporating the comments from dbasnett as well. Also, shouldn't the label reflect the current image number? The way you have it the value in the Label is one "ahead" of the background image.
Try something more like:
Private i As Integer = 1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim FileName As String = "C:\images\" & i.ToString & ".jpg"
Try
Me.BackgroundImage = Image.FromFile(FileName)
Label1.Text = i.ToString()
i += 1
If (i > 4) Then
i = 1
End If
Catch ex As Exception
Debug.Print(FileName)
MessageBox.Show(FileName & vbCrLf & vbCrLf & ex.ToString, "Error Loading Image")
End Try
End Sub
Related
I am working on a calculator project, done with 2 operand but struggling to code and solve the 3 operand calc. The problem is when I do for example, 10+10= (answer 20) works fine but 10+10+10= (also answer 20). Seems it forget the 1st variable.
Below is my code on vb2010, please advise if I miss something. Thank you.
Dim first As Double
Dim second As Double
Dim third As Double
Dim answer As Double
Dim process As String
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "x"
process = "*"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "+"
process = "+"
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
first = "0"
second = "0"
answer = "0"
formula.Text = ""
TextBox1.Text = ""
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If process = "+" Then
second = Val(TextBox1.Text)
formula.Text = first & "+" & second & "="
answer = first + second
TextBox1.Text = answer
End If
If process = "*" Then
second = Val(TextBox1.Text)
formula.Text = first & "x" & second & "="
answer = first * second
TextBox1.Text = answer
End If
If process = "/" Then
second = Val(TextBox1.Text)
formula.Text = first & "÷" & second & "="
answer = first / second
TextBox1.Text = answer
End If
If process = "-" Then
second = Val(TextBox1.Text)
formula.Text = first & "-" & second & "="
answer = first - second
TextBox1.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "-"
process = "-"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "÷"
process = "/"
End Sub
I have added a Timer control to a vb.net application that is used to check if a website is up or down. There are two Timer controls in the app. Both are from System.Windows.Forms.Timer.
There is Timer1 and Timer2. Timer1 is used to perform the website check. Timer2 is simply to display a current date and time in the ToolStrip at the base of the form. The Timer2 runs without issue displaying the time, but when I start the website check that runs Timer1, the application freezes after two checks. Sometimes it runs longer, but eventually freezes.
I have to End Task from within Task Manager in order to shut down the application. I have tested this from within the Debugger and after running the executable. I have also completely removed Timer2 and tested, but the freezing remains. Here is my code. Any assistance would be greatly appreciated.
Imports System
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
lblStatus.Text = ""
Timer1.Stop()
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
Public Function CheckAddress(ByVal URL As String) As Boolean
Try
Dim request As WebRequest = WebRequest.Create(URL)
Dim response As WebResponse = request.GetResponse()
Catch ex As Exception
Return False
End Try
Return True
End Function
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim startPoint As Integer = -1
If CheckAddress(txtAddress.Text).ToString() = True Then
rtxtResults.AppendText(" -- " & txtAddress.Text & " - Website shows UP at " & tsClock.Text & " - " & vbNewLine)
ElseIf CheckAddress(txtAddress.Text).ToString() = False Then
rtxtResults.AppendText(" -- " & txtAddress.Text & " - Website shows DOWN at " & tsClock.Text & " - " & vbNewLine)
End If
Do
startPoint = rtxtResults.Find("Website shows DOWN at", startPoint + 1, RichTextBoxFinds.None)
If (startPoint >= 0) Then
rtxtResults.SelectionStart = startPoint
rtxtResults.SelectionLength = "Website shows DOWN at".Length
rtxtResults.SelectionColor = Color.Red
End If
Loop Until startPoint < 0
End Sub
Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs)
tsClock.Text = Now()
End Sub
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
If txtInterval.Text = "" Then
MsgBox("Must enter a number")
Exit Sub
End If
lblStatus.ForeColor = Color.Green
lblStatus.Text = "Running"
btnStart.Enabled = False
btnStop.Enabled = True
Timer1.Interval = Int(txtInterval.Text) * 1000
Timer1.Start()
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
lblStatus.ForeColor = Color.Red
lblStatus.Text = "Stopped"
btnStop.Enabled = False
btnStart.Enabled = True
Timer1.Stop()
End Sub
End Class
Your application's UI will "freeze" until the Timer1_Tick method completes. Most likely your Timer1_Tick's Do...Loop is not exiting. Try deleting the Do and Loop lines.
As the title suggest, I need help on changing the code on an old project I'm planning on revamping for an upcoming one. I need to change the Financial.DDB to Financial.SLN without an error occuring concerning there being too many arguments. Also I think the Main_Form_Load contents should be replaced with lstUsefulLife.Items.Add("2") to ("20"). Can anyone help? Here's the code.
Public Class Main_Form
Private Sub Main_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
i = 3
Do
lstUsefulLife.Items.Add(i.ToString())
i = i + 1
Loop While i <= 20
End Sub
Private Sub btnSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchedule.Click
Dim i As Integer
Dim amount As Double
txtSchedule.Text = ""
txtSchedule.Text = "Year" & vbTab & "Depreciation" & vbNewLine
Dim years As Integer
years = Convert.ToDouble(lstUsefulLife.SelectedItem)
i = 1
Do
amount = Financial.DDB(Convert.ToDouble(txtAsset.Text), Convert.ToDouble(txtSalvage.Text), years, i)
txtSchedule.Text = txtSchedule.Text & i & vbTab & amount.ToString("N2") & vbNewLine
i = i + 1
Loop While i <= years
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
End Class
Hi I am trying to make a program that has 6 checkboxes and when one is checked a label displays the actors take in average, but if more than one is checked the label will only show the highest average of the checked boxes.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
SC = ((Val(425488741) + Val(555909803) + Val(868659354) + Val(966435555) + Val(720388023) + Val(617520987)) / 6)
If CheckBox1.Checked = True Then
Label3.Text = "Sean Connery $" & SC
Exit Sub
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
GL = 513445231
If CheckBox2.Checked = True Then
Label3.Text = "George Lazenby $" & GL
Exit Sub
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
RM = ((Val(785677477) + (426826774) + (666367656) + (624527272) + (481005579) + (405873493) + (316186616)) / 7)
If CheckBox3.Checked = True Then
Label3.Text = "Roger Moore $" & RM
Exit Sub
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
TD = ((Val(362876056) + (271586451)) / 2)
If CheckBox4.Checked = True Then
Label3.Text = "Timothy Dalton $" & TD
Exit Sub
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
PB = ((Val(499954330) + (465588535) + (504705882) + (546490272)) / 4)
If CheckBox5.Checked = True Then
Label3.Text = "Pierce Brosnan $" & PB
Exit Sub
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
DC = ((Val(640803677) + (586090727)) / 2)
If CheckBox6.Checked = True Then
Label3.Text = "Daniel Craig $" & DC
Exit Sub
End If
End Sub
In the future, when asking a question, please do not include all the unnecessary details. If understand your problem correctly, this is one possible simple solution:
Dim incomes = New Integer() {100, 2000, 500}
Dim names = New String() {"John", "Tim", "Allan"}
Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim maxIncome As Integer = 0
Dim name As String = ""
For i = 0 To incomes.Length - 1
If CheckedListBox1.GetItemChecked(i) And incomes(i) > maxIncome Then
maxIncome = incomes(i)
name = names(i)
End If
Next
Label1.Text = name & " $" & maxIncome
End Sub
For this code to work you need to create a CheckedListBox with 3 items in it's collection. However, this approach might lead to a very bad code once there are many values. In that case you should use a database or at least a structure like:
Structure People
Dim name As String
Dim income As Integer
Dim checkBox As CheckBox
End Structure
Also the is no need to write Exit Sub anywhere in your code, it does nothing.
Details: Saulius is correct in that you should use a database and that you should use a checkedlistbox instead to make it much easier. However, I will just assume you want to use separate checkboxes for whatever reason.
Solution:
Create an Actor Class
Public Class Actor
Property Name As String
Property TotalValue As Double
End Class
In your main form
Public Sub DisplayHighestPaidActor(ByVal actorName As String, ByVal isChecked As Boolean)
If isChecked Then
'Add the Actor
selectedActors.Add((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
Else
'Remove the Actor
selectedActors.Remove((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
End If
If (selectedActors.Count > 0) Then
'Display the highest value
lblHighestPaidActor.Text = selectedActors.Item(0).Name.ToString() _
+ " $" + selectedActors.Item(0).TotalValue.ToString()
Else
lblHighestPaidActor.Text = ""
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
DisplayHighestPaidActor("Sean Connery", CheckBox1.Checked)
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
DisplayHighestPaidActor("George Lazenby", CheckBox2.Checked)
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
DisplayHighestPaidActor("Roger Moore", CheckBox3.Checked)
End Sub
I have recently made some changes to one of the subs within a form of my vb project and I am now getting an error whilst attempting to load the form. I have little idea what start index or Parameter name are with relation to a form, so don't where to start looking to solve this issue. Here is the error message I get:
An error occurred creating the form. See Exception.InnerException for details. The error is: StartIndex cannot be less than zero.
Parameter name: startIndex
The Sub that I have made changes to is the last sub in the code below called TextBox1changed_textchanged. I have added all but the last 5 lines as to limit the characters that can be put into the text box. This new code is edited from another forum page so I assume it should work correctly, but I can't be sure as the form will no longer run.
Public Class frmAddQuantity
Private Sub frmFieldMaster_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cmbStateRateSumRatio.Items.Clear()
cmbStateRateSumRatio.Items.Insert(0, "State")
cmbStateRateSumRatio.Items.Insert(1, "Rate")
cmbStateRateSumRatio.Items.Insert(2, "Sum")
cmbStateRateSumRatio.Items.Insert(3, "Ratio")
End Sub
Private Sub bttAddQUAtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddQUAtoDatabase.Click
AddQuantity("'" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & cmbStateRateSumRatio.Text & "'")
InitialiseAll()
frmFieldMaster.InitialiseNewParameter()
Me.Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text <> "Enter SI Units" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub cmbStateRateSumRatio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStateRateSumRatio.SelectedIndexChanged
If TextBox1.Text <> "Enter Quantity" Then
If TextBox2.Text <> "Enter SI Units" Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890^-*()."
Dim Text As String = TextBox2.Text
Dim Letter As String
Dim SelectionIndex As Integer = TextBox2.SelectionStart
Dim Change As Integer
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If Letter = "/" Then
Text = Text.Replace(Letter, "^(-")
SelectionIndex = SelectionIndex - 1
End If
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If charactersAllowed.Contains(Letter) = False Then
Text = Text.Replace(Letter, String.Empty)
Change = 1
End If
TextBox2.Text = Text
TextBox2.Select(SelectionIndex - Change, 0)
If TextBox1.Text <> "Enter Quantity" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
End Class
Tell the debugger to stop when the exception is thrown. Debug + Exceptions, tick the Thrown checkbox for CLR exceptions.
There's a good candidate in your snippet for this exception:
Letter = TextBox1.Text.Substring(SelectionIndex - 1, 1)
You'll need to deal with the possibility that the textbox is empty or the SelectionStart property is 0. Which will always bomb your code with "StartIndex cannot be less than zero".
Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
Private Sub TxtSIUnit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub TxtSIUnit_KeyPress2(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
Dim selectstart
If e.KeyChar = "/" Then
e.Handled = True
selectstart = txtSIUnit.SelectionStart
txtSIUnit.SelectedText = "*^-1"
txtSIUnit.Select(selectstart + 1, 0)
End If
End Sub
I have solved the issue above using this code. Instead of using the .textchanged handle I have used .keypress which stopped the issue I was having previously. The text in the textbox was changing as the form was loading, leading to a problem with the start index of the text. Using .keypress mean only user inputs let the code run, avoiding errors on loading.