Retrieving the item right-clicked in a listbox for a context menu action - vb.net - vb.net

In my vb.net app, I've got a list box that contains a bunch of email addresses.
There's a context menu on the list box with view contact, modify and remove options.
I'm currently stuck on how to determine which item in listbox1.items the user has right-clicked for use in the context menu actions.... So say for example a user right clicks 'johnsmith#stackoverflow.com' and clicks remove I then need to say
listbox1.items.remove(THEITEMTHATWASRIGHTCLICKED)
But how would I determine THEITEMTHATWASRIGHTCLICKED?
I tried...
itemthatwasrightclicked = listbox1.SelectedIndex
But if I right click on an item before left-clicking, I get a returned index of -1. If I left click the item to select it first and then right click I get the correct index returned, so it seems that if a user right clicks without first left clicking, the item isn't selected as such.
I'm at a loss and any help is appreciated!
I feel like this should be something simple.
Thanks in advance! :)

The listbox class provides a method for this in the MSDN. You will want to use the IndexFromPoint(Point) method. When this method is called it returns the index for the item in the list box found at the coordinates of the Point specified. You will want to capture the coordinates of the right click event by implementing this within the MouseDown event of the ListBox.
In its most basic form, the code for this would be as follows.
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)
End If
End Sub

Related

How to hide a DataGridViewButtonCell

I have a DataGridViewButtonCell in my DataGridView and I wanted to set the property Visible to True.
I have tried:
DataGridView1.Rows("number of row i want").Cells("number of cell i want").Visible = True
Unfortunately it says that the property visible is read only.
Here is the code:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'does not work
DataGridView1.Rows(e.RowIndex).Cells(6).Visible = True
End Sub
Does anyone knows how I can achieve this?
Thanks.
There is no actual way to hide a DataGridViewButtonCell. Currently I can only see two options:
Use padding to move the button over as shown here. I will provide similar VB.NET code
Set the Cell to a DataGridViewTextBoxCell and set the ReadOnly property to True
Use Padding:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
Dim columnWidth As Integer = DataGridView1.Columns(e.ColumnIndex).Width
Dim newDataGridViewCellStyle As New DataGridViewCellStyle With {.Padding = New Padding(columnWidth + 1, 0, 0, 0)}
DataGridView1.Rows(e.RowIndex).Cells(6).Style = newDataGridViewCellStyle
End If
End Sub
Use DataGridViewTextBoxCell:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
Dim newDataGridViewCell As New DataGridViewTextBoxCell
DataGridView1.Rows(e.RowIndex).Cells(6) = newDataGridViewCell
newDataGridViewCell.ReadOnly = True
End If
End Sub
Both of these should give you the effect of not showing the button.
This is really a perspective issue. From a programmer’s perspective, simply ignoring the button clicks on the buttons I want to disable is very easy to do and takes just a few lines of code.
From a user perspective, this situation would play out like this… the user clicks what appears to be a valid enabled button, and nothing happens. The user did not write the code for this… so at best the user will think the computer is not responding to the button click or at the worst… would think your coding skills are dubious!
The same situation happens if the button is missing. The user is not going to know why it is missing… but will most likely come to the same conclusion described above with a non-working button.
In another very simple approach, let say that all the buttons are enabled and we have a list of the button indexes we want to disable. The users presses one of the buttons, we check the disabled button list and if the clicked button is one that is disabled, simply display a message box to indicate why this button is disabled. This approach says to the user… “Here are a bunch of buttons, guess which ones are enabled”…
The DataGridViewDisableButtonCell and DataGridViewDisableButtonColumn wrappers solve all of the above issues… the button is visible so the user wont question where the button went if you set it to invisible and it is greyed out. “Greyed out” is something most users understand and will relieve the user of having to “guess” which buttons are enabled.
You can create a wrapper for two classes: the DataGridViewButtonCell and the DataGridViewButtonColumn.
The link How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control to the MS example is one I have used before using C#, however there is a VB implementation at the link also.
Below is a picture of the result of using the two wrappers described in the MS link. For testing, the picture below uses the check boxes to left of the button to disable the button on the right.
IMHO, using this strategy is user friendly. If you simply make the button invisible or read only, then the user is possibly going to think your code is messed up and not have a clear understanding of WHY the button is missing or doesn’t work. A disabled button indicates to the user that the button is not available for that item. An option would be to have a mouse roll-over indicating why the button is disabled.

how to add combobox items in listbox or listview

I want to know what control to be use on my project is it Listview or listbox???. I have a comboBox control on my project what I want to do is when I selected 1 item on my combobox it will automatically add on listbox or listview and when I selected more than 1 item I want to add it on listbox or listview on newline...
Is it simple, please help me to do that in listbox or listview..thanks!
Listbox > Is for viewing too but user can select it
Listview > Is for viewing only, user cannot select also it viewing by five view directly cause it's for viewing only
If your project wanted the list to be viewing from what have been select by Combobox, then you just pick List View, but if you want for viewing also user can select it, better use listbox, so it's up to you.
Also you can know how the Tools work by focus your mouse cursor to the tool, then it will pop up an tooltip that write what the tool for.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ListView1.Items.Add(ComboBox1.SelectedIndex)
End Sub
That is the code to viewing in listview for what you select in combobox
For clearing all the item in the Listview or listbox, just write to your form_load
Listview.items.clear
Why i said in form load, cause the list just for viewing, of course every time the form begin to run, it will need blank list, so the best is put in form load
UPDATE
To remove the selected index in listbox
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
ListView Items can be selected a couple of ways In the Properties window for the ListBox the activation property allows an item to be activated by one click or two clicks. Here is an example of how the selected items can be used
If Me.ListView1.SelectedItems.Count = (1) Then
'Declare the selected item
Dim lviSelectedItem As ListViewItem = Me.listView1.SelectedItems(0)
'Now you can use it
lblLabel1.Text = lviSelecetedItem.Text
Else
lblLabel2.Text = "You can make Items selectable by switching CheckBoxes property to True in the Properties windows and using CheckBox event handlers"
End If

Make a button have multiple uses

okay... How do I explain this without being totally confusing?... Alright, I have this form that has MenuScripts (top-levels and second-levels). The problem that I am having is one of the second-levels is "Add" which brings you to another form when clicked. This other form has a button ("Record") and text boxes. This other form allows the user to input data and when the record button is clicked, the inputted data is written into a text file. Ok, so back to the first form. Another second-level MenuScript is "Update" which also brings the user to the other form; but first, the user has to click an item within a listbox to proceed. How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the record button to update data instead of being confused and thinking it is only a add-data button?
Is there a way to use an "if" statement to say something like "if mnuAdd is clicked then" "elseif mnuUpdate is clicked then". Would something like that work for giving the record button multiple uses?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus! Thanks, guys!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal
Easiest way: before displaying the second form set it's Tag property to something distinct – say "Add" or "Update" – depending on which menu item is selected. Then you just test the Tag value in the button's Click event and proceed accordingly.
As for determining whether a list item is selected: well if there isn't the ListBox's SelectedIndex property will be set to -1.
You need to put a public property on the second form (Details) which specifies which mode it is in. For instance, you could create a mode enumeration like this:
Public Enum EntryModes
AddBook
UpdateBook
End Enum
Then, define a public mode property on the second form, like this:
Public Property EntryMode As EntryModes
Get
Return _entryMode
End Get
Set(ByVal value As EntryMode)
_entryMode = value
End Set
End Property
Private _entryMode As EntryMode
Then, when you show the second form from the menu, just set the property first, before showing it:
Private Sub mnuAdd_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.AddBook
dialog.ShowDialog()
End Sub
Private Sub mnuUpdate_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.UpdateBook
dialog.BookToUpdate = ListBox1.SelectedItem
dialog.ShowDialog()
End Sub
As you can see, in the Upate menu click, I also added a line that passes the information for which book should be updated.

Canceling a TabControl tab selection in VB.NET

Really wracking my brain here and I'm sure it's something simple I'm missing.
Basically I have a form with two tabs. I'm checking the controls on each tab to see if they're dirty and want to prevent a user from clicking a tab if there are changes on the current tab.
I had thought if I check the dirty variable and just set the tab index to the one that hasn't been selected I'd be fine but every time I programatically set a tab's property, it fires off a bunch of the tab's events that just produce an undesireable result such as the right control set is showing but the wrong tab is selected.
Here is the code for as close as I've gotten to getting it to work.
Private Sub objTabs_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles objTabs.Selected
If bIsDirty Then
If Me.objTabs.SelectedIndex = 1 Then
Me.objTabs.SelectedTab = tabLetterofCreditBanks
Me.objTabs.SelectTab(0)
Else
Me.objTabs.SelectedTab = tabWireTransferBanks
Me.objTabs.SelectTab(1)
End If
End If
End Sub
Thanks in advance.
Try using the Selecting event instead of the Selected event - this will give you the opportunity to override the user's behavior (i.e. setting TabControlCancelEventArgs.Cancel to True).

Showing List of combobox while getting focus (vb.net)

When we click on drop down combobox control in our windows form, it shows the list automatically.
But when we press tab and navigate to that control from keyboard it doesn't shows the list automatically. So in other to show the list automatically on receiving focus what should be done?
Set the DroppedDown property of the combobox equal to true.
Private Sub myComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.GotFocus
myComboBox.DroppedDown= true
End Sub
I would like to mention on thing here.
I used Enter event to show Drop down list with DroppedDown=true,
But When I type something in text area of combobox and if i leave the area to next control, entered text is lost.
My combobox is databound.