Visual Basic 2015 Select a item in Listview programatically - vb.net

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

Related

Converting check List in VB.NET to integer

This is my first time working with VB.Net It's a college assignment and is already due for submission.I want to create a simple program that determines if a user has Ebola or not. So, there is a list of checkbox containing the core symptoms of Ebola. If the user selects 4 or more there should be a prompt message saying the user most likely has Ebola otherwise user does not have Ebola.
I have created the form and it works but don't know how to implement the checkbox into numbers that will be summed up.
Here is the code for the form
Public Class Checkbxvb
Private Sub Checkbxvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "Click to select the symptoms you are having"
CheckBox1.Text = "Fever"
CheckBox2.Text = "Loss of appetite"
CheckBox3.Text = "sore throat"
CheckBox4.Text = "Gastrointestinal Symptoms"
CheckBox5.Text = "Unexplained bleeding or bruising"
CheckBox6.Text = "Loss of weight"
Button1.Text = "Submit"
Button2.Text = "Close"
End Sub
I want to create a button that will collect the user input like I said earlier on. Thanks
If you are using a CheckListBox you can used its CheckedItems collection Count property to get what you need.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckedListBox1.CheckedItems.Count > 3 Then
MessageBox.Show("You may have Ebola.")
Else
MessageBox.Show("You probably don't have Ebola.")
End If
End Sub
If you are using CheckBox controls add them to a List(Of T). The T stands for Type. Create a variable for the list at Form level so it can be seen from the Form.Load and the button click. Then you can loop through your collection and increment a counter when the Checked property is True.
Private CkBxList As New List(Of CheckBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CkBxList.AddRange({CheckBox1, CheckBox2, CheckBox3, CheckBox4})
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter As Integer
For Each cb In CkBxList
If cb.Checked Then
counter += 1
End If
Next
If counter > 3 Then
MessageBox.Show("You may have Ebola.")
Else
MessageBox.Show("You probably don't have Ebola.")
End If
End Sub
As you can see the code is much simpler with a CheckedListBox. Try to use the control that best suits your purpose.

How to transfer data from listbox to textbox in Visual Basic

I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub

Keep the latest combobox selection when re-opening the application

I am working on a VB.Net Visual Studio application. If I select say value "B" from a combobox and then close the application, when I re-open it I want that combobox to have the latest choice selected before closing the app to be selected. How do I do that? Thank you.
In Project Properties select Settings.
Set the Name, Type, Scope and Value as shown below.
I use the SelectedIndex so if nothing is selected, it will still work with -1.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = My.Settings.ComboSelection
End Sub
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.ComboSelection = ComboBox1.SelectedIndex
End Sub

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.

How to Pass Image in ImageList to PictureBox in Reverse Index Order

I have a VB.Net application which has spelling words that a child can type in and it verifies the spelling. On the form, I have a Next and Back button, which advances a PictureBox with items contained in an ImageList. I have 3 items in the ImageList collection with an index[2].
Here's the code that I'm using to advance the Next button to display the next image in the collection:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Get images and place them in the Imagebox on each click.
Static count As Integer = 1
If count > ImageList1.Images.Count - 1 Then
count = 0
End If
PictureBox1.Image = ImageList1.Images(count)
count += 1
End Sub
While this works, I cannot figure out how to get this to work in the reverse order. Here's the OnLoad event handler that starts with the first image in the collection:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set picturebox with initial picture
PictureBox1.Image = ImageList1.Images(0)
txtSpell.Focus()
End Sub
How can I get the Back button to go backwards in the index from it's current point in the index which is shown in the PicutureBox control? This is where I'm stumped. Tried to re-write the code several times, if I click Next once, I go to the next image index[1] and if click the Back button, it takes me to index[0].
But if click next again, PictureBox jumps to index[2] the last image, rather than going back to index[1]. If I click Back again, the code is blowing up.
Move your count variable (which is badly named, BTW - I've used CurrIdx instead) to a higher visibility. In the previous button's click handler, decrement the index; if it drops below 0, reset it to the ImageList.Images.Count - 1 again. I'd also move the code that actually sets the image index to its own procedure, so that you're not repeating yourself and it's more clear. Something like this should work for you:
Private CurrIdx As Integer = 0
Private Sub UpdateImage()
PictureBox1.Image = ImageList1.Images(CurrIdx)
End Sub
Private Sub PrevButton_Click(sender As Object, e As EventArgs) Handles PrevButton.Click
CurrIdx -= 1
If CurrIdx < 0 Then
CurrIdx = ImageList1.Images.Count - 1
End If
UpdateImage()
End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
CurrIdx += 1
If CurrIdx > ImageList1.Images.Count - 1 Then
CurrIdx = 0
End If
UpdateImage()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateImage()
End Sub
First of all, I would like to give thanks to both Ken White, and valter, for assisting me and opening up my thinking in this approach. I used Ken's advice on my variable naming and valter's approach for my first solution of the btnNext problem.
Thus, as I had a working btnNext to flip through my images in the imagelist1 collection and push them to the picturebox1, I took Ken's advice arranged my code and renamed variables to get btnPrevious to work as such:
Private CounterVar As Integer = 0
Private Sub ImageUpdate()
'Sets the picture box to the first image of the series.
PictureBox1.Image = ImageList1.Images(CounterVar)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
'Allow the Back button to respond the appropriate PictureBox item.
If CounterVar = 1 Then 'This is the second imagelist item, index[1].
CounterVar = 0 'This is the first imagelist item, index[0].
ImageUpdate()
ElseIf CounterVar = 2 Then 'This is the last imagelist item, index[2].
CounterVar = 1 'This is the second imagelist item, index[1].
ImageUpdate()
Else
CounterVar = CounterVar 'Otherwise, the count remains where it is till Back is clicked.
End If
ImageUpdate()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Setup counter variable to begin the button processing.
CounterVar += 1
'Get Imagebox items and place them in the PictureBox, in order, on each click.
If CounterVar > ImageList1.Images.Count - 1 Then
CounterVar = 0
End If
ImageUpdate()
'On a correct answer, clear the textbox, spell label, and hide the result label,
'then set the focus of the textbox.
txtSpell.Text = String.Empty
lblAnsResult.Text = String.Empty
lblAnsResult.Visible = False
txtSpell.Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set imagebox to first index image.
ImageUpdate()
'Set focus to the textbox.
txtSpell.Focus()
End Sub
This is what I was trying to accomplish with btnPrevious, without it skipping around or out of order. Although the code can skip around via Next, the user cannot go backward beyond the first image in the Imagebox1 index[0]. This is what I wanted. Again, a BIG THANKS to those who helped me to think it out. That's what being on Stack is all about... learn from the answers you get, and run free developing in confidence... like a happy baby w/out a diaper! Thanks fellas!