Here is how it will present information when form loads, what I want to do is when I press the button (without select any checkbox) to show me error there isn't selected any rows.
And if I click on the first checkbox and click the button to show me message (Okay you selected ID=% row)
so in my User Control I put Tag = Number from database for each control created
In button "Done" I add this (in Form)
Private Sub btnTaskDone_Click(sender As Object, e As EventArgs) Handles btnTaskDone.Click
If FlowLayoutPanel3.Controls.OfType(Of UserTasks)().Any(Function(cb) cb.CheckBox1.Checked = False) Then
'At least one CheckBox in myGroupBox is checked.
MsgBox("Checkbox Selected.", MsgBoxStyle.OkOnly, "Yeyy!")
Else
MsgBox("No Checkbox Selected.", MsgBoxStyle.OkOnly, "Error!")
End If
End Sub
and here is my call from DB to fill the user control:
Private Sub GenerateTasksUser()
FlowLayoutPanel3.Controls.Clear()
Dim dt As DataTable = New ClassBLL().GetTasksUser()
If dt IsNot Nothing Then
If dt.Rows.Count > 0 Then
Dim listItems As UserTasks() = New UserTasks(dt.Rows.Count - 1) {}
For i As Integer = 0 To 1 - 1
For Each row As DataRow In dt.Rows
Dim listItem As New UserTasks()
listItems(i) = listItem
listItems(i).Width = FlowLayoutPanel3.Width - 24
listItems(i).TaskTitle = row("taskstitle").ToString()
listItems(i).TaskSubject = row("tasksubject").ToString()
listItems(i).TaskFrom = row("taskfromname").ToString()
listItems(i).Tag = row("ID").ToString()
listItems(i).CheckBoxText = row("taskstatus").ToString
If listItems(i).CheckBoxText = "Completed" Then
listItems(i).CheckBoxCheck = True
Else
listItems(i).CheckBoxCheck = False
End If
FlowLayoutPanel3.Controls.Add(listItems(i))
Next
Next
End If
End If
End Sub
And now I need that when I select some row that has checkbox and click button to find the record ID row and update the database:
Private Sub btnTaskDone_Click(sender As Object, e As EventArgs) Handles btnTaskDone.Click
If FlowLayoutPanel3.Controls.OfType(Of UserTasks)().Any(Function(cb) cb.CheckBox1.Checked = False) Then
'At least one CheckBox in myGroupBox is checked.
MsgBox("Checkbox Selected.", MsgBoxStyle.OkOnly, "Yeyy!")
UpdateRecordDB(the id of the checkbox user control to update table)
Else
MsgBox("No Checkbox Selected.", MsgBoxStyle.OkOnly, "Error!")
End If
End Sub
Related
I have 2 Forms in a Product Registration Project
The 1st form has 3 buttons: New | Consult | Change | - that call the 2nd Form where I have a Photo Button.
New Button:
Private Sub tsbNew_Click(sender As Object, e As EventArgs) Handles tsbNew.Click
Try
Using frm As New frm2ndForm
frm.txtPrdCod.Enabled = True
frm.txtPrdCod.Text = ""
frm.ShowDialog()
End Using
tsbRefresh.PerformClick()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Consult Button:
Private Sub tsbConsult_Click(sender As Object, e As EventArgs) Handles tsbConsult.Click
Try
If DGProds.CurrentRow Is Nothing Then
MessageBox.Show("Select one product")
Exit Sub
End If
Using frm As New frm2ndForm
frm.txtPrdCod.Enabled = False
frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim 'dr.Item("prdCod")
frm.txtDes.Enabled = False
frm.txtDesRed.Enabled = False
frm.ShowDialog()
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Exit Sub
End Try
End Sub
Change Button
Private Sub tsbChange_Click(sender As Object, e As EventArgs) Handles tsbChange.Click
Try
If DGProds.CurrentRow Is Nothing Then
MessageBox.Show("Select one product")
Exit Sub
End If
Using frm As New frm2ndForm
frm.txtPrdCod.Enabled = False
frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim 'dr.Item("prdCod")
frm.ShowDialog()
End Using
tsbRefresh.PerformClick()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
In the 2nd Form, the Photo button will have two different behaviors:
when the user has clicked on the "New" button on Form 1, the code will open a search screen for the user to select an image in a folder on the Photos in server and show it in the picturebox1 in Form 2;
when the user has clicked on the "Consult" or "Change" button on Form 1, the code will make a comparison between the prdCod field of the Products Table and the filename of the image in the Photos folder and, when found, will show the image in the picturebox1 in Form 2.
If the "clicked button" on form 1 is "New", do the commands below:
Private Sub btnPhoto_Click(sender As Object, e As EventArgs) Handles btnPhoto.Click
Using open As New OpenFileDialog With {
.Title = "Select Photo",
.FileName = "",
.Filter = "Images PNG,JPEG,BMP,JPG|*.png;*.jpeg";*.bmp;*.jpg,
.Multiselect = False}
If open.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(open.FileName)
End If
End Using
End Sub
If the "clicked button" on form 1 is "Consult or Change", execute the commands below:
Private Sub btnPhoto_Click(sender As Object, e As EventArgs) Handles btnPhoto.Click
Dim IdProduto As String = prdCod ***field Products Table that will be used for the search in the Photos folder
If File.Exists("\\server\cmg\projects\Photos" & IdProduto) Then ***Search the image from the Photos folder on the server
PictureBox1.Image = Image.FromFile("\\server\cmg\projects\Photos" & IdProduto)
End If
End Sub
How do I check which button was clicked on the 1st form to be able to perform the correct Private Sub on the 2nd form?
It worked:
In the first form (frmConsProd), in the New Button code (tsbNew_Click), I include the parameter that will be sent to
the second form (frmCadProd):
Public Class frmConsProd
Private Sub tsbNew_Click(sender As Object, e As EventArgs) Handles tsbNew.Click
Try
Using frm As New frmCadProd("New")
frm.txtPrdCod.Enabled = True
frm.txtPrdCod.Text = ""
frm.ShowDialog()
End Using
tsbRefresh.PerformClick()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
In the second form (frmCadProd) I created a class variable (_operation) and the constructor (Public Sub New) to receive the sent parameter:
Public Class frmCadProd
Dim _operation As String
Public Sub New(operation As String)
InitializeComponente()
Select Case operation.ToLower()
Case "new"
_operation = operation
Case Else
MessageBox.Show("Invalid option!")
Close()
End Select
End Sub
End Class
Thanks to Messrs. #F0r3v3r-A-N00b, #jmcilhinney e #user09938, for the help.
I made a flashcard application where the user can edit the difficulty of each flashcard.
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then
txtFront.Text = dt.Rows(index)(2).ToString()
txtBack.Visible = False
txtBack.Text = dt.Rows(index)(3).ToString()
Else
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub
This button selects all the flashcards where difficulty is equal to 3 but if there are no records than the system produces an error. So how would I get it so the system produces a message if there are no records with that difficulty?
Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
Dim sqlstring As String = "select * from flashcards where Difficulty = 3" 'Select from flashcard table where difficulty = 3
dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
dt.Clear() 'Clears datatable
dataadapter.Fill(dt) 'Fills datatable
If dt.Rows.Count = 0 Then 'If record is not found in the database
MsgBox("There are no more flashcards inside this deck")
Exit Sub 'Code continus running if record is found
End If
Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
If txtBack.Visible = True Then 'If the back of the flashcard is shown
txtFront.Text = dt.Rows(index)(2).ToString() 'Displays a random record in the third column (front of flashcard)
txtBack.Visible = False 'Does not display the back of the flashcard
txtBack.Text = dt.Rows(index)(3).ToString() 'Displays a random record in the fourth column ()
Else 'If the user has not pressed the reveal button before
MsgBox("Please first reveal the back of the flashcard")
End If
End Sub
I have added a checkbox list with some names and a select all option. I am able to select all check boxes in checkbox list when I select (Select all) option.
Problem here is, I am not able to uncheck (Select All) option whenever I uncheck any of the options from check box list.
Below is the vb.net code attached for (Select All) functionality.
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If e.Index = 0 Then
Dim newCheckedState As Integer = e.NewValue
For i As Integer = 1 to ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, newCheckedState)
Next
End If
End Sub
Below is an image of the checkbox list on the windows form, for your reference.
.
In general i would use a Boolean variable to avoid that this handler is called for every item that you change programmatically. This will also fix the problem that you cannot uncheck the first item:
Private updatingListProgramatically As Boolean = False
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If updatingListProgramatically Then Return
If e.Index = 0 Then
updatingListProgramatically = True
For i As Integer = 1 To ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, e.NewValue)
Next
Else
Dim checked As Boolean = e.NewValue = CheckState.Checked
If Not checked Then
updatingListProgramatically = True
Me.ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Unchecked)
End If
End If
updatingListProgramatically = False
End Sub
The Else block seems to be what you are asking for.
Try adding an Else for the first 'If' condition and add a second 'If' statement to check if "Select All" Check box is Unchecked or not.
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If e.Index = 0 Then
If ChkLB_dl_name_cb.GetItemCheckState(0) = CheckState.Unchecked Then
Dim newCheckedState As Integer = e.NewValue
For i As Integer = 1 To ChkLB_dl_name_cb.Items.Count - 1
ChkLB_dl_name_cb.SetItemCheckState(i, newCheckedState)
Next
End If
Else
ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Unchecked)
End If
End Sub
Like following you should get the desired functionality:
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If e.Index = 0 Then
Dim newCheckedState As Integer = e.NewValue
For i As Integer = 1 To ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, newCheckedState)
Next
Else
'Remove the event-handler to prevent the ItemCheck-method from being called again
RemoveHandler ChkLB_dl_name_cb.ItemCheck, AddressOf ChkLB_dl_name_cb_ItemCheck
'If the item is being checked and all items are checked
'(except "Select All"), then check "Select All"
If e.NewValue = CheckState.Checked AndAlso
ChkLB_dl_name_cb.CheckedItems.Count + 1 = ChkLB_dl_name_cb.Items.Count - 1 Then
'Check "Select All"
ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Checked)
Else
'Uncheck "Select All"
ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Unchecked)
End If
'Re-Add the handler
AddHandler ChkLB_dl_name_cb.ItemCheck, AddressOf ChkLB_dl_name_cb_ItemCheck
End If
End Sub
Please help me with this!
I have a ListView with checkboxes enabled. I need to disable all the checked items checkboxes, where the user should not try to click it again.
Here is my code, where I am getting error.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Try
' submit
Dim path As String = "C:\Users\jtb43661\Documents\Visual Studio 2017\Projects\IGI Event Tracker\IGI Event Tracker\bin\Debug\Logs\Event.LOG"
If Not File.Exists(path) Then
Using sw As StreamWriter = File.CreateText(path)
End Using
End If
Using sw As StreamWriter = File.AppendText(path)
For Each item In ListView1.CheckedItems
sw.WriteLine(item.Text & "->" & " Completed-#---> " & Label2.Text)
item.SubItems.Add("Completed")
item.BackColor = Color.GreenYellow
'If item.subItems.text = "Completed" Then
'here I need to disable or lock the checked checkboxes
'End If
Next
sw.Close()
End Using
MsgBox("Events Submitted Successfully")
Catch ex As Exception
MsgBox(ex.Message.ToString)
Finally
End Try
End Sub
If I understand your implied logic correctly, once a ListviewItem is checked and contains a ListViewSubItem with the Text property equal to "Completed" you do not want the user to be able to uncheck that item. The addition of the "Completed" subitem is performed in a Button click event handler something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each itm As ListViewItem In ListView1.CheckedItems
' add "Completed" subitem only if it does not currently exist
Dim hasCompleted As Boolean = False
For Each subitem As ListViewItem.ListViewSubItem In itm.SubItems
hasCompleted = subitem.Text.Equals("Completed")
If hasCompleted Then Exit For
Next
If Not hasCompleted Then itm.SubItems.Add("Completed")
Next
End Sub
As far as I know, there is no way to directly disable a ListViewItem to prevent it from being unchecked. However, the ListView does have the ItemCheck event that can be used to prevent changing the "Checked" state. The following code prevents the Checked state change if the item being "UnChecked" has a SubItem with the Text "Completed".
Private Sub ListView1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ListView1.ItemCheck
If e.CurrentValue = CheckState.Checked Then
Dim item As ListViewItem = ListView1.Items(e.Index)
For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
If subitem.Text.Equals("Completed") Then
e.NewValue = e.CurrentValue ' do not allow the change
Exit For
End If
Next
End If
End Sub
I have a datagrid on a form with a checkbox to select all records. Column (0) of my datagrid is a checkbox column, and on the header I put a checkbox to select all records and display the number of records selected. Here is my code:
Private Sub chkSelectRecords_CheckedChanged(sender As Object, e As EventArgs) Handles chkSelectRecords.CheckedChanged
'Procedure runs when the checkSelectRecords is clicked.
'The loop selects all records on the datagrid when checked
'and clears selection when unchecked.
Dim chkRow As Integer = 0
If chkSelectRecords.Checked = True Then
For Each row As DataGridViewRow In grdDeleteRecord.Rows
row.Cells(0).Value = (row.Cells(0).Value IsNot DBNull.Value)
chkRow += 1
lblRowCount.Visible = True
lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
Next
Else
For Each row As DataGridViewRow In grdDeleteRecord.Rows
row.Cells(0).Value = (row.Cells(0).Value Is DBNull.Value)
chkRow += 0
lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
Next
End If
End Sub
I placed another procedure when the user selects one record at a time:
Private Sub grdDeleteRecord_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellContentClick
' Shows how to get count of checked or unchecked checkbox column count along with
' how to get column data back for checked items.
Dim chkRowCount As Integer = 0
grdDeleteRecord.EndEdit()
For Each row As DataGridViewRow In grdDeleteRecord.Rows
If row.Cells(0).Value = True Then
chkRowCount += 1
Else
chkRowCount += 0
lblRowCount.Visible = True
lblRowCount.Text = chkRowCount.ToString & " Record(s) Selected"
End If
Next
End Sub
Both procedures work as needed. The problem is when I select all records my label does not update the number of records that are now selected. I am sure this is happening because they are two separate events, I just don't know how to link the events or write a procedure that accomplishes all three tasks
Here's a function I wrote for you. You must use the CellValueChangedEvent and the CurrentCellDirtyStateChanged events...
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblRowCount.Text = SetLabel()
End Sub
Private Function SetLabel() As String
Dim strRows As String = String.Empty
Dim intRows As Integer = 0
For i As Integer = 0 To grdDeleteRecord.Rows.Count - 1
If CBool(grdDeleteRecord(0, i).Value) Then
intRows += 1
End If
Next
If intRows > 0 Then
strRows = intRows.ToString & " Record(s) Selected"
lblRowCount.Visible = True
End If
Return strRows
End Function
Private Sub grdDeleteRecord_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellValueChanged
lblRowCount.Text = SetLabel()
End Sub
Private Sub grdDeleteRecord_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles grdDeleteRecord.CurrentCellDirtyStateChanged
If grdDeleteRecord.IsCurrentCellDirty Then
grdDeleteRecord.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
When you check or uncheck these checkboxes it will trigger this event and thus update your label. Then you can use this where ever you need it to be used.