Csharp/C Sharp/GUI Windows Form/LinkLabel

Материал из .Net Framework эксперт
Версия от 11:33, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

About Box Links

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace AboutBoxLinks
{
    /// <summary>
    /// Summary description for AboutBoxLinks.
    /// </summary>
    public class AboutBoxLinks : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.LinkLabel lnkBuy;
        internal System.Windows.Forms.LinkLabel lnkWebSite;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public AboutBoxLinks()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lnkBuy = new System.Windows.Forms.LinkLabel();
            this.lnkWebSite = new System.Windows.Forms.LinkLabel();
            this.SuspendLayout();
            // 
            // lnkBuy
            // 
            this.lnkBuy.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lnkBuy.LinkArea = new System.Windows.Forms.LinkArea(0, 0);
            this.lnkBuy.Location = new System.Drawing.Point(26, 79);
            this.lnkBuy.Name = "lnkBuy";
            this.lnkBuy.Size = new System.Drawing.Size(244, 48);
            this.lnkBuy.TabIndex = 5;
            this.lnkBuy.Text = "Buy it at Amazon.ru or Barnes and Noble.";
            this.lnkBuy.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkWebSite_LinkClicked);
            // 
            // lnkWebSite
            // 
            this.lnkWebSite.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lnkWebSite.LinkArea = new System.Windows.Forms.LinkArea(0, 0);
            this.lnkWebSite.Location = new System.Drawing.Point(26, 35);
            this.lnkWebSite.Name = "lnkWebSite";
            this.lnkWebSite.Size = new System.Drawing.Size(248, 36);
            this.lnkWebSite.TabIndex = 4;
            this.lnkWebSite.Text = "See www.prosetech.ru for more information.";
            this.lnkWebSite.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkWebSite_LinkClicked);
            // 
            // AboutBoxLinks
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(300, 162);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lnkBuy,
                                                                          this.lnkWebSite});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "AboutBoxLinks";
            this.Text = "Link Examples";
            this.Load += new System.EventHandler(this.AboutBoxLinks_Load);
            this.ResumeLayout(false);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new AboutBoxLinks());
        }
        private void AboutBoxLinks_Load(object sender, System.EventArgs e)
        {
            lnkBuy.Links.Add(10, 10, "http://www.amazon.ru");
            lnkBuy.Links.Add(24, 16, "http://www.bn.ru");
            lnkWebSite.Links.Add(4, 17, "http://www.prosetech.ru");
        }
        private void lnkWebSite_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            e.Link.Visited = true;
            System.Diagnostics.Process.Start((string)e.Link.LinkData);
        }
    }
}


Change LinkColor after it has been clicked

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
      private System.Windows.Forms.LinkLabel driveLinkLabel;
      private System.Windows.Forms.LinkLabel deitelLinkLabel;
      private System.Windows.Forms.LinkLabel notepadLinkLabel;
      
      public Form1() {
        InitializeComponent();
      }
      private void driveLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
        driveLinkLabel.LinkVisited = true;
        System.Diagnostics.Process.Start( @"C:\" );
      } 
      private void deitelLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
        deitelLinkLabel.LinkVisited = true;
        System.Diagnostics.Process.Start( "IExplore", "http://www.nfex.ru" );
      }
      private void notepadLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
         notepadLinkLabel.LinkVisited = true;
         System.Diagnostics.Process.Start( "notepad" );
      }
      private void InitializeComponent()
      {
         this.driveLinkLabel = new System.Windows.Forms.LinkLabel();
         this.deitelLinkLabel = new System.Windows.Forms.LinkLabel();
         this.notepadLinkLabel = new System.Windows.Forms.LinkLabel();
         this.SuspendLayout();
         // 
         // driveLinkLabel
         // 
         this.driveLinkLabel.AutoSize = true;
         this.driveLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.driveLinkLabel.Location = new System.Drawing.Point(28, 24);
         this.driveLinkLabel.Name = "driveLinkLabel";
         this.driveLinkLabel.Size = new System.Drawing.Size(133, 20);
         this.driveLinkLabel.TabIndex = 0;
         this.driveLinkLabel.TabStop = true;
         this.driveLinkLabel.Text = "Click to browse C:\\";
         this.driveLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.driveLinkLabel_LinkClicked);
         // 
         // deitelLinkLabel
         // 
         this.deitelLinkLabel.AutoSize = true;
         this.deitelLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.deitelLinkLabel.Location = new System.Drawing.Point(28, 68);
         this.deitelLinkLabel.Name = "deitelLinkLabel";
         this.deitelLinkLabel.Size = new System.Drawing.Size(198, 20);
         this.deitelLinkLabel.TabIndex = 1;
         this.deitelLinkLabel.TabStop = true;
         this.deitelLinkLabel.Text = "Click to visit www.nfex.ru";
         this.deitelLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.deitelLinkLabel_LinkClicked);
         // 
         // notepadLinkLabel
         // 
         this.notepadLinkLabel.AutoSize = true;
         this.notepadLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.notepadLinkLabel.Location = new System.Drawing.Point(28, 114);
         this.notepadLinkLabel.Name = "notepadLinkLabel";
         this.notepadLinkLabel.Size = new System.Drawing.Size(147, 20);
         this.notepadLinkLabel.TabIndex = 2;
         this.notepadLinkLabel.TabStop = true;
         this.notepadLinkLabel.Text = "Click to run Notepad";
         this.notepadLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.notepadLinkLabel_LinkClicked);
         // 
         // LinkLabelTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(261, 164);
         this.Controls.Add(this.notepadLinkLabel);
         this.Controls.Add(this.deitelLinkLabel);
         this.Controls.Add(this.driveLinkLabel);
         this.Name = "LinkLabelTestForm";
         this.Text = "LinkLabelTest";
         this.ResumeLayout(false);
         this.PerformLayout();
      }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}


LinkLabel Clicked event

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
      private System.Windows.Forms.LinkLabel driveLinkLabel;
      private System.Windows.Forms.LinkLabel deitelLinkLabel;
      private System.Windows.Forms.LinkLabel notepadLinkLabel;
      
      public Form1() {
        InitializeComponent();
      }
      private void driveLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
        driveLinkLabel.LinkVisited = true;
        System.Diagnostics.Process.Start( @"C:\" );
      } 
      private void deitelLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
        deitelLinkLabel.LinkVisited = true;
        System.Diagnostics.Process.Start( "IExplore", "http://www.nfex.ru" );
      }
      private void notepadLinkLabel_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
      {
         notepadLinkLabel.LinkVisited = true;
         System.Diagnostics.Process.Start( "notepad" );
      }
      private void InitializeComponent()
      {
         this.driveLinkLabel = new System.Windows.Forms.LinkLabel();
         this.deitelLinkLabel = new System.Windows.Forms.LinkLabel();
         this.notepadLinkLabel = new System.Windows.Forms.LinkLabel();
         this.SuspendLayout();
         // 
         // driveLinkLabel
         // 
         this.driveLinkLabel.AutoSize = true;
         this.driveLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.driveLinkLabel.Location = new System.Drawing.Point(28, 24);
         this.driveLinkLabel.Name = "driveLinkLabel";
         this.driveLinkLabel.Size = new System.Drawing.Size(133, 20);
         this.driveLinkLabel.TabIndex = 0;
         this.driveLinkLabel.TabStop = true;
         this.driveLinkLabel.Text = "Click to browse C:\\";
         this.driveLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.driveLinkLabel_LinkClicked);
         // 
         // deitelLinkLabel
         // 
         this.deitelLinkLabel.AutoSize = true;
         this.deitelLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.deitelLinkLabel.Location = new System.Drawing.Point(28, 68);
         this.deitelLinkLabel.Name = "deitelLinkLabel";
         this.deitelLinkLabel.Size = new System.Drawing.Size(198, 20);
         this.deitelLinkLabel.TabIndex = 1;
         this.deitelLinkLabel.TabStop = true;
         this.deitelLinkLabel.Text = "Click to visit www.nfex.ru";
         this.deitelLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.deitelLinkLabel_LinkClicked);
         // 
         // notepadLinkLabel
         // 
         this.notepadLinkLabel.AutoSize = true;
         this.notepadLinkLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.notepadLinkLabel.Location = new System.Drawing.Point(28, 114);
         this.notepadLinkLabel.Name = "notepadLinkLabel";
         this.notepadLinkLabel.Size = new System.Drawing.Size(147, 20);
         this.notepadLinkLabel.TabIndex = 2;
         this.notepadLinkLabel.TabStop = true;
         this.notepadLinkLabel.Text = "Click to run Notepad";
         this.notepadLinkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.notepadLinkLabel_LinkClicked);
         // 
         // LinkLabelTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(261, 164);
         this.Controls.Add(this.notepadLinkLabel);
         this.Controls.Add(this.deitelLinkLabel);
         this.Controls.Add(this.driveLinkLabel);
         this.Name = "LinkLabelTestForm";
         this.Text = "LinkLabelTest";
         this.ResumeLayout(false);
         this.PerformLayout();
      }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}