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\Jim\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 !
public partial class Form1 : Form { 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) { //Process.Start(this.Text); //doesn't work Process.Start(@"C:\Users\Jim\Desktop\gravity.exe"); //works } } Update:
Thank you for your comments guys. I have changed the line in question to:
Process.Start(((LinkLabel)sender).Text); ... and it does indeed work. But perhaps I could ask a question about this line, as I am finding the syntax a little unusual and confusing.
Isn't sender a property of the LinkLabel object? So to reference it, shouldn't we use LinkLabel.sender? (this would be more JavaScript style ! I don't understand the (LinkLabel)sender notation)
I also do not understand:
private void LinkClick(object sender, System.EventArgs e) What does a space mean? Such as between object and sender? Or between System.EventArgs e? LinkClick is the name of the event, but why do we have two things here, separated by a comma?
As you can tell, I am currently finding the C# syntax a bit hard going!
Thank you in advance.