I am new to C # and previously wrote only written programs in JavaScript, so it's easy on me!
I wrote an application launcher that reads a text file line by line. Each line is just the path to the program, for example. C: \ Users \ Jim \ Desktop \ Gravity.exe
So far, my program can successfully read each line and create a list of links. As expected, each link appears as a path.
The problem I am facing is that these links will not work. However, they will work if they all got the same fixed path. I would like each link to use the .Text property as the destination. (see 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 with this, as I find C # is much more complicated than Javascript!
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
int counter = 0;
string line;
string myfile = @"c:\users\matt\desktop\file.txt";
System.IO.StreamReader file = new System.IO.StreamReader(myfile);
while ((line = file.ReadLine()) != null)
{
LinkLabel mylinklabel = new LinkLabel();
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(@"C:\Users\Jim\Desktop\gravity.exe");
}
}
Update:
Thanks for your guys. I changed the corresponding line:
Process.Start(((LinkLabel)sender).Text);
... and it really works. But perhaps I could ask a question about this line, as I find the syntax a bit unusual and confusing.
sender LinkLabel? , , LinkLabel.sender? ( JavaScript! (LinkLabel)sender)
:
private void LinkClick(object sender, System.EventArgs e)
? , object sender? System.EventArgs e? LinkClick - , , ?
, # !
.