For some reason the below query doesn't seem to work. Can anyone advise why it is behaving different to what I expect.
Basically if the combobox.text or textbox.text stated are both blank then a message should show. If one of them are selected then present the current datetime.
If String.IsNullOrEmpty(lbx_Aux_Codes.Text) OrElse String.IsNullOrEmpty(com_Work_Item.Text) Then
MessageBox.Show("Please Select a Task or Work Item")
Else
StartTime = DateTimeLog()
MessageBox.Show(StartTime)
End If
Any help is appreciated.
change OrElse to AndAlso
Your code should look like this
If String.IsNullOrEmpty(lbx_Aux_Codes.Text) AndAlso String.IsNullOrEmpty(com_Work_Item.Text) Then
MessageBox.Show("Please Select a Task or Work Item")
Else
StartTime = DateTimeLog()
MessageBox.Show(StartTime)
End If
OrElse or Or will return true if one is true so if you want that message to pop only when both are empty, since you want to proceed with else if one is filled, then use And instead.
I think it is better to use AndAlso for this requirement. OrElse serves a different "short-circuiting" purpose. You may check this out: Or versus OrElse and use an ElseIf for your second criteria:
If String.IsNullOrEmpty(lbx_Aux_Codes.Text) AndAlso String.IsNullOrEmpty(com_Work_Item.Text) Then
MessageBox.Show("Please Select a Task or Work Item")
ElseIf String.IsNullOrEmpty(lbx_Aux_Codes.Text) Or _
String.IsNullOrEmpty(com_Work_Item.Text) Then
StartTime = DateTimeLog()
MessageBox.Show(StartTime)
End If
Related
I have this if statement right here:
If lbl1.Text <> "Good" Or lbl2.Text <> "Good" Or lbl3.Text <> "Good" Then
MsgBox("Something.")
Exit Sub
End If
This works fine, but I also need to attach another condition to it, but for some reason I am drawing a blank on it. I need it to also pass that it is ok for lbl2 & lbl3 to be an empty string. In order words, if lbl1.text = "Good" then it is ok for lbl2 & lbl3 to be empty, therefore it will not exit sub.
If you look at it from the other way around, what you're saying is that if any one of the labels says "Good", then don't enter this statement, so, you could say enter this statement if that is not the case.
In other words:
If Not(lbl1.Text = "Good" Or lbl2.Text = "Good" Or lbl3.Text = "Good") Then
MessageBox.Show("Something.")
End If
Hope that does the trick!
What about a statement like this?
If Not ((lblA.Text = "Good" OrElse lblA.Text = String.Empty) AndAlso _
(lblB.Text = "Good" OrElse lblB.Text = String.Empty) AndAlso _
(lblC.Text = "Good" OrElse lblC.Text = String.Empty)) Then
MessageBox.Show("Something.")
End If
I have multiple textboxes in a groupbox, and can successfully cycle through them all. However the checkNumbers sub fails to recognise blank/null entries, and also non-numeric characters. The correctValidation boolean should return true if all the criteria are met (no blanks/nulls, and must be a number between 1-20). Any thoughts on how to solve this would be appreciated.
Private Sub checkNumbers()
Try
For Each txt As TextBox In Me.gbTechnical.Controls.OfType(Of TextBox)()
If txt.Text <> "" And IsNumeric(txt.Text) And (Integer.Parse(txt.Text) >= 1 And Integer.Parse(txt.Text) <= 20) Then
correctValidation = True
Else
correctValidation = False
MsgBox("Please ensure all numbers are between 1 and 20")
Exit Sub
End If
Next
Catch ex As Exception
MessageBox.Show("General: Please ensure all numbers are between 1 and 20")
End Try
End Sub
I would use Integer.TryParse and then >= 1 AndAlso <= 20. You could use this LINQ query:
Dim number As Int32
Dim invalidTextBoxes =
From txt In gbTechnical.Controls.OfType(Of TextBox)()
Where Not Integer.TryParse(txt.Text, number) OrElse number < 1 OrElse number > 20
Dim correctValidation = Not invalidTextBoxes.Any()
Note that you should almost always use AndAlso instead of And and OrElse instead of Or since those operators are Is short-circuiting boolean operators. This can be more efficient and - more important - can prevent errors. Consider this:
Dim text = ""
If txt IsNot Nothing And txt.Text.Length <> 0 Then text = txt.Text
This fails if txt is nothing since the second condition is evaluated even if the first already was evaluated to false which causes a NullReferenceException at txt.Text.
if you only want a number value, why don't you try to use NumericUpDown. You can also set the Minimum and Maximum in property or use
NumericUpDown1.Maximum = 20
so, there won't be a need to do checkNumbers.
Or is there any reason that you have to use textbox??
say I wanted the following code:
Sub X
If TextBox1.Text = "Value" then
' Do something
ElseIf TextBox1.Text = "Value1" then
' Also do some other code
End IF
End Sub
How would I do this?
I would like the program to check something first, and if that is true then check something else, and if that is true, also execute that code.
Are you looking for AndAlso?
If TextBox1.Text = "Value" AndAlso TextBox2.Text = "Value1" Then
....
End If
The AndAlso operator performs a logical operation between the two sides of the expression. It evaluates the first condition and if this condition is false it stop further processing (without evaluating the second expression). Only if both conditions are true the code inside the if is executed. This behavior is called short-circuiting evaluation
However, the code in your question cannot be evaluated as true in both conditions for the same TextBox1
If condition1
then
if condition2
then
// do something
end if
end if
If the example in your code is valid, equals value1 then equals value2, do you mean you want if either, because it cannot be both equal?
In this case you can use OR.
Instead of else-if, do:
If TextBox1.Text = "Value" then
' Do something
end if
If TextBox1.Text = "Value1" then
' Also do some other code
End IF
or:
If TextBox1.Text = "Value" then
' Do something
If TextBox1.Text = "Value1" then
' Also do some other code
End IF
end if
Depending on whether you want to execute B only if A is also true.
I'm trying to find a way to only allow a user to input ".32" or "Not Used" into my datagridviewcell. Right now my code stands as below. When I run the code all it does is change everything to "Not Used". Even if I type in ".32" it will change it to "Not Used". Is there anyway to fix this? I appreciate any help or suggestions anyone can give.
If (e.ColumnIndex = 6) Then ' This specifies the column number
Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
MessageBox.Show("Cannot Be Empty") ' This will prevent blank datagridviewcells
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Not Used"
End If
Select Case cellData
Case 0.32
DataGridViewSize.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0.32
Case Else
DataGridViewSize.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Not Used"
End Select
End If
I think it no necessary to use select string
Better you change to Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
i am using grid view in vb.net.
here is code...
If Not DataGridView1.SelectedRows.Count = 0 Then
i = DataGridView1.SelectedRows(0).Index
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
namebox.Text = Trim(DataGridView1.Rows(i).Cells(0).Value)
salarybox.Text = DataGridView1.Rows(i).Cells(1).Value
End If
End If
now if the cell has nothing in it then it will show the exception....
like this...
Operator '<>' is not defined for type 'DBNull' and 'Nothing'.
this is code will be called when selected cell will be changed.
i trying to get the values of the selected cell and put that in in one text box.
You don't want to use the <> operator, you should us value IsNot Nothing to check if it IsNot Nothing or inversely Is Nothing to check if a value Is Nothing.
Also the reason is that there is no comparer for the DBNull and Nothing Types so if this is the case you will need to check to see for both. Something like
If value IsNot Nothing AndAlso value <> DBNull.Value Then
''#Do something
End If
Change
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
to
If Not DataGridView1.Rows(i).Cells(0).Value Is DBNull.Value Then