c#新知识-链接标签和函数的问题

本文关键字:标签 函数 问题 链接 新知识 知识 | 更新日期: 2023-09-27 17:54:08

我是c#的新手,以前只用JavaScript写过程序,所以请原谅我!

我写了一个"应用程序启动器"程序,它逐行读取文本文件。每一行只是一个程序的路径,例如C:'Users'Jim'Desktop'Gravity.exe

到目前为止,我的程序可以成功地读取每一行并生成一个链接列表。按照预期,每个链接都显示为路径本身。

我的问题是,这些链接将不工作。然而,他们将工作,如果他们都只是给相同的固定路径。我希望每个链接都使用它的. text属性作为目的地。(请参阅评论"工作"answers"不工作"在我的代码下面)。我得到的唯一错误是"不能找到指定的文件"。

我真的很感谢任何帮助,因为我发现c#比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
    }        
}

更新:

谢谢你的评论。我已将这一行修改为:

Process.Start(((LinkLabel)sender).Text); 

…它确实有效。但是也许我可以问一个关于这一行的问题,因为我发现语法有点不寻常和令人困惑。

sender不是LinkLabel对象的属性吗?所以要参考它,我们不应该用LinkLabel.sender吗?(这将是更JavaScript风格!我不懂(LinkLabel)sender符号)

我也不明白:

private void LinkClick(object sender, System.EventArgs e)

空格是什么意思?比如在objectsender之间?或者在 System.EventArgse之间?LinkClick是事件的名称,但为什么我们在这里有两个东西,用逗号分开?

你可以告诉我,我目前发现c#语法有点难!

提前感谢。

c#新知识-链接标签和函数的问题

您对this.Text的使用似乎至少是其中一个问题。

this引用类的当前实例。您需要的是单击的LinkLabel的实例。幸运的是,事件的sender参数提供了这些信息。

那就试试这样吧。

LinkLabel lnk = sender as LinkLabel;
System.Diagnostics.Process.Start(lnk.Text);

在这种情况下"this。"文本"指的是您的表单文本标题。用户((LinkLabel)发送者)。文本

private void LinkClick(object sender, System.EventArgs e)
{
    LinkLabel ll = (LinkLabel)sender;
    System.Diagnostics.Process.Start(ll.Text);
}

这个例子告诉你实现这个的更好方法。

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);
        }
    }