How to use IF AND statement with events - vb.net

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".

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.

messagebox appears twice when using event ListView_ItemSelectionChanged

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

Avoid some functions/events to run when one function/events is clicked or executed

im developing a system using vb.net and i have some question about datagridview function. Is it possible in gridview that when i click the rowheader function it will not execute the cell enter function of it?
Because my problem is in my cell enter event/function there is a code that will show some text box if the user enter on the 1st cell. and i want that when i click the row header of my grid view this cell enter event/function will not be executed. is it possible guys? give me some tips or tricks on how to do it.
i've also done trying like this
Private sub gridview_RowHeaderMouseClick . . .
textbox.visible = false
gridview.endEdit(true)
end sub
the result is okay but it is not good for the client view and i want to improve it more.
This is my code.
Private Sub dgCharges_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgCharges.CellEnter
if e.ColumnIdex >= 0 Then
if e.ColumnIndex = 5 Then
'Show Textbox.
End If
End if
End Sub
Private Sub dgCharges_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgCharges.RowHeaderMouseClick
'Textbox.visible = false //this line i've use to hide the textbox when user
clicked row header.
dgCharges.EndEdit(True)
End Sub
Just put a test in the CellEnter function that only executes the restricted part of the code if the column index >= 0. The header has index -1
Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If e.ColumnIndex >= 0 Then
'Do my stuff
End If
End Sub
------ EDIT ----
To prevent the datagridview cell editors from popping up just set the column to readonly. Alternatively for more control use
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridViewMsg.CellBeginEdit
'Test your criteria
If shouldBeReadonly Then
e.Cancel = True
End If
End Sub

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.
If anyone woyuld like to know I am ussing VB.net
How can I do this?
If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.
Would look like this.
Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged
For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows
If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then
row.Visible = False
End If
Next
End Sub
If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.
Would look like this.
Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim dg As DataGridView = sender
If dg.Columns.Count > 0 And e.RowIndex <> 0 Then
Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)
If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then
theRow.Visible = False
End If
End If
End Sub

Double-click DataGridView row?

I am using vb.net and DataGridView on a winform.
When a user double-clicks on a row I want to do something with this row. But how can I know whether user clicked on a row or just anywhere in the grid? If I use DataGridView.CurrentRow then if a row is selected and user clicked anywhere on the grid the current row will show the selected and not where the user clicked (which in this case would be not on a row and I would want to ignore it).
Try the CellMouseDoubleClick event...
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
Dim selectedRow = DataGridView1.Rows(e.RowIndex)
End If
End Sub
This will only fire if the user is actually over a cell in the grid. The If check filters out double clicks on the row selectors and headers.
Use Datagridview DoubleClick Evenet and then Datagrdiview1.selectedrows[0].cell["CellName"] to get value and process.
Below example shows clients record upon double click on selected row.
private void dgvClientsUsage_DoubleClick(object sender, EventArgs e)
{
if (dgvClientsUsage.SelectedRows.Count < 1)
{
MessageBox.Show("Please select a client");
return;
}
else
{
string clientName = dgvClientsUsage.SelectedRows[0].Cells["ClientName"].Value.ToString();
// show selected client Details
ClientDetails clients = new ClientDetails(clientName);
clients.ShowDialog();
}
}
Use DataGridView.HitTest in the double-click handler to find out where the click happened.
I would use the DoubleClick event of the DataGridView. This will at least only fire when the user double clicks in the data grid - you can use the MousePosition to determine what row (if any) the user double clicked on.
You could try something like this.
Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
For index As Integer = 0 To DataGridView1.Rows.Count
If DataGridView1.Rows(index).Selected = True Then
'it is selected
Else
'is is not selected
End If
Next
End Sub
Keep in mind i could not test this because i diddent have any data to populate my DataGridView.
You can try this:
Private Sub grdview_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdview.CellDoubleClick
For index As Integer = 0 To grdview.Rows.Count - 1
If e.RowIndex = index AndAlso e.ColumnIndex = 1 AndAlso grdview.Rows(index).Cells(1).Value = "" Then
MsgBox("Double Click Message")
End If
Next
End Sub