Visual Basic.Net stopping user selecting listbox items - vb.net

How do I make it so that the user can't select the first two items in a list box. I'm trying to make a program that runs when the user changes their selected index, but the first two items are going to be headings for a table so I do not want the program to run if the user tries to select them.

You could just unselect them again:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex < 2 Then
If ListBox1.Items.Count < 2 Then
ListBox1.SelectedIndex = -1
Else
ListBox1.SelectedIndex = 2
End If
End If
End Sub
It does have -5 elegance points, the kind you can get back by using a Label above the ListBox or by using a ListView instead.

As with Hans' answer, it's not very clean but might I suggest:
ListBox1.RemoveAt(0)
ListBox1.RemoveAt(1)
It won't work if the list is databound, but if that's the case then you might like to try
ListBox1.DataSource = linqQuery.Skip(2).ToList()

Related

dynamically add webbrowsercontrols to form

I have a listbox, this listbox contains numbers. (ID's) A timer should check this listbox every mintue, and for every newly added ID, add a webbrowsercontrol to the Form, with 100% width and hight.. So if for example after 5 Minutes I have 3 Items in the listbox, I need to have 3 Webbrowsercontrols one on top of the other. These webbrowsercontrols need to be called in a way that I recognize which one is what. for example: wbcontrol_ID so that I can selectively bring to front the one I currently need. (wbcontrol_ID.BringToFront()) The code also needs to verify that the Webbrowsercontrol has not been generated already, else it will create them over and over again.
This is my code of the timer so far:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
fillitemsinlistbox()
For Each r In ListBox1.Items
dim mybrowser as new webbrowser
Next
End Sub
And here I am stuck.

VB.NET Deleting last item from 2 listboxes at once won't work

I'm working on a program called "Smart Search". In the "customize search" form,
I made it possible to delete items. Well, the problem here is that it's two listboxes. One for what you should search for and one for what happens when you search: https://i.imgur.com/aGZRoeX.png
Oh, and btw. When you select an item in one of the listbox, the same selectedindex will be selected in the other.
But when I delete the last item(s) in the list I get a error.
I think it's something wrong with my code but I can't find anything wrong with it.
The code: http://pastebin.com/LRBfx5DL
Try something like this:
Private Sub rembtn1_Click(sender As Object, e As EventArgs) Handles rembtn1.Click
Dim index As Integer = ListBox1.SelectedIndex
If index <> -1 Then
ListBox1.Items.RemoveAt(index)
If index < ListBox2.Items.Count Then
ListBox2.Items.RemoveAt(index)
End If
save()
End If
End Sub

my program is not adding items to listbox and is freezing up

im trying to add a very large amount of items to list box and what i need it for is i'm using to add selected items to itextsharp table report im using filters in the table just to display either the sales person who handled the customer or the date at which the incident occurred (or Issue was reported with the product) my filter system is as follows i have 4 categories which is 4 listboxes customer name, customer code(named listBox1 i have not got around to changing it yet) species name, and the error type my filter is placed under the searchBtn_Click event my filter and item adding code is as follows:
Private Sub searchBtn_Click(sender As Object, e As EventArgs) Handles searchBtn.Click
For Each obj As contactObject In custList
For Each item As speciesObject In speciesList
'loadLists()
If Trim(fromDate.Value) < Trim(obj.eventDate) Then
If Trim(toDate.Value) > Trim(obj.eventDate) Then
If Trim(fromDate.Value) < Trim(item.compDate) Then
If Trim(toDate.Value) > Trim(item.compDate) Then
End If
If Not customerListBox.Items.Contains(obj.customerName) Then
customerListBox.Items.Add(obj.customerName)
End If
If Not ListBox1.Items.Contains(obj.customer) Then
ListBox1.Items.Add(obj.customer)
End If
If Not speciesListBox.Items.Contains(item.name) Then
If ListBox1.Items.Contains(item.customerCode) Then
speciesListBox.Items.Add(Trim(item.name).ToUpper)
End If
End If
If Not errorListBox.Items.Contains(obj.issue + " - " + obj.issueDescription) Then
errorListBox.Items.Add(Trim(obj.issue + " - " + obj.issueDescription).ToUpper)
End If
End If
End If
End If
Next
Next
countErrors()
End Sub
then i have the query which is set up to get the customer info from the database system
Dim SqlText As String = "SELECT DISTINCT QEE.[EventID] ,QEE.[EventDate] ,QEE.[Employee] ,QEE.[Communication] ,QEE.[OtherCommunication] ,QEE.[Issue] ,QEE.[IssueDescription] ,QEE.[IssueComments] ,QEE.[Resolution] ,QEE.[ResolutionComments] ,QEE.[SalesOrderNumber] ,QEE.[CustomerPO] ,QEE.[SOStatus] ,QEE.[Customer] ,QEE.[CustomerName] ,QEE.[SalesPersonName] ,QEE.[IsResolved] ,QEE.[IssueValue] ,QEE.[DateAndTimeAdded] ,DATEDIFF(day, SOR.ReqShipDate, QEE.[EventDate]) AS Elapsed, SOR.ReqShipDate FROM [QualityTracking].[dbo].[tblQualityEventEntry] QEE INNER JOIN SysproCompanyC.dbo.SorMaster SOR ON QEE.SalesOrderNumber = SOR.SalesOrder COLLATE Latin1_General_CI_AS ORDER BY EventDate ASC, CustomerName ASC, SalesOrderNumber ASC;"
I could not fit all code on here
if you could also just general things to help as well i am new to vb.net but for other information things i have tried :
*listbox.startUpdate/endUpdate
*changing querys
*changing the sorted property (Right now its set for false)
*the issue happens when i choose select all and then hit search the database is holding almost 2Mil items and i need to be able to get it to move once i get it work stop freezing i will be able to increase the speed i just cant figure out totally where the issue is i know the query could be better probably (if you have any suggestions please feel free i'm learning)
*but i also see alot of people having this issue with listbox as being kinda a broken down way of listing items
*i have tried researching it and people have said use something else i cant do that for listbox is committed
Assuming Windows Forms.
You program might not be responding because of too many records to add, and each time you add an item into the ListBox.Items collection, the UI is refreshed.
You may either SuspendLayout while adding the lines into your Listbox, and ResumeLayout afterwards.
Private Sub searchBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
customerListBox.SuspendLayout();
// Place your code to populate the ListBox control here...
customerListBox.ResumeLayout();
End sub
This shall avoid a lot of refreshes from occuring while adding the items one by one and allow the application to lighten the add of items, then you resume the layout so that it refreshes the controls to display adequate information to the screen.
OR
You may use the ListBox.Items.AddRange() method along with List.ToArray().
Private Sub searchBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim custList As List(Of ConstactObject) = loadYourCustomers();
customerListBox.Items.AddRange(custList.ToArray());
Dim speciesList As List(Of SpeciesObject) = loadYourSpecies();
speciesListBox.Items.AddRange(speciesList.ToArray());
End sub
OR ELSE
I recommend using a DataGridView and setting its DataSource property to your list of objects.
So, in order to have the correct data, you'll have to:
Drop two DataGridView on your Form
Rename both DataGridView to a meaningful name (e.g. custListDataGridView, speciesListDataGridview)
Drop two BindingSource on your Form
Rename both BindingSource to a meaningful name (e.g. custListBindingSource, speciesListBindingSource)
In the designer, set the DataGridView.DataSource property to your respective BindingSource (e.g. custListDataGridview.DataSource = custListBindingsource, speciesListDataGridView.DataSource = speciesListBindingSource)
In the backing code, that is, in your searchBtn.Click event, you may set both your binding sources DataSource property
Private Sub searchBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim custList As IList(Of ContactObject) = loadYourContactObjects();
custListBindingSource.DataSource = custList;
Dim speciesList As IList(Of SpeciesObject) = loadYourSpeciesObject();
speciesListBindingSource.DataSource = speciesList;
End Sub
And your information data should be listed automatically without you having to manually add each record.

Storing listbox item indices for persistent selection

I've had such fantastic help so far. I've got a listbox that automatically populates with the folder names of the path that the user browses to. When the browse button is clicked and the listbox populates the first folder is automatically selected:
ListBox1.SetSelected(0, True)
Next I wrote some code so that if the user tries to unselect the only folder, then it holds its selected state:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count = 1 Then
My.Settings.Index1 = ListBox1.SelectedItem
End If
If ListBox1.SelectedItems.Count = 0 Then
ListBox1.SetSelected(My.Settings.Index1, True)
End If
End Sub
However, ideally I need to hold the indices of all selected items in memory so that when the form reloads I can restore the selection along with the list box items. I think I would like to do this when the item count is greater than one. But I need some suggestions on how to do this. Not necessarily examples, but just the methodology or approach so I can make an attempt. Many thanks!!!

Adding a default number of items to list box in Visual Basic

I'm working on a project for my Visual Basic class and need a nudge in the right direction. I'm not going to go into exactly what the program is supposed to do in the end because I'm not really at that point yet. The step I'm stuck on now is this.
We are supposed to have two list boxes, one for Ingredients (which I've named lstIngredients) and the second for the Recipe (lstRecipe), we are also supposed to have a text box labeled Quantity for the user to enter how many of the selected item to add to the Recipe list. For example, if the user selects "eggs" and types 3 for the quantity, "eggs" should appear 3 times in the Recipe list box. Also, if nothing is put into the quantity box, it is supposed to default to adding one of the selected item to the Recipe list box.
With the code I have written, I am able to add items to the Recipe list as long as I type something into the quantity text box, but I cannot get the program to just add one when nothing is typed into the text box. Here is the code I have so far.
Public Class Form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim i As Integer = lstIngredients.SelectedIndex
If txtQuantity.text= "" Then
lstRecipe.Items.Add(1)
End If
Dim intCount As Integer = 0
While intCount < txtQuantity.Text
lstRecipe.Items.Add(lstIngredients.Items(i))
intCount += 1
End While
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstRecipe.Items.Clear()
txtQuantity.Clear()
End Sub
Any help on this step would be greatly appreciated. I'm sure I'll probably have more questions as I get farther into the coding, but I will edit this question to include those when the time comes.
First, you'll need to convert the value in your Quantity text box to an integer. For that, you can use either Integer.Parse or Integer.TryParse. For instance:
Dim value As Integer = Integer.Parse(Quantity.Text)
Then you can use a For loop to add the same item that many times, for instance:
For x As Integer = 1 to value
lstRecipe.Items.Add(lstIngredients.Items(i))
Next