CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2014
    Posts
    3

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: What should I be doing differently?

    Form_Load is BEFORE the form is loaded. Wrong place!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: What should I be doing differently?

    Quote Originally Posted by dglienna View Post
    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

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    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
  •  





Click Here to Expand Forum to Full Width

Featured