2011-05-02 45 views
3

我是C#的新手,並且以前只使用JavaScript編寫程序,所以輕鬆點我!C#的新手問題 - 鏈接標籤和函數的問題

我寫了一個「app啓動程序」程序,它逐行讀取文本文件。每一行只是程序的一個路徑,例如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); 

...並且確實有效。但是也許我可以提出一個關於這條線的問題,因爲我發現語法有點不尋常和令人困惑。

是不是senderLinkLabel對象的屬性?所以要引用它,我們不應該使用LinkLabel.sender? (這將是更多的JavaScript的風格,我不明白(LinkLabel)sender符號!)

我也想不明白:

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

是什麼空間呢?如objectsender之間?或者System.EventArgs e? LinkClick是事件的名稱,但爲什麼我們在這裏有兩件事,用逗號分隔?你可以告訴我,我目前發現C#語法有點難!

預先感謝您。

+1

@user:歡迎StackOverflow上,但僅供參考,StackOverflow的是Q&A網站,而不是一個討論的論壇,所以我們不使用 「你好」 或 「謝謝」,或簽名。 – 2011-05-02 18:43:10

+0

@user你需要的是關於C#的教程。之後,事情應該對你更清楚。 – dandan78 2011-05-04 11:47:45

回答

3

您使用this.Text似乎至少是其中一個問題。

this指向您的班級的當前實例。你想要的是點擊了LinkLabel的實例。幸運的是,該事件的sender參數提供了此信息。

所以嘗試這樣的事情,而不是。

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

在這種情況下,「this.Text」指的是您的FORMS文本標題。用戶((LinkLabel)發件人).Text

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

此示例告訴您更好的實現方法。

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