vb.net select listviewitem by giving it's index - vb.net

I'm searching everywhere but I cannot seem to find the answer.
In VB.net I want to select a listviewitem by giving it's index.
Let say: If I push a button it has to select row 5.
I don't seem to get it to work...
Can anyone help me?
Thanks

Let's assume the following situation:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.Items.Add("Item 1") 'Index 0
ListView1.Items.Add("Item 2") 'Index 1
ListView1.Items.Add("Item 3") 'Index 2
End Sub
You can select an item by using:
ListView1.Items.Item(index).Selected = True
For example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.Items.Item(1).Selected = True
'Not necessary, just to show the blue color instead of gray
ListView1.Select()
End Sub
Here is the result before and after pressing Button1.

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

Visual Basic 2015 Select a item in Listview programatically

I am trying to figure out how to show a item as selected on a listview in a form using Visual Basic 2015. I fill in a listview on a form load, then I want to be able to use up and down buttons on the form to move through the list. I have seen almost this exact question all over the place but none of the answers work. Some of the examples given don't even show up on the IDE as choices. I am using Visual Studio 2015 IDE. Does anyone have a link or code that is more recent to go with the new Visual Basic IDE?
Not sure if this is actually what you are looking for (or if there is a more elegant manner).
The comments are self-explanatory of what are we doing.
''Loading test items
Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ListView1.Items.Add("Text1")
Me.ListView1.Items.Add("Text2")
Me.ListView1.Items.Add("Text3")
Me.ListView1.Items.Add("Text4")
End Sub
''Go up in the list
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim intActualIndex As Integer
If Me.ListView1.SelectedItems.Count > 0 Then ''We check if we have a selected item
intActualIndex = Me.ListView1.SelectedIndices.Item(0) ''We get the actual index of the selected item
If intActualIndex < Me.ListView1.Items.Count - 1 Then Me.ListView1.Items(intActualIndex + 1).Selected = True ''We set the .selected=true on the next item
End If
End Sub
''Go down in the list
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim intActualIndex As Integer
If Me.ListView1.SelectedItems.Count > 0 Then
intActualIndex = Me.ListView1.SelectedIndices.Item(0)
If intActualIndex > 0 Then Me.ListView1.Items(intActualIndex - 1).Selected = True
End If
End Sub

Visual Studio Button Event

I would like to code a button that after 3 clicks, links the user to my site. The first 2 should generate a code in the textbox and the last one should then link them. This is what i have so far
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "Thank you"
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Timer1.Start()
ElseIf RadioButton2.checked = True Then
Timer1.Start()
Else
TextBox1.Text = "Please Select Option"
End If
End Sub
I dont know what is the use of ProgressBar and Timer. But if you wan to know how many times user has click on a button, a counter variable should do.
Private _clickCounter As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_clickCounter += 1
If _clickCounter = 3 Then
MsgBox("3 times")
_clickCounter = 0
'link to your site
Else
'generate code in textbox
End If
End Sub
Use an integer that increases each time the button is clicked and a Select Case statement to detect that;
Dim NumberOfClicks As Integer = 0
Dim webAddress As String = "http://www.YourWebsite.com/"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = number + 1
Select Case number
Case 1
''''' Code for your textbox
Case 2
''''' Code for your textbox
Case 3
Process.Start(webAddress)
End Sub
This should do. Please mark my answer as solved if it helps.

Select item from ComboBox to open web links on WebBrowser?

i've been looking around on how to make a combobox list choice to access a webpage on webbrowser. For example, if i choose the first item in the combobox wich is named "Google" then i would press on the button next to it to access google on the webbrowser.
I got this code but it doesn't work, once i choose the first option, nothing happens.
If ComboBox1.SelectedIndex = 1 Then
WebBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl")
End If
I seems so close, but i have no clues why it doesn't work..
Try this...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case ComboBox1.SelectedItem
Case "Please Select"
MsgBox("ERROR - No selection made in dropdown box!")
Case "Google"
WebBrowser1.Navigate("www.google.com")
Case "Microsoft"
WebBrowser1.Navigate("www.microsoft.com")
Case "Stack Overflow"
WebBrowser1.Navigate("www.stackoverflow.com")
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
ComboBox1.Items.Add("Please Select")
ComboBox1.Items.Add("Google")
ComboBox1.Items.Add("Microsoft")
ComboBox1.Items.Add("Stack Overflow")
ComboBox1.SelectedIndex = 0
'
End Sub
Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
ProgressBar1.Visible = True
With ProgressBar1
.Minimum = 0
.Maximum = 50
.Step = 5
End With
For index As Integer = 0 To 50 Step 5
ProgressBar1.Value = index
System.Threading.Thread.Sleep(35)
Next
End Sub
End Class
Is the item that's selected first? The index is 0-based. Meaning the first item in the list is index #0. Try it with selectedindex = 0.

VB.NET Save Combo Box to My.Settings

Looking to save combobox items to my.settings collection. I developed a webbrowser and a combobox will be my address bar. I am attempting to save a history of visited sites.
I tried the below code and it doesnt work. It errors out with "Object reference not set to an instance of an object":
Went into settings added MyItems for the name, and then select System.Collections.Specialized.StringCollection as the data type. Then onload is the below:
For Each i As String In My.Settings.MyItems
ComboBox1.Items.Add(i)
Next
FormClosing and ive tried FormClosed: For now i put it in a button event to save it for testing
My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
My.Settings.MyItems.Add(i)
Next
I love this site very much! So I came back to post the correct code that will correctly save and load combobox entries to my.setting! This has been tested as working!!!
Private Sub Form1_FormClosing(
sender As Object,
e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Categories.Clear()
For Each item In ComboBox1.Items
My.Settings.Categories.Add(item.ToString)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.Categories
ComboBox1.Items.Add(item)
Next
ComboBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.Items.Count > 0 Then
Dim Index As Int32 = ComboBox1.SelectedIndex
ComboBox1.Items.RemoveAt(Index)
If Index - 1 <> -1 Then
ComboBox1.SelectedIndex = Index - 1
End If
End If
End Sub