VB.NET Tutorial - List Collections - Adding, Retrieving, and Removing Data (Visual Basic .NET)
Вставка
- Опубліковано 27 сер 2024
- In this Visual Basic .NET tutorial, I will be demonstrating how to easily add, retrieve, and delete data from a VB List array. I'll also be showing you various looping methods used to access the collection data and return it to a ListBox.
TUTORIAL FEATURES:
Collections - List Of String, List Of Integer
Loops - For Each, For X = (Index Count), List.ForEach method
Form Controls - TextBox, ListBox, Button
Subs & Variables
TIP JAR: www.aardaerimus...
This Techniques that you explained which is CollectionName.Foreach(addressof FunctionName) is really great, thank you teaching it to us, I really did not know that it exists.Thank you for those new things, its really helpful.
Really Really Nice Video. And i Would like to thank u sir for this....
+Manish kishore sharma Thank you for the encouraging comment, Manish. :-)
Sir I am thank full to you. because you have demonstrated the thing this way like even a novice can get the topic very easily.
Very good.
Once again... perfectly explained and made simple.
Only a question:
After saving the file to an executable, the items go within the executable ? Also saved ?
Thanks again, Carlos. :-)
In this example, the items that are added programmatically via MyStrList.Add() would be stored/saved when the application was compiled.
Anything that is added _after_ compiling will not be saved, unless you add a way to save outside of the application (database, text file, etc.).
VB Toolbox such as using my.settings
Very nice, helped a lot! Say one already has some data that they would like insert into the list, how would you preload from a class in order to keep the main form more clean?
You could easily populate your list with values from a text file (.txt,.csv,etc.) at runtime using the StreamReader. This would keep your code clean and also allow you to easily change any of those values outside of the application if you wanted to.
If you prefer to use a class, string, or something similar, you can certainly do that as well; However, if you already have a class fully loaded with variables containing your list values, you could just use an instance of that class and abandon the collection altogether.
how does a list implement the equals method
Hi, how are you? I've got one question. Can this collection be used for containing all forms in the project(those closed or open). And would it be posible to show a chosen form? I'm trying to show some forms without using the openforms method because these forms aren't open :(. Could you, please make a video tutorial to show that with a listbox?
Thank you for ur guiding, It is really helpful to the beginner of Visual base like me.
I have saw this code before when i learning to make Mario for those uncountable monster. However i couldn't make it clear how it works at that time.
Got a question,
I want to show out what is in the list anytime, so i make a timer tick to updating data to ListBox1..
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MyStrList.ForEach(AddressOf ListBox1)
End Sub
However, this doesn't work. Have i do something wrong in this code? Or it is unthinkable to combine timer with list-out?
It would probably be more efficient to have a Sub to refresh the listbox contents whenever the list is manipulated. That way you don't have a timer constantly recycling the list even if nothing is changed.
Example:
[Refresh the ListBox]
Private Sub RefreshListBox()
ListBox1.Items.Clear()
MyStrList.ForEach(Sub(i) ListBox1.Items.Add(i))
ListBox1.Sorted = True
End Sub
[Remove an item from the list and refresh the ListBox]
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyStrList.RemoveAt(1)
RefreshListBox()
End Sub
nice
Quick question, how do i remove a specific line from the list? what would be the code for that e.g say you have 3 different things in the list, and you want to remove the second one and replace it with something else, how do i go about doing that?
Wardude202 This will depend upon a couple of things like what data type is in your list, and whether you know the position (index) of the item that you want to change.
The simplest method is to just set the new value by its index:
Example [List Of String]
MyList(3) = "Awesome"
That would replace the 4th item in the list with "Awesome" and preserve its position in the list.
If you're using custom data types and looping the values you cannot alter their indexes directly; However, you can use a little magic and convert the List to a copy of itself and use the current index position to alter the original.
_For Each item In MyList.ToList_
Thanks u!!!!!!!! Great job
very good tutorial. easy to understand. thanks. i have a question though. i have a picturebox control which is part of a list collection. how do i reference it without referencing it tostring? i want to be able to change properties of the pictureboxes in the list.
buddyroach Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.PictureBox'.
buddyroach Are you actually storing it to your List as a PictureBox? [e.g., Private lstPictureBoxes New List(Of PictureBox) ]
If so, you should be able to reference their properties directly or via lambda expression.
If you're just storing the Names of PictureBoxes as a string to the List, then you could call the controls by name from the Form's Control collection.
Let me know if you need a demo. :-)
VB Toolbox yes i am storing the pictureboxes themselves to the list because the user creates them. im using this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim canvas = New PictureBox
canvases.Add(canvas)
canvas.Name = "canvas" & canvases.Count.ToString()
canvas.BackColor = Color.White
canvas.BorderStyle = BorderStyle.FixedSingle
canvas.Image = Nothing
canvas.Height = TextBox2.Text
canvas.Width = TextBox1.Text
AddHandler canvas.MouseDown, AddressOf PictureBox1_MouseDown
AddHandler canvas.MouseUp, AddressOf PictureBox1_MouseUp
AddHandler canvas.MouseMove, AddressOf PictureBox1_MouseMove
canvas.Top = Panel2.Bottom
canvas.Left = Panel1.Right
Controls.Add(canvas)
Panel3.Visible = False
Catch ex As Exception
End Try
My.Settings.canvaswidth = TextBox1.Text
My.Settings.canvasheight = TextBox2.Text
My.Settings.Save()
ComboBox1.Items.Clear()
For Each C In canvases
ComboBox1.Items.Add(C.Name)
ComboBox1.SelectedItem = C.Name
C.BringToFront()
Next
ComboBox1.SelectedItem = My.Settings.selectedcanvas
End Sub
thanks...
buddyroach oh, and of course in public class at top i did:
Dim canvases As New List(Of PictureBox)
buddyroach Sorry for the slow replies. I'm super buried at work right now, but I'll try to help out with this as soon as I'm able. :-)
Nice explained, but I don't have a use for this as it's only a single dimension.
Hi, Thanks for the videos. Any plans to publish a video on Async Await & Task class in VB .Net ?
Unfortunately, production is kind of dead in the water until I have electricity and and reliable internet again.
@@VBToolbox Oh, I am so sorry about that. I hope the situation will be better soon. :)
Hi.. can u help with the list things?
Hi, do you know how to populate a string array with data from a sharepoint list. My goal is to populate a listbox in a windows form with this data every time a user uses the form. sorry if this is a dumb question I'm very new to vb
elkolodj1 I'm sorry, elkolodj1. Unfortunately, I have no experience with Sharepoint lists. Perhaps someone else around here might know? Adding values to a list or array is generally easy enough.
With a List you can simply use the Add() method:
Dim lst As New List(Of String)
lst.Add("mySharepointValue")
If you're using an array you may need to redefine (ReDim) to increase its indexes.
Thanks, I solved with a workaround creating a connection that gets the recordset for the fields in the sharepoint list. I thought it was easier, but i solved it now. Thanks!
Is it possible that other members can also add(view, etc.) strings to the list?
exocrypton Due to the fact that the collection exists only in the memory of the computer running the application, the only way that another user could access the list is via a client/server application. For multi-client or multi-user access, it would be much easier to use a database.
VB Toolbox I thought of a Database that works with e.g. a ListView via MySQL, does that work?
Yep. :-) With a database connection you can do a lot of different things. I prefer a DataGridView over a ListView, but that depends entirely upon your needs.
instead of a message box how does one show the contents of a list to a textbox
+Mark Watson Hello, Mark. :-)
Simply add a TextBox to your form and set its "Multiline" property to "True", then use the following:
For Each s In MyStrList
TextBox1.AppendText(s & vbNewLine)
Next
Or if you're on VB 2010+ you can shorten to one line:
MyStrList.ForEach(Sub(s) TextBox1.AppendText(s & vbNewLine))
If you want a specific line index in a single-line textbox:
TextBox1.Text = MyStrList(0) 'Displays the first item in your List
I hope that helps!
Thankyou very much!
+Mark Watson Happy to help out!
Woah I thought I knew vb.net, I've been trying all the methods I wouldn't touch, I would do it like...
For Each s In MyStrList
TextBox1.text &= (s & vbCrLf)
Next
All the lambda expressions i didn't know vb.net even supported
excelente, como lo podría hacer un While sdr.Read()
Another great video if you do.nt say where you learn or the names of the books which explaining all this staf i will kill my self
Hi baherwahby22, I answered on your other comments. I only learned by playing around and sometimes searching on specific problems if I get stuck - no books, no school.