C#新手-链接标签和函数的问题
我是C#的新手,以前只用JavaScript编写过程序,所以对我放松点 我写了一个“应用程序启动器”程序,逐行读取文本文件。每一行只是一个程序的路径,例如C:\Users\Jim\Desktop\Gravity.exe 到目前为止,我的程序可以成功地读取每一行并生成链接列表。正如预期的那样,每个链接都显示为路径本身 我的问题是,这些链接将无法工作。但是,如果它们都被赋予相同的固定路径,那么它们将工作。我希望每个链接都使用其.Text属性作为目标。(请参见下面我的代码中的注释“有效”和“无效”)。我得到的唯一错误是“找不到指定的文件” 我真的非常感谢在这方面的任何帮助,因为我发现C#比Javascript困难得多C#新手-链接标签和函数的问题,c#,.net,winforms,linklabel,C#,.net,Winforms,Linklabel,我是C#的新手,以前只用JavaScript编写过程序,所以对我放松点 我写了一个“应用程序启动器”程序,逐行读取文本文件。每一行只是一个程序的路径,例如C:\Users\Jim\Desktop\Gravity.exe 到目前为止,我的程序可以成功地读取每一行并生成链接列表。正如预期的那样,每个链接都显示为路径本身 我的问题是,这些链接将无法工作。但是,如果它们都被赋予相同的固定路径,那么它们将工作。我希望每个链接都使用其.Text属性作为目标。(请参见下面我的代码中的注释“有效”和“无效”)。
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
}
}
更新:
谢谢你们的评论,伙计们。我已将该行更改为:
Process.Start(((LinkLabel)sender).Text);
。。。这确实有效。但也许我可以问一个关于这行的问题,因为我发现语法有点不寻常和混乱
sender
不是LinkLabel
对象的属性吗?所以为了引用它,我们是否应该使用LinkLabel.sender
?(这将是更多的JavaScript风格!我不理解(LinkLabel)sender
符号)
我也不明白:
private void LinkClick(object sender, System.EventArgs e)
空格是什么意思?例如在对象
和发送方
之间?或者在System.EventArgs
e之间LinkClick
是事件的名称,但为什么这里有两个东西,用逗号分隔
正如你所知,我目前发现C#语法有点难学
提前谢谢。您使用此.Text似乎至少是其中一个问题
此
引用类的当前实例。您需要的是单击的LinkLabel
的实例。幸运的是,事件的sender
参数提供了这些信息
所以试试这样的方法吧
LinkLabel lnk = sender as LinkLabel;
System.Diagnostics.Process.Start(lnk.Text);
在该上下文中,“this.Text”指的是表单文本标题。用户((链接标签)发件人)。Text此示例告诉您实现此目的的更好方法
@用户:欢迎使用StackOverflow,但仅供参考,StackOverflow是一个问答网站,不是一个讨论论坛,所以我们不使用“嗨”、“谢谢”或签名。@用户您需要的是关于C#的教程。在那之后,事情对你来说应该更清楚。
private void LinkClick(object sender, System.EventArgs e)
{
LinkLabel ll = (LinkLabel)sender;
System.Diagnostics.Process.Start(ll.Text);
}
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);
}
}