Question How to read text of a text file and use it in an app?

hamzabil11

Member
Joined
Aug 23, 2014
Messages
9
Programming Experience
1-3
I need some help making a vos in visual basic .net. So far I am very confused. The vos is supposed to read a text file and then get info from it. Every text file will contain 3 lines of information for the vos. The 1st line will be the name of the custom app, the 2nd being the icon, and the 3rd being the executable path. The app is supposed to read all these types of text files in a special folder and then:

  1. create a picturebox and then set its picture to the 2nd line of text in the specified text file.
  2. create a label and place it just under the new picturebox, and then set its text to the 1st line in the text file
  3. add a panel to the form, and place the newly created picturebox and label into the panel.
  4. Add a onclick handler to the new panel, and set it to run the executable path specified in the 3rd line of code.
For example:
In the folder there are 2 text files, one being for a calculator app and the other for a text editor. when the vos starts up, it will find all the text files in that folder, and read them one by one, getting the info for them and creating the controls.
I have sort of got an idea for creating the picturebox, the panel and the label, but i am stuck on the onclick handler for the panel.


I do have some code:

VB.NET:
[COLOR=#222222][FONT=Verdana]Imports System.IO
[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]Module ReadApplicationsAtBoot[/FONT][/COLOR][/FONT][/COLOR]
    Dim AppPath As String = New System.IO.FileInfo(Windows.Forms.Application.ExecutablePath).DirectoryName
    Dim dir As New DirectoryInfo(AppPath & "\customapps")
    Dim allLines As List(Of String) = New List(Of String)
    Sub Checkforapps()
        For Each file As FileInfo In Dir.GetFiles()
            Dim read As New StreamReader(file.DirectoryName)
            Do While Not read.EndOfStream
                allLines.Add(read.ReadLine())
            Loop
            Dim apppic As String = ReadLine(2, allLines)
            Dim pan1 As New Panel
            Dim pic1 As New PictureBox
            pic1.Parent = pan1
            pic1.Image = Image.FromFile(apppic)
            pan1.Controls.Add(pic1)
            Dim lab1 As New Label
            lab1.Text = ReadLine(1, allLines)
            pan1.Controls.Add(lab1)
            Desktop.Controls.Add(pan1)
            AddHandler pan1.Click, AddressOf Appclick
        Next
    End Sub


    Public Function ReadLine(lineNumber As Integer, lines As List(Of String)) As String
        Return lines(lineNumber - 1)
    End Function


    Sub Appclick()
        'This is where i need some help, the below code does not work...
        Try
            Shell(ReadLine(3, allLines))
        Catch
            MessageBox.Show("An error has occured launching the application.")
        End Try
        'The problem with the code is that it only runs the app whose text file is first in the folder
    End Sub
End Module

The aim is that anyone can add apps for the vos, even games and stuff like microsoft word and windows media player, and anyone can use or create apps to run in the vos shell environment.
Any help would be very grateful,
hamzabil11
 
I would suggest that the first thing you should do is define your own user control for displaying the data. Don't create a PictureBox, a Label and a Panel each time; just create an instance of your user control. I would also suggest that you then add those controls to a FlowLayoutPanel or TableLayoutPanel, depending on the type of layout you want.

If you aren't aware, you add a user control to your project and design it in pretty much exactly the same way as a form and then use it in pretty much exactly the same way as any other control. You should start by adding a Picturebox and a Label to the user control in the appropriate layout. You would then add three properties to the user control for the three data items that you're reading from the file. One would simply pass the text through to the Text property of the Label, one would pass the text through to the ImageLocation property of the PictureBox and the third would store the path of the executable.

You would also add an event to the user control that indicated a click. Internally, you would handle the Click event of the PictureBox and then raise the event of your user control. It's up to you but I would suggest that you should handle the Click event of the Label as well. In the form's handler for your user control's event, you can then get the path of the executable for the user control that was clicked via the `sender` parameter.

As for reading the data, you should call File.ReadAllLines to read a file in a single line. It will return a String array and you can then simply assign the three elements of that array to the three properties of the user control.
 
Thanks so much, I was really confused. If it's not too much of a problem, can I also ask you how to Minimize and maximise other apps from my app.
Thanks again,
hamzabil11
 
Thanks so much, I was really confused. If it's not too much of a problem, can I also ask you how to Minimize and maximise other apps from my app.
Thanks again,
hamzabil11

One topic per thread and one thread per topic. If you have a new question on a new topic then create a new thread with a title that describes that topic and ask your question there. Keep this thread for any further questions you may on this topic as you implement the suggested solution.
 
Oh ok, it's all right. Thanks for the help anyways
hamzabil11
EDIT:
You would also add an event to the user control that indicated a click. Internally, you would handle the Click event of the PictureBox and then raise the event of your user control. It's up to you but I would suggest that you should handle the Click event of the Label as well. In the form's handler for your user control's event, you can then get the path of the executable for the user control that was clicked via the `sender` parameter.

I understand what you mean by the paragraphs before this one, but I am getting confused here. I am only a noob and I don't really get it when you said
"...you can then get the path of the executable for the user control that was clicked via the `sender` parameter.
I am the type of guy that would understand the problem better by analysing a code sample, if you could give one. but it's no problem if you can't.
Thanks,
hamzabil11
 
Last edited:
Back
Top