New to C# - problems with link labels and functions
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;
开发者_C百科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.
Your use of this.Text
appears to be at least one of the problems.
this
refers to the current instance of your class. What you want is the instance of the LinkLabel
that was clicked. Fortunately, the event's sender
argument provides this information.
So try something like this instead.
LinkLabel lnk = sender as LinkLabel;
System.Diagnostics.Process.Start(lnk.Text);
In that context "this.Text" refers to your FORMS text caption. User ((LinkLabel)sender).Text
private void LinkClick(object sender, System.EventArgs e)
{
LinkLabel ll = (LinkLabel)sender;
System.Diagnostics.Process.Start(ll.Text);
}
This example tells you better way to achieve this.
http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.linkclicked%28v=VS.100%29.aspx
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
// Determine which link was clicked within the LinkLabel.
this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
// Display the appropriate link based on the value of the
// LinkData property of the Link object.
string target = e.Link.LinkData as string;
// If the value looks like a URL, navigate to it.
// Otherwise, display it in a message box.
if(null != target && target.StartsWith("www"))
{
System.Diagnostics.Process.Start(target);
}
else
{
MessageBox.Show("Item clicked: " + target);
}
}
精彩评论