Link to home
Start Free TrialLog in
Avatar of Matanic
MatanicFlag for United States of America

asked on

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!!
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

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.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Matanic

ASKER

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!
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Matanic

ASKER

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