Determine if listview multiple item checkbox is checked - vb.net

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

Related

the codes shows a notif 'Index was out of range. Must be non-negative and less than the size of the collection

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Cursor = Cursors.AppStarting
Dim id As Integer
Dim fx As frmItemEntry
id = DataGridView1.SelectedRows(0).Cells("id").Value
fx = New frmItemEntry(id)
Button4.PerformClick()
fx.ShowDialog()
Cursor = Cursors.Default
End Sub
try this code from a blog but, am don't know where is wrong
Obviously no rows are selected. Therefore you cannot access row 0. The best way to deal with this situation is to disable the button when no row is selected.
To do this you must handle the SelectionChanged event
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles DataGridView1.SelectionChanged
Button2.Enabled = DataGridView1.SelectedRows.Count > 0
End Sub
Also, disable the button by default in the Form designer. This code will enable it whenever a selection is made.
Another possibility is to do something like this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim id As Integer
Dim fx As frmItemEntry
If DataGridView1.SelectedRows.Count > 0 Then
Cursor = Cursors.AppStarting
id = DataGridView1.SelectedRows(0).Cells("id").Value
fx = New frmItemEntry(id)
Button4.PerformClick()
fx.ShowDialog()
Cursor = Cursors.Default
Else
MessageBox.Show("Please select a row")
End If
End Sub

How can I make a button move up in size (by going vertically up), but not in total size?

I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it

Get selected value from DateTimePicker

I would like to get selected value from DateTimePicker in VB (If I select only day value then I would like to get only selected day value.)
In this image I have selected (blue marked) year value from this DateTimePicker. So I need only this year value.
In the case of TextBox I can get selected value using
TextEndTime.SelectedText
Is there any syntax or approach to get selected value from DateTimePicker?
As the DateTimePicker-control can be manipulated by using the arrow keys, you can use SendKeys to change the current selected value.
The following example gets the current DateTime-value of the DateTimePicker and, after sending the ↑ key, compares the value to the new value. At last it resets the DateTimePicker to the original value.
So the variable currSelected will contain the last Selection.
Dim currVal As DateTime
Dim newVal As DateTime
Dim valCheck As Boolean
Dim currSelected As Selection = Selection.None
Public Enum Selection
None = 0
Year = 1
Month = 2
Day = 3
End Enum
Private Sub CheckDTPSelection(dtp As DateTimePicker)
valCheck = True
currVal = dtp.Value
SendKeys.Send("{UP}")
End Sub
Sub RefreshSelection(dtp As DateTimePicker)
If valCheck Then
newVal = dtp.Value
If currVal.Year <> newVal.Year Then
currSelected = Selection.Year
ElseIf currVal.Month <> newVal.Month Then
currSelected = Selection.Month
ElseIf currVal.Day <> newVal.Day Then
currSelected = Selection.Day
End If
dtp.Value = currVal
valCheck = False
End If
End Sub
Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles MyDateTimePicker.DropDown
RemoveHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
End Sub
Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles MyDateTimePicker.CloseUp
AddHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_KeyUp(sender As Object, e As KeyEventArgs) Handles MyDateTimePicker.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
CheckDTPSelection(MyDateTimePicker)
End If
End Sub
Private Sub MyDateTimePicker_MouseUp(sender As Object, e As MouseEventArgs) Handles MyDateTimePicker.MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles MyDateTimePicker.ValueChanged
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
RefreshSelection(dtp)
End Sub
Private Sub Btn_WhatsSelected_Click(sender As Object, e As EventArgs) Handles Btn_WhatsSelected.Click
'Show the current selected value in a MessageBox
MessageBox.Show(currSelected.ToString())
End Sub
Hi Everyone and thanks for your tips that the DateTimePicker-control can be manipulated.
I had the selection problem with DateTimePicker , the currently selected item value could not send to a text box, as DTP works only valuechanged event. I spend 4 hours time to find the solution and wrote the following code:
Public MyEventCounter As Integer = 0
Private Sub DTPAcquDt_DropDown(sender As Object, e As EventArgs) Handles DTPAcquDt.DropDown
RemoveHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
End Sub
Private Sub dtpacqudt_closeup(sender As Object, e As EventArgs) Handles DTPAcquDt.CloseUp
AddHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
'Check the Mouse/Keys event counter
If MyEventCounter > 0 Then
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End If
End Sub
Private Sub DTPAcquDt_KeyUp(sender As Object, e As KeyEventArgs) Handles DTPAcquDt.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
MyEventCounter = MyEventCounter + 1
End If
End Sub
Private Sub DTPAcquDt_MouseUp(sender As Object, e As MouseEventArgs) Handles DTPAcquDt.MouseUp
MyEventCounter = MyEventCounter + 1
End Sub
Private Sub DTPAcquDt_ValueChanged(sender As Object, e As EventArgs) Handles DTPAcquDt.ValueChanged
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End Sub

Datagridview_cellvaluechanged only updates on second try

DirtyStateChanged lets me commit immediately when I click the checkbox
Private Sub dataGridView4_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView4.CurrentCellDirtyStateChanged
If DataGridView4.IsCurrentCellDirty Then
DataGridView4.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub DataGridView4_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView4.CellValueChanged
For c As Integer = 2 To DataGridView4.ColumnCount - 1
If DataGridView4.Rows(e.RowIndex).Cells(c).Value = True Then
'Disable this persons' row on datagridview5
Dim aName As String = DataGridView4.Rows(e.RowIndex).Cells(0).Value
For Each dRow As DataGridViewRow In DataGridView6.Rows
If dRow.Cells(0).FormattedValue = aName Then
dRow.ReadOnly = True
For Each cell As DataGridViewCell In dRow.Cells
dRow.Cells(cell.ColumnIndex).Style.BackColor = Color.LightGray
Next
'DataGridView6.Update()
End If
Next
End If
Next
End Sub
When I click a checkbox on datagridview3, it should update the row on datagridview5 with a new backcolor and readOnly.
What seems to happen is it does not seem to work the first time clicking the checkbox. It does work perfectly on the second try though.
How can I make it validate on the first try?

event handling in multiple checkedlistbox

i am new in vb.net and im thinking if there is a possible way to handle all checkedlistbox events inside a tabcontrol
here is my code for one checkedlistbox and i guess that it is inefficient to paste this to the rest of the 49 checkedlistboxes
Private Sub cbA1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbA1.SelectedIndexChanged
For i = 0 To cbA1.Items.Count - 1
cbA1.SetItemCheckState(i, CheckState.Unchecked)
Next
cbA1.SetItemCheckState(cbA1.SelectedIndex, CheckState.Checked)
'DISABLE CHKBOX AFTER PICKING THE CHOICE
cbA1.Enabled = False
End Sub
this is how I usually do to handle many objects with just 1 event.
Private Sub cb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbA1.SelectedIndexChanged, cbA2.SelectedIndexChanged, cbA3.SelectedIndexChanged 'and add more checkedlistbox here separated by comma
For i = 0 To sender.Items.Count - 1
sender.SetItemCheckState(i, CheckState.Unchecked)
Next
sender.SetItemCheckState(sender.SelectedIndex, CheckState.Checked)
'DISABLE CHKBOX AFTER PICKING THE CHOICE
sender.Enabled = False
End Sub
how to get the checked values in checkedlistbox, can be found here :
this code will get the index
For Each indexChecked In CheckedListBox1.CheckedIndices
MessageBox.Show("Index " + itemChecked.ToString() + " is checked.")
Next
this code will get the value
For Each itemChecked In CheckedListBox1.CheckedItems
MessageBox.Show(itemChecked.ToString() & " is checked")
Next
change 'CheckedListBox1' to 'sender' and put it inside Sub cb_SelectedIndexChanged
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
For i = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.SelectedIndex = i Then
CheckedListBox1.Items.RemoveAt(i)
End If
Next
End Sub