C# 新手 - 链接标签和函数的问题

发布于 2024-11-04 14:48:17 字数 2123 浏览 0 评论 0原文

我是 C# 新手,之前只用 JavaScript 编写过程序,所以请对我放心!

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

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

我遇到的问题是这些链接不起作用。然而,如果它们都被赋予相同的固定路径,它们就会起作用。我希望每个链接都使用其 .Text 属性作为目的地。 (请参阅下面我的代码中的注释“有效”和“无效”)。我得到的唯一错误是“找不到指定的文件”。

我真的很感激任何关于这方面的帮助,因为我发现 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.EventArgs 之间? LinkClick 是事件的名称,但是为什么我们这里有两个东西,用逗号分隔呢?

如您所知,我目前发现 C# 语法有点难!

先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奈何桥上唱咆哮 2024-11-11 14:48:17

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

this 指的是类的当前实例。您想要的是被单击的 LinkLabel 实例。幸运的是,事件的 sender 参数提供了此信息。

所以尝试这样的事情吧。

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

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);
女中豪杰 2024-11-11 14:48:17

在这种情况下,“this.Text”指的是您的 FORMS 文本标题。用户((LinkLabel)sender).Text

In that context "this.Text" refers to your FORMS text caption. User ((LinkLabel)sender).Text

梦冥 2024-11-11 14:48:17
private void LinkClick(object sender, System.EventArgs e)
{
    LinkLabel ll = (LinkLabel)sender;
    System.Diagnostics.Process.Start(ll.Text);
}
private void LinkClick(object sender, System.EventArgs e)
{
    LinkLabel ll = (LinkLabel)sender;
    System.Diagnostics.Process.Start(ll.Text);
}
再浓的妆也掩不了殇 2024-11-11 14:48:17

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

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

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文