• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 591
  • Last Modified:

My first dumb questions

Hey all,

I have a vb project I am finishing and hope to sell to my industry, plastic molders and I am stuck on a few little things to make this a better user experence.

This question pertains to comboboxes that I have populated like this:

Private Sub IMM1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IMM1ToolStripMenuItem.Click


        ToolStripComboBox1.Items.Clear()
        For Each s As String In System.IO.Directory.GetFiles("C:\QPILABS\Machines\")
            ToolStripComboBox1.Items.Add(s)
        Next

    End Sub

Open in new window


That gets the entire file path in the combo box which I don't want but it worked really well with the streamreader:

    Public Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged

        Dim sr As New StreamReader((CType(ToolStripComboBox1.SelectedItem, String)))
        Dim file As String
        Dim lines() As String

        ' Reads machine data file to end
        file = sr.ReadToEnd
        ' Closes machine data file
        sr.Close()

        ' File into its lines
        lines = Split(file, vbCrLf)
        ' Clears memory
        file = ""

        ' Popluates form1.vb IMM1 textboxes and checkboxes
        If lines(9) = 0 Then textbox1.Text = lines(0)
        If lines(9) = 1 Then TextBox86.Text = lines(0)
        textbox2.Text = lines(1)
        TextBox3.Text = lines(2)

Open in new window


But as I said I want only the file name so I found this method of popluating the combobox(s):

   Public Sub IMM1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IMM1ToolStripMenuItem.Click, OpenToolStripMenuItem.Click


        ToolStripComboBox1.Items.Clear()
        Try
            ToolStripComboBox1.Items.Clear()
            For Each s As String In System.IO.Directory.GetFiles("C:\QPI\Machines\")

                ToolStripComboBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(s))
            Next
        Catch ex As Exception
            MsgBox("There are no Machine files to load")
        End Try

   End Sub

Open in new window


But that method does not give my streamreader a good file path to open and read so, how can I do this?

A little about me, total kludge hobby coder but I think I have done a fair job on this project. It builds and publishes fine, have several beta copies running with some friends in the business they report no issues with the app. They just want me to "clean it up" before I start to try to sell it. I don't plan to ever code another project, I will do the wire frames and watch someone else to the heavy lifting, this is not my gig so to speak.

Thanks all!!
0
Matanic
Asked:
Matanic
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
Miguel OzSoftware EngineerCommented:
Replace this line at ToolStripComboBox1_SelectedIndexChanged:
Dim sr As New StreamReader((CType(ToolStripComboBox1.SelectedItem, String)))
with:
Dim myFileName As String =
System.IO.Path.Combine( "C:\QPI\Machines", CType(ToolStripComboBox1.SelectedItem, String))
Dim sr As New StreamReader((myFileName ))

Note: I will suggest to define a field for : "C:\QPI\Machines\" so that  you get this value from your configuration file rather than hard- code the value all over your code.
0
 
käµfm³d 👽Commented:
Why not create a small class (or struct) that will hold the two parts, or perhaps the full path and just the filename? Then you can override the default ToString implementation to return the filename only for display purposes. When a user selects an item from the list, an actual instance of that class or struct you created (the one you originally added) will be returned. With this you can get the associated filename. For example:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each path In System.IO.Directory.GetFiles("C:\")
            Dim mfp As New MyFileParts() With {.Filename = System.IO.Path.GetFileName(path), .FilePath = path}

            ComboBox1.Items.Add(mfp)
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim mfp As MyFileParts = DirectCast(ComboBox1.SelectedItem, MyFileParts)

        MessageBox.Show("You selected the file: " & mfp.FilePath)
    End Sub
End Class

Public Structure MyFileParts
    Public Filename As String
    Public FilePath As String

    Public Overrides Function ToString() As String
        Return Me.Filename
    End Function
End Structure

Open in new window

0
 
MatanicAuthor Commented:
I was shown this to store to store the value of the extension

 ToolStripComboBox3.Tag = "C:\QPI\Processes\" 

Open in new window


And to add to use it

 Dim SelectedFilename3 As String = ToolStripComboBox3.Tag & CType(ToolStripComboBox3.SelectedItem, String) & ".txt"
 

Open in new window


One more down and a couple more to tackle. Thanks for the input I will save the ideas!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Miguel OzSoftware EngineerCommented:
I was thinking more on a field in the form class:
Public Class Form1
    Public FilePath As String = "C:\QPI\Processes" 

Open in new window

Usage:
Dim SelectedFilename3 As String = Path.Combine(FilePath, CType(ToolStripComboBox3CType(ToolStripComboBox3.SelectedItem, String) & ".txt")

Open in new window


This is more of post comment, to my answer, so that your code is more maintenable.
As you correctly points out , a developer (like me) later on can make further changes and performance improvements - but the most important thing is that your code is currently solviing your business user needs.

Good luck,

Miguel
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's a variation of kaufmed's approach using a List as a DataSource, which allows you to set the DisplayMember and ValueMember properties of the ComboBox:
Public Class Form1

    Private Sub IMM1ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IMM1ToolStripMenuItem.Click
        ToolStripComboBox1.ComboBox.DataSource = Nothing

        Dim data As New List(Of MyFileInfo)
        Dim path As String = "C:\Users\Mike\Documents" ' "C:\QPILABS\Machines\"
        For Each File As String In System.IO.Directory.GetFiles(path)
            data.Add(New MyFileInfo(File))
        Next

        ToolStripComboBox1.ComboBox.ValueMember = "FullName"
        ToolStripComboBox1.ComboBox.DisplayMember = "GetFileNameWithoutExtension"
        ToolStripComboBox1.ComboBox.DataSource = data
    End Sub

    Private Sub ToolStripComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
        If Not IsNothing(ToolStripComboBox1.ComboBox.SelectedValue) Then
            Dim FullName As String = ToolStripComboBox1.ComboBox.SelectedValue
            Debug.Print(FullName)
        End If
    End Sub

End Class

Public Class MyFileInfo

    Public Sub New(ByVal FullName As String)
        Me._FullName = FullName
    End Sub

    Public _FullName As String

    Public ReadOnly Property FullName As String
        Get
            Return _FullName
        End Get
    End Property

    Public ReadOnly Property GetFileNameWithoutExtension As String
        Get
            Return System.IO.Path.GetFileNameWithoutExtension(FullName)
        End Get
    End Property

End Class

Open in new window

0
 
käµfm³d 👽Commented:
I was shown this to store to store the value of the extension
The Tag property is handy for stuff like this. Just be warned:  in six months, when you haven't looked at the code once since the last deployment, and bug fix comes in, are you going to remember where you stored the root path? I'm not saying that using the Tag property is incorrect--I've used it myself for similar purposes. Just keep in mind that in addition to satisfying your business need, your code should be understandable by both yourself and other developers who might have to take up the maintenance of such code.
0
 
MatanicAuthor Commented:
I see value in this, there are two assigned or callable values for the combobox so to me it makes sence to use both of them.

I see tagging the combobox as me assigning a value to it that is needed because of the method I used to fullfill a need of a clean combobox. I would guess there was a better way to do that also.

To touch on kaufmed's last comment, you are right and I do add lots of notes in the code. There is 2917 lines in the code today, 944 are notes on function, design or why I did something.  My trail of breadcrumbs so to speak.

Thanks again
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now