messagebox appears twice when using event ListView_ItemSelectionChanged - vb.net

I used a listview (to simplify i added one item) and when i click it a messagebox is shown.
when i close the messagebox and click again the item the messagebox appears twice i must to close it twice
Private Sub ListView1_ItemSelectionChanged(sender As Object, e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
Select Case e.ItemIndex
Case 0
MessageBox.Show("AAAA")
End Select
End Sub
Thanks

The SelectionChanged event fires when an item is selected or deselected. If you only care about when an item is selected, try checking if the item is selected first.
Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
If Not e.IsSelected Then Exit Sub
Select Case e.ItemIndex
Case 0
MessageBox.Show("AAAA")
End Select
End Sub

Related

Unselect a listbox item if it contains a specific character (don't allow its selection)

I have a listbox1 with a list of items. If a user has to select an item with a single mouse click and if that selected list item has an equal sign in it, it must immediately display a message stating this and deselect the item.
I have looked at various solutions all of which does not work as I want it to:-
Below is my code which works ummmm most of the time but not when two or more items have already been previously selected. I need a always will work solution.
Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick
For I = 0 To ListBox1.SelectedItems.Count - 1
If Not ListBox1.Items(I).ToString.Contains("=") Then
ListBox1.SetSelected(I, False)
' MsgBox("Please only select items that have = in description ! ! ! Edit item if you want to include . . .", MsgBoxStyle.Critical)
End If
Next
ListBox1.Refresh()
End Sub
I think it's better using SelectedIndexChangedEvent:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
With CType(sender, ListBox)
For i As Integer = .SelectedItems.Count - 1 To 0 Step -1
If Not IsNothing(.SelectedItems(i)) AndAlso Not .SelectedItems(i).ToString.Contains("=") Then
MsgBox("Invalid selection.")
.SelectedItems.Remove(.SelectedItems(i))
End If
Next i
End With
End Sub
One solution for this problem is to get - in the SelectedIndexChanged event - the indices of the invalid selections if any to deselect them and show the message. Handling the SelectedIndexChanged event in particular is to make it work with the mouse and keyboard inputs.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles ListBox1.SelectedIndexChanged
With ListBox1
Dim invalidSel = .SelectedIndices.Cast(Of Integer).
Where(Function(i) Not .GetItemText(.Items(i)).Contains("="))
If invalidSel.Any() Then
RemoveHandler ListBox1.SelectedIndexChanged,
AddressOf ListBox1_SelectedIndexChanged
For Each i In invalidSel
.SetSelected(i, False)
Next
MessageBox.Show("Please....")
AddHandler ListBox1.SelectedIndexChanged,
AddressOf ListBox1_SelectedIndexChanged
End If
End With
End Sub
Note, removing then adding the handler again is to avoid showing the message box repeatedly in the multi-selection modes. Without it, the event fires for each .SetSelected(i, False) call.

How to update a form based on a checkbox status

I'm currently trying to code some macros for a software (Revit), and I ran into an issue I do not know how to solve.
So I have a windows form with two checkboxes and a list of elements, and I would like that list of elements to be updated depending on what the status of the checkboxes is.
Here's my checkbox status' code:
If StructcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.EngineeringPlan
vpList.Add(tmpVP)
End Select
End If
If LegcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.Legend
vpList.Add(tmpVP)
End Select
End If
Now the problem with that code is that it only checks the initial status of the checkboxes, but do not update the list when the checkboxes are checked/unchecked.
How to make it so that the list VpList is updated everytime a checkbox status is changed?
Thanks!
The key here is to add a sub that does the checkboxes checking. You will need that sub because it will called multiple times depending on the user's actions.
Because you are not providing any insights about tmpView and vpList I will stick with your code, however you should note that depending on what exactly are you trying to do your code can be simplified or rewritten to be a bit more efficient. For example you don't specify if you want the tmpVP value to be unique in the list or to be more than one times, I assume that you want to be unique, so here is the sub's code (please read the comments in the code):
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Now that you have the sub you can call it whenever you want. "Whenever you want" means on various user's actions, like checking or un-checking a checkbox and in various other places like form's initialization (loading) these are events. According to your question you need to call it from 3 different events.
1.When the form is loading in order to get the initial status:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
2.When the user changes the StructcheckBox status:
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
3.When the user changes the LegcheckBox status:
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Here is the full form's code:
Public Class Form1
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
End Class
Hope this helps.

Define individual events with sub handles

I have a sub routine that handles multiple labels being clicked on.
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards()
End Sub
While this works, the problem is that I want each individual label to have a slightly different action each time, such as
checkCards(labelClicked)
I want each label to be able to be clicked on and call the same checkCards() sub, however with slightly different values of the parentheses. How would I go about this?
I am somewhat new to Visual Basic, so please try to keep the answer basic.
The sender parameter of your event is the clicked Label:
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards(sender)
End Sub
So if you click on the Label1 the sender parameter contains the Label1 object.
So you can implement the sub checkCards like the following:
Private Sub checkCards(ByVal labelClicked As Label)
Select Case labelClicked.Name
Case Label1.Name
'do some stuff if Label1 was clicked.
Case Label2.Name
'do some stuff if Label2 was clicked.
Case Label3.Name, Label4.Name
'do some stuff if Label3 or Label4 was clicked.
Case Else
'do some stuff if none of the above labels was clicked.
End Select
End Sub

ComboBox selectedIndexchange event does not fire

My combobox that has 2 item. This box has a selecteindexchange event. How do I get this event to fire off again if the user chooses the same item the second time around?
This seems to work in your case
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
End Sub
At this point the ComboBox doesn't have a selected text so use ComboBox1.SelectedItem which is populated at this point.

How to use IF AND statement with events

I want to do something if both a Listbox Item is selected and a Button is clicked. What I'm thinking is like this but it clearly isn't correct.
If ListBox1.SelectedIndex >= 0 And btnConvert_click Then
(This is under the ListBox1_SelectedIndexChanged Private Sub)
You would need to do something like this
Private sub btnConvert_click (sender as Object, e as EventArgs) Handles btnConvert.Click
If Combo.SelectedIndex > -1 Then ' SelectedIndex= -1 --> nothing selected
' Do your code
End If
End Sub
And in your case you wouldn't use Combo_Selected. This is if you want select the item and click the button. Remember, first item has index "0".