delete selected entry in CheckedListBox - vb.net

Good day!
so delete all the ticked entries from CheckedListBox
For Each item In CheckedListBox1.CheckedItems.OfType(Of String)().ToList()
CheckedListBox1.Items.Remove(item)
Next item
Tell me how to delete the selected entry in the CheckedListBox?
in the picture the record with the name 3 is highlighted. Is it possible and how to delete it without putting a check?

Here is a simple example for
Removing all checked items
Removing the current item disregarding the checked state.
Form code
Imports System.Globalization
Public Class Form1
Private Sub RemoveCheckedButton_Click(sender As Object, e As EventArgs) Handles RemoveCheckedButton.Click
If MonthCheckedListBox.CheckedItems.Count > 0 Then
MonthCheckedListBox.CheckedItems.Cast(Of String)().ToList().
ForEach(Sub(item) MonthCheckedListBox.Items.Remove(item))
SetActive()
End If
End Sub
Private Sub RemoveCurrentButton_Click(sender As Object, e As EventArgs) Handles RemoveCurrentButton.Click
If MonthCheckedListBox.Items.Count > 0 Then
MonthCheckedListBox.Items.RemoveAt(MonthCheckedListBox.SelectedIndex)
SetActive()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MonthCheckedListBox.Items.AddRange(
Enumerable.Range(1, 12).
Select(Function(index)
Return DateTimeFormatInfo.CurrentInfo.GetMonthName(index)
End Function).ToArray())
SetActive()
End Sub
Private Sub SetActive()
ActiveControl = MonthCheckedListBox
If MonthCheckedListBox.Items.Count > 0 Then
MonthCheckedListBox.SelectedIndex = 0
End If
End Sub
End Class

Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
If isDeleted = False Then
isDeleted = True
CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndex)
Else
isDeleted = False
End If
End Sub

Dim isDeleted As Boolean = False
Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
If isDeleted = False Then
isDeleted = True
CheckedListBox1.Items.RemoveAt(CheckedListBox1.SelectedIndex)
Else
isDeleted = False
End If
End Sub

Private Sub CheckedListBox1_Click(sender As Object, e As EventArgs) Handles CheckedListBox1.Click
Dim i As String = ""
i = CheckedListBox1.SelectedItem
If i = Nothing Then
Exit Sub
Else
CheckedListBox1.Items.Remove(i)
End If
End Sub

For index As Integer = CheckedListBox1.Items.Count - 1 To 0 Step -1
If CheckedListBox1.GetItemChecked(index) Then
CheckedListBox1.Items.RemoveAt(index)
End If
Next

Related

Timer.tick within 1 min back-forth

I have 2 Form and i want it to be slide show back & forth within 1 minute.
Please write down the code thank you!
My Code:
Form 1
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE.Enabled = True
tmrSLIDE.Start()
Else
tmrSLIDE.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE.Tick
trs1.ShowSync(Form2)
Me.Hide()
End Sub
Form 2:
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE2.Enabled = True
tmrSLIDE2.Start()
Else
tmrSLIDE2.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE2.Tick
trs2.ShowSync(Form1)
Me.Hide()
End Sub
Property tmrSLIDE interval = 60000
property tmrSLIDE2 interval = 120000
I just Want to have 1 Checkbox with a simplified code thank you!

How to subtract checkbox values?

When I select a checkbox, the value gets added in the textbox and gets incremented when I check another. Problem is, when I uncheck them, it should subtract the values too. Total_sum in my textbox. Please help me.
Dim sum As Integer
Public Sub ChangeValue(add As Boolean, value As Decimal)
sum += If(add, value, -value)
Total_sum.Text = sum.ToString
End Sub
Private Sub chk_1_CheckedChanged(sender As Object, e As EventArgs) Handles chk_1.CheckedChanged
If chk_1.Checked = True Then
ChangeValue(DirectCast(sender, CheckBox).Checked = True, 25I)
End If
End Sub
Private Sub chk_2_CheckedChanged(sender As Object, e As EventArgs) Handles chk_2.CheckedChanged
If chk_2.Checked = True Then
ChangeValue(DirectCast(sender, CheckBox).Checked, 25I)
End If
End Sub
Private Sub chk_3_1_CheckedChanged(sender As Object, e As EventArgs) Handles chk_3_1.CheckedChanged
If chk_3_1.Checked = True Then
chk_3_2.Checked = False
ChangeValue(DirectCast(sender, CheckBox).Checked, 15I)
End If
End Sub
Private Sub chk_3_2_CheckedChanged(sender As Object, e As EventArgs) Handles chk_3_2.CheckedChanged
If chk_3_2.Checked = True Then
chk_3_1.Checked = False
ChangeValue(DirectCast(sender, CheckBox).Checked, 30I)
End If
End Sub
Private Sub chk_4_CheckedChanged(sender As Object, e As EventArgs) Handles chk_4.CheckedChanged
If chk_4.Checked = True Then
ChangeValue(DirectCast(sender, CheckBox).Checked, 20D)
End If
End Sub
Private Sub chk_5_1_CheckedChanged(sender As Object, e As EventArgs) Handles chk_5_1.CheckedChanged
If chk_5_1.Checked = True Then
chk_5_2.Checked = False
chk_5_3.Checked = False
ChangeValue(DirectCast(sender, CheckBox).Checked, 0D)
End If
End Sub
Private Sub chk_5_2_CheckedChanged(sender As Object, e As EventArgs) Handles chk_5_2.CheckedChanged
If chk_5_2.Checked = True Then
chk_5_1.Checked = False
chk_5_3.Checked = False
ChangeValue(DirectCast(sender, CheckBox).Checked, 15D)
End If
End Sub
You do not need the DirectCast. You know what the sender is. Just passing the Boolean chk_1.Checked to ChangeValue will either increment or decrement the sum.
Private sum As Integer
Public Sub ChangeValue(add As Boolean, value As Integer)
sum += If(add, value, -value)
Total_sum.Text = sum.ToString
End Sub
Private Sub chk_1_CheckedChanged(sender As Object, e As EventArgs) Handles chk_1.CheckedChanged
ChangeValue(chk_1.Checked, 25)
End Sub

Enable a counter at a specific time everyday

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

Datagridview - edit selected row?

I have set Datagridview .ReadOnly property to True, and then added a button column. Button column is meant for edit button when clicked, but I wish to edin only currently selected row. This is what I tried:
EDIT:
Public Class Form2
Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Me.DataGridView1.Height = 0.8 * Me.Height
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Users._USERS' table. You can move, or remove it, as needed.
Me.USERSTableAdapter.Fill(Me.Users._USERS)
Me.DataGridView1.DefaultCellStyle.Font = New Font("Arial", 7)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).ReadOnly = True
Next
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 3
If e.ColumnIndex = yourColumnIndex Then
If MsgBox("Do you wish to edit records?", vbQuestion + vbYesNo, "Edit records") = vbYes Then
DataGridView1.Rows(e.RowIndex).ReadOnly = False
End If
End If
End Sub
Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub
End Class
You can't set the grid's ReadOnly property to true. After the rows are added to the grid, you would have to loop through your rows and set the ReadOnly property for each row:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).ReadOnly = True
Next
End Sub
Note: you can't set these properties in the form's constructor, a quirk of the DataGridView control.
Then use the RowIndex property provided by the e parameter:
Private Sub DataGridView1_CellContentClick(sender As Object,
e As DataGridViewCellEventArgs)
Handles DataGridView1.CellContentClick
If e.ColumnIndex = 3 Then
DataGridView1.Rows(e.RowIndex).ReadOnly = False
End If
End Sub
Set it back to true when leaving the row:
Private Sub DataGridView1_RowLeave(sender As Object,
e As DataGridViewCellEventArgs)
Handles DataGridView1.RowLeave
DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub
This is working, I just changed your suggestion a little bit (DatagridView Readonly property must be set to False):
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = True
Next
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 3
If e.ColumnIndex = yourColumnIndex Then
If MsgBox("Do you wish to edit record?", vbQuestion + vbYesNo, "Edit record") = vbYes Then
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = False
Next
End If
End If
End Sub
Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = True
Next
End Sub
Thanks for all your help Lars!

Determine if listview multiple item checkbox is checked

I have a list view with check box. I want to make the button visible, if item in listView is checked. If there's no checked item visible = false.
Another quick example...
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
ListView1_ItemChecked(Nothing, Nothing)
End Sub
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
Button1.Visible = (ListView1.CheckedItems.Count > 0)
End Sub
Private Sub MOOElv_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles MOOElv.ItemChecked
Dim iCount As Integer
For i = 0 To MOOElv.Items.Count - 1
If MOOElv.Items(i).Checked = True Then
iCount = 1
End If
If iCount >= 1 Then
consumebtn.Visible = True
Else
consumebtn.Visible = False
End If
Next
End Sub
Thank you i got my own solution i just put a loop inside an event handler, then first determine the amount of items and if checked the button will visible :D