I have a check box that I binded with a dataset in its properties on my main form. Then on a separate form I have a datagrid that would be the options for each checkbox for a client. The datagrid is good when I look at it. but when I search for a client in the main form sometimes my checkboxes(some of them) will be selected for no reason. I have no coding for this as its all done in the properties except my search button.
Private Sub CmdSearch_Click(sender As Object, e As EventArgs) Handles CmdSearch.Click
Dim iReturn As String
Dim ClientFound As Boolean
Looking = True
ClientFound = False
Place = ClientBindingSource.Position
If Len(TxtSearch.Text) = 6 Then
ClientBindingSource.MoveFirst()
Do Until ClientFound = True
With ClientBindingSource
If Trim(TxtClient.Text) = TxtSearch.Text Then
If TxtSearch.Text = Trim(TxtClient.Text) Then
ClientFound = True
Else
ClientFound = False
End If
Place = ClientBindingSource.Position
Looking = False
Call LRSearch()
ClientBindingSource.Position = Place
Exit Sub
ElseIf Trim(TxtClient.Text) = "ZZTEST" Then
iReturn = MsgBox(TxtSearch.Text & " Not Found", vbOKOnly)
ClientBindingSource.MoveFirst()
Looking = False
Exit Sub
Else
.MoveNext()
End If
End With
Loop
Else
iReturn = MsgBox("Please check your client code for errors " & TxtSearch.Text, vbOKOnly)
TxtSearch.Text = ""
End If
Looking = False
End Sub
I am not sure why the check boxes on my main form have a mind of their own?
I have a program that writes txtboxes to a .txt file, I don't want to be able to "Generate Log Book Entry" until all text boxes are filled in and all check boxes are checked. I have the error provider to pop up correctly, but I need it implemented in the code so that it works the same way as the check boxes (All checkboxes must be checked before you're able to generate log book entry).
'Aircraft make/model textbox cannot be blank
If Me.TextBox4.Text = "" Then
ErrorProvider1.SetError(TextBox4, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
'N-number textbox cannot be blank
If Me.TextBox3.Text = "" Then
ErrorProvider1.SetError(TextBox3, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox3, "")
End If
'If all checkboxes are checked then no error is shown; if a single checkbox is not check, errorprovider shown
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked And CheckBox4.Checked And CheckBox5.Checked And CheckBox6.Checked And CheckBox7.Checked Then
ErrorProvider1.SetError(Button2, "")
Dim FILE_NAME As String = "C:\Users\Blake\Documents\test2.txt" 'Sends information to test2.txt
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write("Aircraft Make & Model: " & TextBox4.Text & ", N-number: " & TextBox3.Text)
objWriter.Write("
Gascolator Removed & Inspected in accordance with FAA approved maintenance manual")
objWriter.Write("
Signature _____________________________")
objWriter.Write(" " & DateTime.Now) 'Displays current date and time
objWriter.Close()
MsgBox("Text Written To File")
Else
MsgBox("File Does Not Exist")
End If
Else ErrorProvider1.SetError(Button2, "All boxes must be checked")
End If
I have tried moving
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
but it doesn't allow more than one "If" statement in that section of code.enter image description here
Doesn't allow text to be written to file bc/ txtboxes are not checked
Blank boxes able to write in .txt file
Entered info with .txtfile
Try restructuring your code like this
If Me.TextBox4.Text = "" Then
ErrorProvider1.SetError(TextBox4, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
If Me.TextBox3.Text = "" Then
ErrorProvider1.SetError(TextBox3, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox3, "")
'Add this line here
validateCheckBoxes()
End If
Sub validateCheckBoxes()
'Move this part here
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked And CheckBox4.Checked And CheckBox5.Checked And CheckBox6.Checked And CheckBox7.Checked Then
ErrorProvider1.SetError(Button2, "")
Dim FILE_NAME As String = "C:\Users\Blake\Documents\test2.txt" 'Sends information to test2.txt
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write("Aircraft Make & Model: " & TextBox4.Text & ", N-number: " & TextBox3.Text)
objWriter.Write("Gascolator Removed & Inspected in accordance with FAA approved maintenance manual")
objWriter.Write("Signature _____________________________")
objWriter.Write(" " & DateTime.Now)
'Displays current date and time
objWriter.Close()
MsgBox("Text Written To File")
Else
MsgBox("File Does Not Exist")
End If
Else
ErrorProvider1.SetError(Button2, "All boxes must be checked")
End If
End Sub
While the way you did this will eventually work, I would suggest another approach.
There is like a bazillion ways to do this. I would simply disable the "Generate Log" button until the user has checked and filled everything. This would prevent most problems you mentioned, but you wouldn't have the nice error messages I read through your code, so if they are important you won't be able to work this way.
Here's a code snippet that would do just what I just spoke about:
'Check if the user is checking boxes
Private Sub Checkboxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged, CheckBox5.CheckedChanged, CheckBox6.CheckedChanged, CheckBox7.CheckedChanged
UpdateGenerateLogButtonEnabled()
End Sub
'Check if the user is writing text
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
UpdateGenerateLogButtonEnabled()
End Sub
'Manage the GenerateLog button Enabled state
Private Sub UpdateGenerateLogButtonEnabled()
GenerateLogBookButton.Enabled = CheckBox1.Checked AndAlso CheckBox2.Checked AndAlso CheckBox3.Checked AndAlso CheckBox4.Checked AndAlso CheckBox5.Checked AndAlso CheckBox6.Checked AndAlso CheckBox7.Checked AndAlso Not TextBox1.Text = "" AndAlso Not TextBox2.Text = ""
End Sub
As I stated, there are many, many ways to validate this kind of code. If you need your error messages, you'll want a different one. Either way, have fun!
How can I pass a value from a DataGridVeiw to a RadioButton in VB.Net?
I have tried the following code but it is not working.
If DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("A") Then
RadioButton1.Checked = True
ElseIf DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("B") Then
RadioButton2.Checked = True
End If
Check if your database is with "A" spaces, for example it can be like "A ":
If DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("A ")
Then RadioButton1.Checked = True
ElseIf DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("B ")
Then RadioButton2.Checked = True End If
I am doing calculator and I wrote codes if user doesn't enter value to num1 and num2 it will give message to the screen and I did it with if-else. It is working for num1 and operator but not num2. How can I fix it?
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
ElseIf TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
End If
Your code should be something like:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
End If
' Seperate the conditions because TextBox1 and TextBox2 can both be empty, so an ElseIf will not handle it
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
End If
Else ' Neither of them (so TextBox1 and TextBox2 won't be empty)
Label1.ForeColor = SystemColors.ControlText
End If
This will popup a message when the user leaves TextBox1 empty or TextBox2 empty, if he doesn't leave any of the two empty, the Else will kick in.
For more informations about conditions in VB.NET, take a look here: https://www.dotnetperls.com/if-vbnet
I've made a button1 that if you click on it it starts the timer. In timer_tick I typed Label7.Text = "L O A D I N G". I've made also Exit (Button2). I want to do a code that if Label7.Text = "L O A D I N G" then the msgboxresult.no in button2 will be Button1.Enabled = True . Here is video how I want to do that: http://www.youtube.com/watch?v=4URYhd0xA8I&feature=youtu.be
Here is the code for Button2 (Exit)
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to exit?" ' Define message for exit."
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Question Or MsgBoxStyle.YesNo
title = "Exit"
' Display message.
response = MsgBox(msg, style, title)
If response = MsgBoxResult.No Then
If Label7.Text = "L O A D I N G" Then
Button1.Enabled = True
Button5.Enabled = False
TextBox1.Enabled = True
End If
End If
If response = MsgBoxResult.Yes Then
Me.Close()
End If
I could just simply do:
If response = MsgBoxResult.No Then
Button1.Enabled = True
but it is important that label7 is also mentioned, because label7 shows up when timer1 starts. Please help I don't know what is happening.