So I'm just curious and trying to improve my current application. Couldn't really find any examples of this.
I've got a bunch of checkboxes cbcin my form and I want to do something if none of them is checked.
Is there any better ways to do this?
If Not cbc1.Checked = True Then
If Not cbc2.Checked = True Then
If Not cbc3.Checked = True Then
If Not cbc4.Checked = True Then
If Not cbc5.Checked = True Then
If Not cbc6.Checked = True Then
If Not cbc7.Checked = True Then
If Not cbc8.Checked = True Then
If Not cbc9.Checked = True Then
'Do this and that
End If
End If
End If
End If
End If
End If
End If
End If
End If
I find this a bit messy and its probably not a good way to write a good code.
Why not use .All() from LINQ? It's cool (and readable)
Dim checkBoxes As New List(Of CheckBox) From {cbc1,cbc2,cbc3} 'all your check boxes
If checkBoxes.All(Function(cb) Not cb.Checked) Then
'Do this and that
End If
Also you don't need to do if boolObj = True Then. Just if boolObj Then is fine.
Edit: The condition was the other way round. Fixed now.
Since you aren't doing anything, except in your last If statement, why not use AND operator and combine all the boolean checks ?
That way you will only have one if then end if loop.
Related
I am trying to automate a scenario using silk test and am very new to coding using vb.net. Basically i have a checkbox identified and want to set it to either true or false based on a string value being passed.
for e.g.
Dim tfnSigned As String
tfnSigned = "Yes"
If tfnSigned = "Yes"
Then .CheckBox("SED_TFNSignedCheckBox").Check
End If
In this case, i get a compiler error as .CheckBox is not identified as a class and hence cannot use the Check method
Kindly help
Cheers
Checkboxes accepts only Boolean values which is either True or False.
The syntax is pretty much straightforward and can be easily found on Google
Here I assume that SED_TFNSignedCheckBox is the name of your checkbox control.
Dim tfnSigned As String
tfnSigned = "Yes"
If tfnSigned = "Yes" Then
SED_TFNSignedCheckBox.Checked = True
End If
To use the .Checkbox() method you need to be in the correct context, i.e. it should appear in a With..End With statement. the best way to get the syntax correct is to use the Silk Test Recorder to record a checking the checkbox, this will generate something like this.
With _desktop.Dialog("locator of dialog")
.CheckBox("SED_TFNSignedCheckBox").Check
End With
So your complete code would look something like this ...
Dim tfnSigned As String
tfnSigned = "Yes"
If tfnSigned = "Yes" Then
With _desktop.Dialog("locator of dialog")
.CheckBox("SED_TFNSignedCheckBox").Check
End With
End If
Hope that helps.
Sorry for the messy code :S
If CheckBox2.Checked = True Then
For i As Integer = 0 To 1 Step 0
If CheckBox1.Checked = True Then
If TextBox1.Text = lblCLickLImit.Text Then
Timer1.Stop()
TextBox1.Text = "0"
System.Windows.Forms.SendKeys.Send("{F5}")
System.Threading.Thread.Sleep(delaydelaytime)
System.Windows.Forms.SendKeys.Send("{ENTER}")
Else
If CheckBox1.Checked = False Then
If TextBox1.Text = lblCLickLImit.Text Then
Timer1.Stop()
TextBox1.Text = "0"
End If
End If
End If
Else
If CheckBox2.Checked = False Then
If CheckBox1.Checked Then
If TextBox1.Text = lblCLickLImit.Text Then
Timer1.Stop()
TextBox1.Text = "0"
System.Windows.Forms.SendKeys.Send("{F5}")
System.Threading.Thread.Sleep(delaydelaytime)
System.Windows.Forms.SendKeys.Send("{ENTER}")
End If
Else
If CheckBox1.Checked = False Then
If TextBox1.Text = lblCLickLImit.Text Then
Timer1.Stop()
TextBox1.Text = "0"
End If
End If
End If
End If
End If
Next
Basically this code is for an Auto Clicker program,(Hopefully this will help you understand, http://prntscr.com/7tuc3o interface) Ok so when the "Continuous" checkbox is selected the code is in theory supposed to loop for infinity. However when I run the program with everything selected as shown all that happens is the program clicks once and then crashes (not responding). Any help I have tried this loop in other programs and it works, just not with this code.
Your loop is tying up the UI thread. You'll need to look into using either a background worker:
BackgroundWorker handles long-running tasks. It does not freeze the entire program as this task executes.
(dotnetperls.com)
Here is the msdn walkthrough of how to set-up a backgroundworker:
https://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx
Or
If this is a personal project and no one you love will need to maintain this code, you can use Application.DoEvents() to continue to pump messages while the program is looping. Here is the msdn documentation for that https://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx
First of all, a Step of 0 doesn't really make any sense in a for loop. It may /work/ but it is going to drive anyone reading it later insane. If you want an infinite loop don't use a for loop, use:
While True
'code here
End While
For loops are used when you know exactly how many iterations of your loop you need. A while loop is designed to iterate as long as some condition is true. In this example the condition is always true so it loops indefinitely.
Your code is also just going to spin the UI thread constantly. It never pauses for input (even your sleep calls do not release the thread for input). As far as the OS knows, your app has locked up because it never processes any of the window messages that get posted to it. It is still happily spinning away though, until windows finally gets tired of it and prompts you to kill it.
Not sure what other "programs" you're working with but I can tell you you don't want to use a For loop for this. You want a do/while loop, like either:
While True
...
End While
or
Do
...
Loop While True
My colleagues frown upon me because according to our sourcesafe, I added parentheses to the method-property thus resulting in a stackoverflow.
Since I'm sure I did no such thing on purpose, I wonder if somehow I used a obscure shortcut which performed these changes.
This kind of stuff :
Public Function Opslaan() As Boolean
If something_wrong() Then
opslaan = False
End If
If opslaan = False Then
Do_Something_Else()
End If
Return opslaan
End Function
and this got changed into :
Public Function Opslaan() As Boolean
If something_wrong() Then
opslaan = False
End If
If opslaan() = False Then '' See the parentheses here added automatically
Do_Something_Else()
end If
Return opslaan
End Function
Any help is appreciated.
This looks like bad old VB6 code converted to VB.NET.
The problem is VB6 allowed you to treat the current function name as a variable and update its value all throughout the method. Then, when you exited the method whatever value this variable had would be returned to the caller. This syntax is confusing and should never be used, even in VB6 since there is a better way.
Update all code you find like this to something like:
Public Function Opslaan() As Boolean
Dim result As Boolean = True
If something_wrong() Then
result = False
end if
If result = False Then
Do_Something_Else()
End If
Return result
End Function
This is much clearer code and will never mistakenly call the same routine.
Also, this is totally personal preference, but I prefer to not check booleans with equals statements. So If result = False Then becomes If Not result Then and If result = true Then becomes If result Then. That almost always feels cleaner to me for Booleans.
I am developing a quality control system for my company, and I i want to connect it to a label printer, which is already taken care off, the problem now is with the button itself.
I want the print label button to only be enabled and visible after the whole check was made, what i've got now is this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Button_label.Visible = False
Button_label.Enabled = False
End Sub
Private Sub Motor_OK_Change()
Dim ok As Boolean
ok = Motor_OK.Value
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
End Sub
It does work on conceiling the button, but it fails when trying to enable it again and making it visible upon making the check. It's important to refer that I have tried using If ok = True instead of If ok= 1, I dont know how important that is.
Thanks
You are not testing for both conditions (ok=0), perhaps the following will work:
Replace:
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
With:
Button_label.Visible = ok
Button_label.Enabled = ok
New Solution:
Instead of using the Form Before_Update event, use Form_Current event. If there is only 1 record, you can also use the Form_Load event. I would also suggest that you remove the Visible property and use only the Enabled one so users can see their is print functionality.
Private Sub Form_Current()
'button_Label.Visible = False
button_Label.Enabled = False
End Sub
Instead of using the Motor_OK Before_Updateevent, use After_Update event
Private Sub Motor_OK_AfterUpdate()
Dim ok As Boolean ' defaults to False
If IsNumeric(Motor_OK.Value) Then ok = True ' remove this statement if Motor_OM is
' alpha numeric.
If Not IsNull(Motor_OK.Value) Then ok = True ' otherwise following statement
' to avoid NULL error
'button_Label.Visible = ok
button_Label.Enabled = ok
End Sub
Let me know if I can be of further help.
How to get for null value in datagridview i did this in the cell validation event but it dosent seem to work. I want the user to add a new row and someone force him to give an ID or delete the row. What am i doing wrong. This is not a how to do question. This is a what is wrong Question. So right now it detects null but once i corrected the cell it still dosent allow me to get out of the row.
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
ElseIf Not IsDBNull(DataGrid.CurrentCell.Value) Then
e.cancel = False
End If
End If
So i tried this and it works for me . it simply uses e.formatedvalue. Current Cell Value is the cell value before and after edit while formatedvalue is what is being typed it . i guess i understand now so here is the coding
If grdDataGrid.CurrentCell.ColumnIndex = 2 Then
If e.FormattedValue = "" Or IsDBNull(e.FormattedValue) Then
MsgBox("Cannot be Null")
e.Cancel = True
Exit Sub
End If
End If
They are also different ways like adjusting your column properties to not allowed null but since the column properties inherit from the database i decided to use this.
I think this should be works ...
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
exit sub
End If
End If
Another way around this would be to explicitly set the default null value for the new datagrid cell to the value you are checking for. For instance,set the null= to an empty string if you are pulling string values. You can use the properties palette to set this.