-
October 11th, 2014, 04:02 PM
#1
What should I be doing differently?
I am working on a program for my Visual Basic I class that provides the user with a list of Computer Pioneers and displays their accomplishments to a text box when selected. Ive succesfully loaded the text file, but I am struggling displaying the output! Could anyone give me some insight on where to go from here?
Code:
Public Class Form1
Structure Pioneers
Dim Name As String
Dim Accomplishments As String
End Structure
Dim pioneer() As Pioneers
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim people() As String = IO.File.ReadAllLines("Pioneers.txt")
Dim n As Integer = people.Count - 1
ReDim pioneer(n)
Dim peoples() As String
For i As Integer = 0 To n
peoples = people(i).Split(","c)
pioneer(i).Name = CStr(peoples(0))
pioneer(i).Accomplishments = CStr(peoples(1))
lstNames.Items.Add(pioneer(i).Name)
Next
End Sub
Private Sub lstNames_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstNames.SelectedIndexChanged
Dim query = From person In pioneer
Where person.Accomplishments = txtAccomplishments.Text
End Sub
End Class
-
October 11th, 2014, 10:51 PM
#2
Re: What should I be doing differently?
Form_Load is BEFORE the form is loaded. Wrong place!
-
October 21st, 2014, 12:30 PM
#3
Re: What should I be doing differently?
Originally Posted by dglienna
Form_Load is BEFORE the form is loaded. Wrong place!
Actually, Form_Load happens after the form has been loaded, thus making it the perfect place for this type of processing. A complete list of Form events can be found here :
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
details on the Form.Load event can be found here :
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
And the order of events ( when a form, or rather an app starts ) can be found here :
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
-
October 31st, 2014, 01:25 PM
#4
Re: What should I be doing differently?
So far, this peace of code doesn't really do anything:
Code:
Private Sub lstNames_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstNames.SelectedIndexChanged
Dim query = From person In pioneer
Where person.Accomplishments = txtAccomplishments.Text
End Sub
The LINQ query above will filter the pioneer array, returning only elements that have Accomplishments = txtAccomplishments.Text (but, the match must be exact - be careful with whitespaces and case).
But, the query is just an object that internally stores all the operations specified for the query - it will not be actually evaluated until you enumerate the elements.
You can force the query to be evaluated by calling .ToList() on it (it will return a List(Of element_type)).
To display this data, you need to assign it to some UI element. Depending on how you're displaying it, you may need to convert it to some form that the UI can use first. For example, if you want to display it as text, then convert the filtered elements to strings (which you might also concatenate to one long string later, if required).
You can use LINQ to do this - simply add a Select clause after the Where-line; Select "projects" the elements it gets into something else, of your choosing - so if you construct and return a string within the Select clause, your query will return a list of strings.
You can either populate your UI manually and keep track of changes, or use data binding.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|