dcsimg
Results 1 to 5 of 5

Thread: Can A ComboBox Load Text Files?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    1

    Can A ComboBox Load Text Files?

    Wondering if anybody can help me, I have virtually no experience whatsoever when it comes to coding in Visual Basic so any help would be much appreciated. Atm I'm just messing about trying to get to grips with various different aspects of Visual Studio.

    Basically, is it possible to use a ComboBox and load specific text files? For example.. Say I were to make a football sim and I want the user to select which team they want to manage, is it possible to then load specific text files onto another form depending on their selection?

    For example..

    Name:  Screen1.png
Views: 191
Size:  19.0 KB

    I have created "TeamA" "TeamB" and "TeamC" in the ComboBox.

    I have then also created a text file for each individual team which contains the "Players" and "Stats"

    Name:  Screen3.png
Views: 179
Size:  27.7 KB

    Is it then possible to load the selected team's data into either "Labels" or "TextBoxes" on a different form like below..

    Name:  Screen2.png
Views: 183
Size:  23.3 KB

    Any help would be very appreciated! Also.. If there's an easier method on how to do this please let me know. Thanks!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,204

    Re: Can A ComboBox Load Text Files?

    A ComboBox displays a list of items and allows you to select one of them. That's it. What you do with that selection is up to you and nothing at all to do with the ComboBox. It has no knowledge of, no care for, what the items mean to the application.

    The first thing you need to do is handle the appropriate event. SelectedIndexChanged is raised every time the SelectedIndex property changes, as the name suggests, while the SelectionChangeCommitted event is raised only when the user selects an item in the UI. Whichever event you choose, you can then get the selection using either the Text or SelectedValue property, whichever is more appropriate for you. It's then completely up to you what you do with that value. If you want to open a file and display its contents in a form then by all means go ahead and do that.

    It's important to realise that you are actually asking at least three different questions here. If you want to solve a problem - any problem and not just in programming - the first step is to break it down into smaller parts and tackle each one individually. In this case, getting the user's selection from a ComboBox, opening and reading the contents from a file and then displaying data in controls are three separate tasks. You should investigate each separately, implement each individually and then put the three solutions together to complete the overall task. If you encounter a specific issue with any one part, ask about that issue specifically.

  3. #3
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,133

    Re: Can A ComboBox Load Text Files?

    You need to detect the selection for example in a Selected Index Change Event you could do this:

    Code:
    If Combobox1.text = Team1 then 
    
    'Open file
    
    End if
    Since its probably certain you have a few options, you would probably want to do something more like Select Case.

    As for the secondary item, you should be using a database. You can then use something like a listbox or listview to add columns and values to the columns.
    Disclaimer: When code is given for example - it is merely a example.
    See my browser project BeffsBrowser
    Try my new Browser project LightBrowse "A Browser and not much else" Screen Capture with Imgur API


    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,072

    Re: Can A ComboBox Load Text Files?

    jmc has given you a place to start,
    getting the user's selection from a ComboBox, opening and reading the contents from a file and then displaying data in controls are three separate tasks
    jdc provided info on the first task.

    As for reading text files, this forum many many posts on the subject (use the search function), or you can also find lots of example using Google.

    I would suggest using a DataGridView to display the data from the file. Again search this forum or Google something like "visual basic display text file datagridview"

  5. #5
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,851

    Re: Can A ComboBox Load Text Files?

    here a sample how you could use a XML File with a Datagridview
    is uses a DataTable and Bindingsource

    might be to much Information at once but those will be the Tools you will always use in the future

    Code:
    Public Class Form2
    
        Private dt As New DataTable("myPlayers")
        Private bs As New BindingSource
    
        Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
            'save the Data when you close the Form
            Me.Validate()
            bs.EndEdit()
            dt.WriteXml("E:\Teams.xml")
        End Sub
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' your columns
            dt.Columns.Add("Player", GetType(System.String))
            dt.Columns.Add("Position", GetType(System.String))
    
            ' XML load
            If IO.File.Exists("E:\Teams.xml") Then
                dt.ReadXml("E:\Teams.xml")
            End If
    
            ' bind  Datatable-Bindingsource
            bs.DataSource = dt
    
            ' bind Textboxes 
            Me.DataGridView1.DataSource = bs
            Me.TextBox1.DataBindings.Add("Text", bs, "Player")
            Me.TextBox2.DataBindings.Add("Text", bs, "Position")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' add new line in Datagridview for Input
            bs.AddNew()
    
        End Sub
    End Class

    hth
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width