Get Max value in array for loop code VB.Net - vb.net

I got an error info while I use the following code to get Max value in array for loop code.
Warning 1 Accessing a shared member, constant member, enum member, or nested type through an instance. Regular expressions are not evaluated.
Private Sub PicVulGraph_MouseMove(sender As Object, e As MouseEventArgs) Handles PicVulGraph.MouseMove
Me.PicVulGraph.Refresh()
Dim g As Graphics = Me.PicVulGraph.CreateGraphics
g.DrawLine(Pens.Red, e.X, 0, e.X, Me.PicVulGraph.Height)
Try
For Each value As Type.Structures.GraphValue In Me._VulGraph
If e.X < value.x1 Then
Me.lblPointValueVul.Text = ""
Me.lblTempVul.Text = ""
ElseIf e.X > value.x1.MaxValue Then
Me.lblPointValueVul.Text = ""
Me.lblTempVul.Text = ""
ElseIf value.x1 = e.X Then
Me.lblPointValueVul.Text = value.PointToTime
Me.lblTempVul.Text = value.Data
Exit For
Else
End If
Next
Catch ex As Exception
End Try
End Sub
The error info occurred from this code
value.x1.MaxValue
How do I get max value in array for loop code ?

Related

System.FormatException: 'Index (zero based) >=0 and < size of the argument list.'

I have a code in vb.net as show below:
Protected Sub gvQPRList_Load(sender As Object, e As EventArgs) Handles gvQPRList.Load
'If Not Session("SurveyID") Is Nothing Then
'If Session("SurveyID").ToString <> "" Then
' If hfSurveyID.Value = "" Then
If gvQPRList.Rows.Count <> 0 Then
gvQPRList.HeaderRow.Cells(0).Text = Me.Master.gm.GetLabel("Flags")
gvQPRList.HeaderRow.Cells(1).Text = Me.Master.gm.GetLabel("Criterion")
gvQPRList.HeaderRow.Cells(2).Text = Me.Master.gm.GetLabel("rpt_Dimensions")
gvQPRList.HeaderRow.Cells(3).Text = Me.Master.gm.GetLabel("InstrumentType")
gvQPRList.HeaderRow.Cells(4).Text = Me.Master.gm.GetLabel("level")
gvQPRList.HeaderRow.Cells(5).Text = Me.Master.gm.GetLabel("DueDate")
gvQPRList.HeaderRow.Cells(6).Text = Me.Master.gm.GetLabel("ConditionStatus")
End If
'End If
'End If
' End If
End Sub
The above code is throwing the following exception at line If gvQPRList.Rows.Count <> 0 Then
System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.'
I am wondering what changes do I need to make in the code above in order to get rid of this exception.

vb.net thread every n second

The following code is making the Application to hang. No error are showed while debugging.
Private Sub SolarInitBtn_Click(sender As Object, e As EventArgs) Handles SolarInitBtn.Click
Try
SolarThread = New Thread(SolarThreadStart) With {
.IsBackground = True,
.Name = "SolarEnergyUpdateSystem"
}
SolarThread.Start()
Catch myError As Exception
End Try
End Sub
Public Sub SolarEnergyUpdate()
Dim Energy As Decimal = SolarStatsLbl.Text
Dim EnergyMax As Int32 = SolarMaxCapLbl.Text
If Energy <= EnergyMax Then
Dim Production As Int32 = SolarProdLbl.Text
Dim ProdTimeQt As Decimal = FormatNumber(CDec(Production / 3600), 3) + Energy
EnergyNowLbl.Text = ProdTimeQt
Thread.Sleep(15000)
Else
SolarThread.Abort()
End If
End Sub
What I try is to catch some numbers, do the maths and put the result into a label, I expected to do that each second or at least for less than 1 minute. Currently the application is hanging.
I catch for exceptions there is none. (Using vb.NET)

Highlight specific text while user is typing

I am writing a code that highlight the duplicate words in a text. The code is working well when I add a button and the user have to press on the button to check for duplicates.
But I want to make an auto-checking code. I set my code in a subroutine that Handles RichTextBox.TextChanged. The problem is the code selects the target word and highlight it but the selection remains so when a new letter is typed, it clear what has been highlighted.
Here is my code:
Private Sub RichTextBox_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox.TextChanged
Try
Call duplicate_check()
Catch ex As Exception
MessageBox.Show("error in RichTextBox.TextChanged")
End Try
End Sub
duplicate check function:
Private Sub duplicate_check()
Try
' read line by line and get input
Dim LineByLineInput() As String = RichTextBox.Lines
Dim selectionStart, selectionLength As Integer
Dim i, j As Integer
For lineNumber = 0 To UBound(LineByLineInput)
selectionStart = 0
selectionLength = 0
'get index of first char index in the current line
Dim count As Integer = lineNumber
While count <> 0
selectionStart += RichTextBox.Lines(count - 1).Length + 1
count -= 1
End While
' get line as string
Dim line As String = RichTextBox.Lines(lineNumber)
' split line into array of strings
Dim input() As String = line.Split(" ")
'check for duplicates
i = 0
For j = i + 1 To UBound(input)
If input(i) = input(j) Then 'compare each 2 consecutive words if they are the same
selectionStart += input(i).Length + 1
selectionLength = input(i).Length
RichTextBox.SelectionStart = selectionStart
RichTextBox.SelectionLength = selectionLength
RichTextBox.SelectionBackColor = Color.Yellow
Else
selectionStart += input(i).Length + 1
End If
i += 1
Next
Next
Catch ex As Exception
MessageBox.Show("error duplicate_check()")
End Try
End Sub
After your duplicate_check call, have you tried to set the selection of the RichTextBox back to the old position ?
See below :
Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
Try
' Get current position
Dim cur_pos As Integer = Me.RichTextBox.SelectionStart
Call duplicate_check()
' Set back to old position
Me.RichTextBox.SelectionStart = cur_pos
' Unselect what your sub duplicate_check has selected
Me.RichTextBox1.SelectionLength = 0
Catch ex As Exception
MessageBox.Show("error in RichTextBox.TextChanged")
End Try
End Sub
If this solution is good for you, you should change your duplicate_check Sub to make this change and not in the RichTextBox1_TextChanged Sub

Problems with VB code regarding Blank Input Box and error converting string "" to double

Hello I'm writing a simple VB form and so far everything is working great. However if I leave a blank input box for "SpaceLeftText.Text" and hit enter I get this error:
"An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid.
Here is the code I am using:
Public Class Form1
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click, MyBase.Activated
SpaceLeftText.Text = Math.Round((((Val(AxleLengthText.Text - 12.7) / 2) - 1.59 - 3)), 2)
Dim count As Decimal
count = SpaceLeftText.Text
If count > 10 Then
TenText.Text = Int(count / 10)
count = count - (TenText.Text * 10)
Else : TenText.Text = 0
End If
If count > 4 Then
FourText.Text = Int(count / 4)
count = count - (FourText.Text * 4)
Else : FourText.Text = 0
End If
If count > 2 Then
TwoText.Text = Int(count / 2)
count = count - (TwoText.Text * 2)
Else : TwoText.Text = 0
End If
If count > 1.5 Then
OnePointFiveText.Text = Int(count / 1.5)
count = count - (OnePointFiveText.Text * 1.5)
Else : OnePointFiveText.Text = 0
End If
If count > 1 Then
OneText.Text = Int(count / 1)
count = count - (OneText.Text * 1)
Else : OneText.Text = 0
End If
End Sub
Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
AxleLengthText.Clear()
SpaceLeftText.Clear()
TenText.Clear()
FourText.Clear()
TwoText.Clear()
OnePointFiveText.Clear()
OneText.Clear()
GapText.Clear()
QuadRingText.Clear()
OringSpacerText.Clear()
FitText.Clear()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
End Sub
End Class
so I Added the following code above Dim Count as decimal:
If SpaceLeftText.Text = "" Then
MsgBox("Please Enter Axle Length!")
Else
but now as soon as i run it, without hitting enter and leaving a blank input box I immediately get my message box saying "Please Enter Axle Length". I hit ok and it immediately pops back up for about 14 times before going into the program. Does anyone have any input. I do not know what I'm doing wrong.
Thank You.
The message is appearing 14 times because your event is handling (MyBase.Activated).
Try the following instead:
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
Also make sure that (AxleLengthText.Text) is not empty and is valid to be converted to double value as shown below:
dim tmpDouble as double
if not string.isNullOrEmpty(AxleLengthText.Text) andalso Double.TryParse(AxleLengthText.Text, tmpDouble ) Then
SpaceLeftText.Text = Math.Round((((Val(tmpDouble - 12.7) / 2) - 1.59 - 3)), 2)
end If

Index out-of-range error

I am getting an error when I execute this button event: here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
' get the details of the item
Dim R As Your_pharmacy.POSDS.ItemsRow = Button1.Tag
' next search for the barcode in the datagridview
Dim I As Integer
Dim ItemLoc As Integer = -1
For I = 0 To DGV1.Rows.Count - 1
If R.barcodeNumber = DGV1.Rows(I).Cells(0).Value Then
' item found
ItemLoc = I
Exit For
End If
Next
' if item is not found, add it
If ItemLoc = -1 Then
DGV1.Rows.Add(R.barcodeNumber, R.ItemName, R.BuyPrice, R.SellPrice, 1, R.SellPrice)
Else
' if item is already there increase its count
Dim ItemCount As Long = DGV1.Rows(ItemLoc).Cells(4).Value
ItemCount += 1
Dim NewPrice As Decimal = R.SellPrice * ItemCount
DGV1.Rows(ItemLoc).Cells(4).Value = ItemCount
DGV1.Rows(ItemLoc).Cells(5).Value = NewPrice
End If
' next clear textbox1 and set focus to it
TextBox1.Text = ""
TextBox1.Focus()
' compute the total for the recipt
Dim Sum As Decimal = 1
For I = 0 To DGV1.Rows.Count - 1
Sum += DGV1.Rows(I).Cells(5).Value 'here the error happens
Next
TextBox4.Text = Sum
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
End Try
End Sub
The error details:
error: index was out of range.must be non-negative and less than the
size of the collection. parameter name: index vb.net
DGV1 must have fewer cells than 5. When the error occurs, use a breakpoint and the debug watch window to see how many cells there are in DVG1(I). Maybe the first one is created with zero cells?