Nasıl yapılır: Windows Forms RichTextBox Denetimi ile Web Stili Bağlantılar Görüntüleme

Windows Forms RichTextBox denetimi, Web bağlantılarını renkli ve altı çizili olarak gösterebilirsiniz. Bağlantı tıklandıktan sonra bağlantı metninde belirtilen Web sitesini gösteren bir tarayıcı penceresi açan kodlar yazabilirsiniz.

  1. özelliğini Text geçerli bir URL içeren bir dizeye ayarlayın (örneğin, " https://www.microsoft.com/" ;).

  2. özelliğinin DetectUrls olarak (varsayılan) ayarlanmış true olduğundan emin olun.

  3. Nesnesinin yeni bir genel örneğini Process oluşturun.

  4. Tarayıcıya istenen metni LinkClicked gönderen olay için bir olay işleyicisi yazın.

    Aşağıdaki örnekte olay, LinkClicked denetimin özelliğinde Internet Explorer URL'ye bir örnek TextRichTextBox açar. Bu örnekte, denetime sahip bir form RichTextBox varsaydır.

    Önemli

    yöntemi çağrılırken, kodu yetersiz ayrıcalıklar nedeniyle kısmi güven bağlamında çalıştırdıysanız bir Process.StartSecurityException özel durumla karşılaşırsiniz. Daha fazla bilgi için, bkz. Code Access Security Basics.

    Public p As New System.Diagnostics.Process
    Private Sub RichTextBox1_LinkClicked _
       (ByVal sender As Object, ByVal e As _
       System.Windows.Forms.LinkClickedEventArgs) _
       Handles RichTextBox1.LinkClicked
          ' Call Process.Start method to open a browser
          ' with link text as URL.
          p = System.Diagnostics.Process.Start("IExplore.exe", e.LinkText)
    End Sub
    
    public System.Diagnostics.Process p = new System.Diagnostics.Process();
    
    private void richTextBox1_LinkClicked(object sender,
    System.Windows.Forms.LinkClickedEventArgs e)
    {
       // Call Process.Start method to open a browser
       // with link text as URL.
       p = System.Diagnostics.Process.Start("IExplore.exe", e.LinkText);
    }
    
    public:
       System::Diagnostics::Process ^ p;
    
    private:
       void richTextBox1_LinkClicked(System::Object ^  sender,
          System::Windows::Forms::LinkClickedEventArgs ^  e)
       {
          // Call Process.Start method to open a browser
          // with link text as URL.
          p = System::Diagnostics::Process::Start("IExplore.exe",
             e->LinkText);
       }
    

    (Visual C++) Form oluşturucusu p içinde aşağıdaki deyimini dahil edersiniz işlemi başlatmanız gerekir:

    p = gcnew System::Diagnostics::Process();
    

    (Visual C#, Visual C++) Olay işleyicisini kaydetmek için formun oluşturucus una aşağıdaki kodu ekleyin.

    this.richTextBox1.LinkClicked += new
       System.Windows.Forms.LinkClickedEventHandler
       (this.richTextBox1_LinkClicked);
    
    this->richTextBox1->LinkClicked += gcnew
       System::Windows::Forms::LinkClickedEventHandler
       (this, &Form1::richTextBox1_LinkClicked);
    

    Bu işlemle çalışmayı bitirdikten sonra oluşturduğunuz işlemi hemen durdurmak önemlidir. Yukarıda sunulan koda bakarak işlemi durdurmak için kodunuz şöyle olabilir:

    Public Sub StopWebProcess()
       p.Kill()
    End Sub
    
    public void StopWebProcess()
    {
       p.Kill();
    }
    
    public: void StopWebProcess()
    {
       p->Kill();
    }
    

Ayrıca bkz.