Results 1 to 13 of 13

Thread: Using combo box and stream reader

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Using combo box and stream reader

    Hi im trying to use a 2 combo boxes each with a list of 7 cities these need to link up with numbers in a stream reader to give a total distance if any1 can help it would be much appriciated. Below is my attempt at the code
    Code:
    Public Class frmDist
    
        Private Sub frmDist_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim rowcolumn(7, 7) As Integer
            Dim sr As IO.StreamReader = IO.File.OpenText("Distance.Txt")
            Dim row, col As Integer
            For row = 1 To 7
                For col = 1 To 7
                    rowcolumn(row, col) = CInt(sr.ReadLine)
                Next
            Next
            sr.Close()
        End Sub
    
        Private Sub btnDistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDistance.Click
            Dim rowcolumn(7, 7) As Integer
            Dim sr As IO.StreamReader = IO.File.OpenText("Distance.Txt")
            Dim row, col As Integer
            Dim Southampton, Edinburgh, London, Cardiff, Birmingham, Newcastle, Manchester As String
            Southampton = Convert.ToInt32(1)
            Edinburgh = Convert.ToInt32(2)
            London = Convert.ToInt32(3)
            Cardiff = Convert.ToInt32(4)
            Birmingham = Convert.ToInt32(5)
            Newcastle = Convert.ToInt32(6)
            Manchester = Convert.ToInt32(7)
            For row = 1 To 7
                For col = 1 To 7
                    rowcolumn(row, col) = CInt(sr.ReadLine)
                Next
            Next
            sr.Close()
            row = (cmb1.Text)
            col = (cmb2.Text)
            If (row >= 1 And row <= 7) And (col >= 1 And col <= 7) Then
                lblDistance.Text = ("The total milage of the return journey is ") & CStr(rowcolumn(cmb1.Text, cmb2.Text)) & (" miles.")
    
            End If
        End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Using combo box and stream reader

    I havent yet looked at your code, what is the problem you are having?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    i keep getting a large number of errors in the code that i have written and dont kno how to rectifiy them

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Using combo box and stream reader

    1) what is the contents of Distance.txt
    2) what are you trying to do exactly? Your description confuses me somewhat
    3) what typeof errors are you getting?

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    contents of distance.text are the distances between cities listed in the drop down list cobo boxes. i want to select say london in 1 drop down box and manchester in the other and have the distance of that journey displayed in a label from distance.text

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Using combo box and stream reader

    How about this:

    Code:
    Public Class Form1 
        Private _TravelLengths As New TravelLengths 
        Public Sub New() 
            InitializeComponent() 
     
            Dim FileCnts() As String = IO.File.ReadAllLines("C:\test\Distance.txt") 
            For Each Line As String In FileCnts 
                Dim CityFrom As String = Line.Split(","c)(0) 
                Dim CityTo As String = Line.Split(","c)(1) 
                Dim Distance As Integer = CInt(Line.Split(","c)(2)) 
                Add(CityFrom, CityTo) 
                _TravelLengths.Add(CityFrom, CityTo, Distance) 
            Next 
     
        End Sub 
        Private Sub Add(ByVal CityFrom, ByVal CityTo) 
     
            If Not Me.cmbCityFrom.Items.Contains(CityFrom) Then 
                Me.cmbCityFrom.Items.Add(CityFrom) 
            End If 
            If Not Me.cmbCityFrom.Items.Contains(CityTo) Then 
                Me.cmbCityFrom.Items.Add(CityTo) 
            End If 
     
            If Not Me.cmbCityTo.Items.Contains(CityFrom) Then 
                Me.cmbCityTo.Items.Add(CityFrom) 
            End If 
            If Not Me.cmbCityTo.Items.Contains(CityTo) Then 
                Me.cmbCityTo.Items.Add(CityTo) 
            End If 
     
        End Sub 
        Private Sub btnDistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDistance.Click 
            If Me.cmbCityFrom.SelectedIndex <> -1 AndAlso Me.cmbCityTo.SelectedIndex <> -1 Then 
                MessageBox.Show(Me._TravelLengths.GetDistance(Me.cmbCityFrom.SelectedItem.ToString, Me.cmbCityTo.SelectedItem.ToString)) 
            End If 
        End Sub 
        Private Class TravelLengths 
            Private _CitysFrom As New List(Of String) 
            Public Property CitysFrom() As List(Of String) 
                Get 
                    Return _CitysFrom 
                End Get 
                Set(ByVal value As List(Of String)) 
                    _CitysFrom = value 
                End Set 
            End Property 
            Private _CitysTo As New List(Of String) 
            Public Property CitysTo() As List(Of String) 
                Get 
                    Return _CitysTo 
                End Get 
                Set(ByVal value As List(Of String)) 
                    _CitysTo = value 
                End Set 
            End Property 
            Private _Distances As New List(Of Integer) 
            Public Property Distances() As List(Of Integer) 
                Get 
                    Return _Distances 
                End Get 
                Set(ByVal value As List(Of Integer)) 
                    _Distances = value 
                End Set 
            End Property 
            Public Function GetDistance(ByVal CityFrom As String, ByVal CityTo As String) As Integer 
                Dim i As Integer = 0 
                Do While i <> Me.CitysFrom.Count - 1 
                    If (Me.CitysFrom.Item(i) = CityFrom AndAlso Me.CitysTo.Item(i) = CityTo) _ 
                                OrElse _ 
                       (Me.CitysFrom.Item(i) = CityTo AndAlso Me.CitysTo.Item(i) = CityFrom) _ 
                                Then 
                        Return Me.Distances(i) 
                    End If 
                    i += 1 
                Loop 
            End Function 
            Public Sub Add(ByVal CityFrom As String, ByVal CityTo As String, ByVal Distance As Integer) 
                Me.CitysFrom.Add(CityFrom) 
                Me.CitysTo.Add(CityTo) 
                Me.Distances.Add(Distance) 
            End Sub 
        End Class 
    End Class
    My text file looks like this:

    Code:
    London,Manchester,3214
    London,Edinburgh,494
    London,Cardiff,853
    Cardiff,Manchester,634
    It *IS* a little excessive since you said you only have 7 cities so you could hard code it in. But this way you could dynamically add city-to-city distances.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    thank you for your help, is there a way i could do it where my distance.txt file is as follows:
    648
    342
    565
    123

    and have it so if i select say london to manchester it selects the relevant line from the text file and displays it in thru the label?

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Using combo box and stream reader

    How will it know what line to pick?

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    maybe somehow assigning a value to each city that corresponds to the relevant line in the .text file or maybe using an if statement??? im not sure jus wondering if there was a way

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Using combo box and stream reader

    You mean for example: Line 1 of the text file (648) is London to Manchester? If so, then yes it is possible to do this as well. You would have to hard code it into your app though. (The relations)

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    ok so there is no way of doing it by using a text file?

  12. #12
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Using combo box and stream reader

    The only way I can think of doing it without coding it, would be to keep a 1:1 relation between the lines in the text file and the item in the combobox. For example:

    Item 1 in the combobox (London to Manchester) = Line 1 in the text file

    Like so:

    Code:
    Public Class Form1 
        Private FileCnts() As String 
        Public Sub New() 
            InitializeComponent() 
            With Me.cmbCities.Items 
                .Add("London to Manchester") 
                .Add("Birmingham to London") 
            End With 
            FileCnts = IO.File.ReadAllLines("C:\test\Distance.txt") 
        End Sub 
        Private Sub btnDistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDistance.Click 
            If Me.cmbCities.SelectedIndex <> -1 Then 
                MessageBox.Show(FileCnts(Me.cmbCities.SelectedIndex)) 
            End If 
        End Sub 
    End Class
    Text file looks like:

    Code:
    123
    456

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Re: Using combo box and stream reader

    ok thank you so much for your help and advice its much appriciated

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