次の方法で共有


方法: Windows フォーム LinkLabel コントロールを使用してオブジェクトまたは Web ページにリンクする

Windows フォーム LinkLabel コントロールを使用すると、フォームに Web スタイルのリンクを作成できます。 リンクがクリックされると、リンクがアクセスされたことを示すようにその色を変更できます。 色の変更の詳細については、「方法: Windows フォーム LinkLabel コントロールの外観を変更する」を参照してください。

別のフォームへのリンク

  1. Text プロパティを適切なキャプションに設定します。

  2. LinkArea プロパティを設定して、キャプションのどの部分をリンクとして示するかを決定します。 表示方法は、リンク ラベルの外観関連のプロパティによって異なります。 LinkArea 値は、開始文字の位置と文字数の 2 つの数値を含む LinkArea オブジェクトによって表されます。 LinkArea プロパティは、[プロパティ] ウィンドウまたは次のような方法でコードで設定できます。

    ' In this code example, the link area has been set to begin
    ' at the first character and extend for eight characters.
    ' You may need to modify this based on the text entered in Step 1.
    LinkLabel1.LinkArea = New LinkArea(0, 8)
    
    // In this code example, the link area has been set to begin
    // at the first character and extend for eight characters.
    // You may need to modify this based on the text entered in Step 1.
    linkLabel1.LinkArea = new LinkArea(0,8);
    
    // In this code example, the link area has been set to begin
    // at the first character and extend for eight characters.
    // You may need to modify this based on the text entered in Step 1.
    linkLabel1->LinkArea = LinkArea(0,8);
    
  3. LinkClicked イベント ハンドラーで、Show メソッドを呼び出してプロジェクト内の別のフォームを開き、LinkVisited プロパティを trueに設定します。

    手記

    LinkLabelLinkClickedEventArgs クラスのインスタンスには、クリックされた LinkLabel コントロールへの参照が含まれているため、sender オブジェクトをキャストする必要はありません。

    Protected Sub LinkLabel1_LinkClicked(ByVal Sender As System.Object, _
       ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
       Handles LinkLabel1.LinkClicked
       ' Show another form.
       Dim f2 As New Form()
       f2.Show
       LinkLabel1.LinkVisited = True
    End Sub
    
    protected void linkLabel1_LinkClicked(object sender, System. Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
       // Show another form.
       Form f2 = new Form();
       f2.Show();
       linkLabel1.LinkVisited = true;
    }
    
    private:
       void linkLabel1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkLabelLinkClickedEventArgs ^  e)
       {
          // Show another form.
          Form ^ f2 = new Form();
          f2->Show();
          linkLabel1->LinkVisited = true;
       }
    

Web ページへのリンク

LinkLabel コントロールを使用して、既定のブラウザーで Web ページを表示することもできます。

  1. Text プロパティを適切なキャプションに設定します。

  2. LinkArea プロパティを設定して、キャプションのどの部分をリンクとして示するかを決定します。

  3. LinkClicked イベント ハンドラーで、例外処理ブロックの実行中に、LinkVisited プロパティを true に設定し、Start メソッドを使用して既定のブラウザーを URL で起動する 2 つ目のプロシージャを呼び出します。 Start メソッドを使用するには、System.Diagnostics 名前空間への参照を追加する必要があります。

    重要

    以下のコードが部分信頼環境 (共有ドライブなど) で実行されている場合、VisitLink メソッドが呼び出されると JIT コンパイラは失敗します。 System.Diagnostics.Process.Start ステートメントにより、リンク要求が失敗します。 VisitLink メソッドが呼び出されたときに例外をキャッチすることで、次のコードは JIT コンパイラが失敗した場合にエラーが正常に処理されることを保証します。

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
       Handles LinkLabel1.LinkClicked
       Try
          VisitLink()
       Catch ex As Exception
          ' The error message
          MessageBox.Show("Unable to open link that was clicked.")
       End Try
    End Sub
    
    Sub VisitLink()
       ' Change the color of the link text by setting LinkVisited
       ' to True.
       LinkLabel1.LinkVisited = True
       ' Call the Process.Start method to open the default browser
       ' with a URL:
       System.Diagnostics.Process.Start("http://www.microsoft.com")
    End Sub
    
    private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
       try
       {
          VisitLink();
       }
       catch (Exception ex )
       {
          MessageBox.Show("Unable to open link that was clicked.");
       }
    }
    
    private void VisitLink()
    {
       // Change the color of the link text by setting LinkVisited
       // to true.
       linkLabel1.LinkVisited = true;
       //Call the Process.Start method to open the default browser
       //with a URL:
       System.Diagnostics.Process.Start("http://www.microsoft.com");
    }
    
    private:
       void linkLabel1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkLabelLinkClickedEventArgs ^  e)
       {
          try
          {
             VisitLink();
          }
          catch (Exception ^ ex)
          {
             MessageBox::Show("Unable to open link that was clicked.");
          }
       }
    private:
       void VisitLink()
       {
          // Change the color of the link text by setting LinkVisited
          // to true.
          linkLabel1->LinkVisited = true;
          // Call the Process.Start method to open the default browser
          // with a URL:
          System::Diagnostics::Process::Start("http://www.microsoft.com");
       }
    

関連項目