Dear all

I am brand new to C# and have previously only written programs in Javascript, so go easy on me !

I have written an "app launcher" program which reads a text file line by line. Each line is just a path to a program e.g. C:\Users\Matt\Desktop\Gravity.exe

So far, my program can successfully read each line and produce a list of links. As intended, each link appears as the path itself.

The problem I am having is that these links will not work. However they WILL work if they are all just given the same fixed path. I would like each link to use its .Text property as the destination. (please see the comments "works" and "does not work" in my code below). The only error I get is "cannot find the file specified".

I would really appreciate any help on this as I am finding C a lot harder than Javascript !

Thank you

Matt

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;  //for reading a text file


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)   //on form load
        {

            int counter = 0;
            string line;
            string myfile = @"c:\users\matt\desktop\file.txt";

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(myfile);
            while ((line = file.ReadLine()) != null)
            {
                //MessageBox.Show(line);   //check whats on each line


                LinkLabel mylinklabel = new LinkLabel();        //LinkLabel tells us the type of the object   e.g.  string mystring ="hello";
                mylinklabel.Text = line;
                this.Controls.Add(mylinklabel);
                mylinklabel.Location = new Point(0, 30 + counter * 30);

                mylinklabel.Click += new System.EventHandler(LinkClick);

                
                counter++;
            }

            file.Close();

        }

        
        private void LinkClick(object sender, System.EventArgs e)
        {
            System.Diagnostics.Process.Start(this.Text);  //doesn't work
            //System.Diagnostics.Process.Start(@"C:\Users\Matt\Desktop\gravity.exe");   //works
        }
        
    }
}

this.Text refers to Form1.Text which is the title of the window. Try this:

System.Diagnostice.Process.Start(((LinkLabel)object).Text);

I didn't test it (nor have I ever used a LinkLabel before) but this should work.
This is why:

Events are all passed 2 objects - the sending object and event args.
The object is generic, but since we know it will always be a linklabel triggering this event, it is safe to assume that we can cast the object to a linklabel. To explicitly cast in c# you just need to put the type to cast to in brackets before the object to cast. The reason why there is 2 sets of brackets is to ensure that the object being referenced is in fact the casted version, not the generic object which will have no .Text property.

I hope this isn't too far over your head, I don't want to scare you away from the wonderful language of C#!

Hi Skatamatic

Thank you very much for your reply.

I have just tried this, with every confidence, but it did not work !

Visual C# Express points to the line that we have changed and gives me the error... Invalid Expression Term 'Object'. I also corrected the typo in typo in system.diagnostics ;-) (as I was lazy and copied and pasted yours !) but it still will not work.

I get the feeling that your idea is the way that this SHOULD be done, but perhaps there is a syntax error?

Thanks again

Matt

Ironic how I made a typo whilst pointing out a tpyo !

Lol i am an idiot.

Instead of the word 'object' use the word 'sender'

Fantastic, this works.

Clearly I need to read up on functions and objects etc. We don't have "senders" in Javascript.

Thanks again, and I shall endeavour to battle on with C# !

Matt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.