I need to validate that the value in a particular cell in my datagridview falls within a certain range (0 - 99.9999) and if not, cancel the user edit. What I have so far is validating a column that it is a double data type and that it is > 0. That works. But now I need to validate each particular cell in that column since they all will need to be validated for particular numeric ranges. Each cell will have a different range of values it can accept.
Private Sub dgvPidVals0_cellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dgvPidVals0.CellValidating
'Me.dgvPIDStatus0.Rows(e.ColumnIndex).ErrorText = ""
Dim newDouble As Double
Select Case e.ColumnIndex
Case 1
If Not Double.TryParse(e.FormattedValue.ToString(), newDouble) OrElse newDouble < 0 Then
e.Cancel = True
MessageBox.Show("PID Parameters values must be non-negative numeric values only!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
dgvPidVals0.CancelEdit()
If dgvPidVals0.Rows(5).Cells(1).Value > 99.9999 Then
e.Cancel = True
MessageBox.Show("Output Filter Value must be less than 100.0!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
dgvPidVals0.CancelEdit()
End If
End If
End Select
End Sub
Tez, I've already tried that exact code and it doesn't work. I'm stumped as to why not. The first If statement of the code works fine. The Else If portion does not work. I punch in a value greater than 100 in the DGV's cell and it takes (when it should not). I then click on the form again, and the Message box appears. I can't do anything at that point since every click of the DGV will make the message box show up. In the end, I used the CellEndEdit event handler to check that values in each of the cells that I'm evaluating fall within a certain range. It's a hacked way of doing it I think and I would think there is a better, more clean way of doing it. But....it works. Still open to answers from others as to what to do or if using the CellEndEdit event is the actual way to do it.
Private Sub dgvPidVals0_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgvPidVals0.CellEndEdit
If dgvPidVals0.Rows(0).Cells(1).Value > 6.14 Then
dgvPidVals0.Rows(0).Cells(1).Value = axis0Parameters(61) 'Load previous value into the cell
MessageBox.Show("Proportional Gain Value must be <= 6.14!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(1).Cells(1).Value > 58.94 Then
dgvPidVals0.Rows(1).Cells(1).Value = axis0Parameters(62)
MessageBox.Show("Integral Gain Value must be <= 58.94!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(2).Cells(1).Value > 0.1753 Then
dgvPidVals0.Rows(2).Cells(1).Value = axis0Parameters(63)
MessageBox.Show("Differential Gain Value must be <= 0.1753!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(3).Cells(1).Value > 5 Then
dgvPidVals0.Rows(3).Cells(1).Value = axis0Parameters(64)
MessageBox.Show("Feed Forward Value must be <= 5!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(4).Cells(1).Value > 1.5 Then
dgvPidVals0.Rows(4).Cells(1).Value = axis0Parameters(65)
MessageBox.Show("Rate Feed Forward Value must be <= 1.5!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(5).Cells(1).Value > 100 Then
dgvPidVals0.Rows(5).Cells(1).Value = axis0Parameters(39)
MessageBox.Show("Output Filter Value must be <= 100.0!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
slider1.Value = dgvPidVals0.Item(1, 0).Value
slider2.Value = dgvPidVals0.Item(1, 1).Value
slider3.Value = dgvPidVals0.Item(1, 2).Value
slider4.Value = dgvPidVals0.Item(1, 3).Value
slider5.Value = dgvPidVals0.Item(1, 4).Value
slider6.Value = dgvPidVals0.Item(1, 5).Value
End Sub
Related
If user inputs something else than an integer, program will not proceed but will give a message box telling there is an error.
Here is what I have now and it's not working:
Sub Validation0()
If IsNumeric(curBat) Then
' Here, it still could be an integer or a floating point number
If CLng(curBat) = curBat Then
Else
MessageBox.Show("Please enter a number greater than 0.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End If
End Sub
Try to diversify your code
1) identify whether string is integer
2) then identify whether it is greater than 0
Sub Validation0()
If IsNumeric(curBat) Then
' Here, it still could be an integer or a floating point number
dim curBat2 as integer = Convert.ToInt32(curBat)
If curBat2 > 0 Then
Else
MessageBox.Show("Please enter a number greater than 0.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End If
End If
End Sub
If you want to round off integer value then use
Math.Roundoff(curBat2)
I have a task to make a calculation of selling price and cost value.
I don't know have any idea to make a good coding for exception.
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
Dim SellingPrice As Double
Dim CostValue As Double
Dim Commission As Double
Dim CommissionRate As Double = 0.2
If Me.TextBoxSellingPrice.Text <> "" Or Me.TextBoxCostValue.Text <> "" Then
Try
SellingPrice = Double.Parse(TextBoxSellingPrice.Text)
CostValue = Double.Parse(TextBoxCostValue.Text)
'Calculation
Commission = CommissionRate * (SellingPrice - CostValue)
'Display
Me.TextBoxDisplay.Text = Me.TextBoxName.Text
Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")
Catch
MessageBox.Show("Wrong Input", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
MessageBox.Show("Please fill your data completely", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Output:
Figure 1
Using exception handling to check that data is correct isn't really the best practice. For a start, the performance of Try..Catch blocks is pretty poor compared to just using If..Then..Endif as there is a fair amount of stuff happening in the background with exceptions. In your case it wont make a major difference, but if the Try..Catch..End Try is in a loop, your program can take quite a performance hit.
There is little else to do other than to write some If..End If statements to validate your input data .. As other commenters had stated, using .TryParse is an exellent way to start things off ..
If Not Double.TryParse(TextBoxSellingPrice.Text, SellingPrice) Or Not Double.TryParse(TextBoxCostValue.Text, CostValue) Then
MessageBox.Show("Invalid input. Please enter positive numbers only")
Exit Sub
End If
If SellingPrice < CostValue Then
MessageBox.Show("Selling Price must be greater than cost")
Exit Sub
End If
If Not SellingPrice > 0 Or Not CostValue > 0 Then
MessageBox.Show("Selling price and cost must be >0")
Exit Sub
End If
'Calculation
Commission = CommissionRate * (SellingPrice - CostValue)
'Display
Me.TextBoxDisplay.Text = Me.TextBoxName.Text
Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")
You could also have a look at the ErrorProvider component
I want my program to check whether the inputs in a TextBox meet certain condition. If the target condition is not met, the cursor should focus back on that particular TextBox.
My code:
Private Sub ButtonSubmit_Click(sender As Object, e As EventArgs) Handles ButtonSubmit.Click
EnterVotes.LabelCan1.Text = CandName1.Text
EnterVotes.Labelcan2.Text = CandName2.Text
EnterVotes.LabelCan3.Text = CandName3.Text
EnterVotes.LabelCan4.Text = CandName4.Text
EnterVotes.LabelCan5.Text = CandName5.Text
If CandName1.Text = "" Then
MessageBox.Show("Please enter a name in Candidate 1")
End If
loading.Show()
Me.Hide()
If CandName1.Text = "" Then //Show your message here // CandName1.focus()//to return to that textbox Else //Show your
message here End If
use the method focus() to return and re-write into that textbox
It is actually quite simple
just check if the value of the text is > 1
then put focus on the textbox
heres an example
if txtbox.text.value < 1 then
messagebox.show("You must enter data for textbox")
txtbox.focus()
end if
and then continue method for each text box you are working with
Could get fancy and use some linq to go threw your names.
Dim Candidate() As TextBox
Candidate = Me.Controls.OfType(Of TextBox)().Where(Function(c) c.Name.Contains("CandName")).ToArray()
Dim i As Integer = 0
While i < Candidate.Count
If(Candidate(i).text.value < 1)
MessageBox.Show("Please enter a name in Candidate " & (i + 1).ToString())
Candidate(i).Focus()
Exit While
End If
i += 1
End While
This way you can check all your candidates in one shot. This is untested code, but I think it should work.
You can play around with this and edit however you please it's pretty flexible at this point.
I think this might be the way but not very efficient.
If CandName1.Text = "" Then
MessageBox.Show("Please enter a name in Candidate 1")
CandName1.Focus()
Else
CandName2.Focus()
End If
If CandName2.Text = "" Then
MessageBox.Show("Please enter an name in Candidate 2")
CandName3.Focus()
Else
CandName3.Focus()
End If
If CandName3.Text = "" Then
MessageBox.Show("Please enter a name in Candidate 3")
CandName3.Focus()
Else
CandName4.Focus()
End If
If CandName4.Text = "" Then
MessageBox.Show("Please enter a name in candidate 4")
CandName4.Focus()
Else
CandName5.Focus()
End If
If CandName5.Text = "" Then
MessageBox.Show("Pleae enter a name in candidat 5")
CandName5.Focus()
Else
loading.Show()
End If
If String.IsNullOrWhitespace( CandName1.Text ) Then
MessageBox.Show("Please enter a name in Candidate 1")
CandName1.Focus()
Return
End If
... for all five
loading.Show()
Me.Hide()
You want to make sure to exit this early with the Return statements so you don't get to the code:
loading.Show()
Me.Hide()
How can I can get tha range of value, if the value is in the range the result is true else false. Example first given value is 5 and second given number is 8. If the I input 5 or 6 or 7 or 8 the output is PASSED but if its not in the range ouput will be FAILED.
Try
If CDbl(TextBox16.Text) > CDbl(TextBox13.Text) And CDbl(TextBox16.Text) > CDbl(TextBox14.Text) Then
TextBox17.Text = "FAILED"
Else
TextBox17.Text = "PASSED"
End If
Catch ex As Exception
MessageBox.Show(" Required Complete LCR Specification!", "Invalid Process", MessageBoxButtons.OK, MessageBoxIcon.Stop)
TextBox16.Focus()
End Try
If CDbl(TextBox16.Text) < CDbl(TextBox13.Text) Or CDbl(TextBox16.Text) > CDbl(TextBox14.Text) Then
TextBox17.Text = "FAILED"
Else
TextBox17.Text = "PASSED"
End If
Catch ex As Exception
MessageBox.Show(" Required Complete LCR Specification!", "Invalid Process", MessageBoxButtons.OK, MessageBoxIcon.Stop)
TextBox16.Focus()
End Try
I think this will work.
Your TextBox13 should contains starting number and TextBox14 should contain ending number
Try This
TextBox1.Text=5
TextBox2.Text=8
If CDbl(TextBox3.Text) >= CDbl(TextBox1.Text) And CDbl(TextBox3.Text) <=
CDbl(TextBox2.Text) Then
MsgBox("Passed")
Else
MsgBox("failed")
End If
I am having problem with a button the code of my button is.
Private Sub btnCalculateCosts_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculateCosts.Click
it handles:
' This Calculate Costs button click event handler edits the
' registration(costs) form to ensure it contains valid data.
' Then, after passing control to the business class, it
' displays the registration cost.
Dim objCourse As Course
Dim objCourseCostsFile As Course
Dim InputError As Boolean = False
' Is student ID entered properly
If Me.txtCorporateID.MaskFull = False Then
MessageBox.Show("Enter your Corporate ID in the Corporate ID box", "Error")
Me.txtCorporateID.Clear()
Me.txtCorporateID.Focus()
InputError = True
' Is student name entered properly
ElseIf Me.txtFirstName.TextLength < 1 Or _
Me.txtFirstName.Text < "A" Then
MessageBox.Show("Please enter your first name in the First Name box", "Error")
Me.txtFirstName.Clear()
Me.txtFirstName.Focus()
InputError = True
' Is number of units entered properly
ElseIf Me.txtLastName.TextLength < 1 Or _
Me.txtLastName.Text < "A" Then
MessageBox.Show("Please enter your last name in the Last Name box", "Error")
Me.txtLastName.Clear()
Me.txtLastName.Focus()
InputError = True
' Is number of units entered properly
ElseIf Not IsNumeric(Me.txtNumberOfDays.Text) Then
MessageBox.Show("Enter the units in the Number of Units box", "Error")
Me.txtNumberOfDays.Clear()
Me.txtNumberOfDays.Focus()
InputError = True
' Has 1-4 units been entered
ElseIf Convert.ToInt32(Me.txtNumberOfDays.Text) < 1 _
Or Convert.ToInt32(Me.txtNumberOfDays.Text) > 4 Then
MessageBox.Show("Units must be 1 - 4", "Error")
Me.txtNumberOfDays.Clear()
Me.txtNumberOfDays.Focus()
InputError = True
End If
' If no input error, process the registration costs
If Not InputError Then
If Me.radPreConferenceCourse.Checked = False Then
objCourse = New Course(txtCorporateID.Text, txtFirstName.Text, txtLastName.Text, txtNumberOfDays.Text)
Me.lblCosts.Visible = True
Me.lblCosts.Text = "Total Conference Costs Are: " & (objCourse.ComputeCosts()).ToString("C2")
Else
objCourse = New Course(txtCorporateID.Text, txtFirstName.Text, txtLastName.Text, txtNumberOfDays.Text, g)
Me.lblCosts.Visible = True
Me.lblCosts.Text = "Total Conference Costs Are: " & (objCourse.ComputeCosts()).ToString("C2")
....
Receiving the error:
Handles clause requires a WithEvents variable defined in the
containing type or one of its base types.
Where is btnCalculateCosts defined? – SLaks 14 hours ago