Csharp/C Sharp by API/System.Windows.Forms/LinkLabel

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

LinkLabel.ImageAlign

<source lang="csharp">


using System; using System.Drawing; using System.Windows.Forms; using System.Collections; public class ImageLists : Form {

 ImageList imgList;
 Label lbl;
 LinkLabel lnk;
 Button btn;
 public ImageLists()
 {
      Size = new Size(300,300);
   imgList = new ImageList();
   Image img;
   String[] arFiles = {"1.ico","2.ico","3.ico"};
   for (int i = 0; i < arFiles.Length; i++)
   {
     img = Image.FromFile(arFiles[i]);
     imgList.Images.Add(img);
   }
   imgList.ImageSize = new Size(32, 32);
   lbl = new Label();
      lbl.Parent = this;
      lbl.Text = "Label";
      lbl.Location = new Point(0,0);
      lbl.Size = new Size(lbl.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lbl.BorderStyle = BorderStyle.Fixed3D;
   lbl.ImageList = imgList;
   lbl.ImageIndex = 0;
   lbl.ImageAlign = ContentAlignment.MiddleRight;
      int yDelta = lbl.Height + 10;
    
      lnk = new LinkLabel();
      lnk.Parent = this;
      lnk.Text = "LinkLabel";
      lnk.Size = new Size(lnk.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lnk.Location = new Point(0, yDelta);
   lnk.ImageList = imgList;
   lnk.ImageIndex = 0;
   lnk.ImageAlign = ContentAlignment.MiddleRight;
   btn = new Button();
   btn.Parent = this;
   btn.ImageList = imgList;
   btn.ImageIndex = imgList.Images.Count - 1;
   btn.Location = new Point(0, 2 * yDelta);
   btn.Size = new Size(3 * imgList.ImageSize.Width, 
             2 * imgList.ImageSize.Height);
   lbl.ImageIndex = 1;
   lnk.ImageIndex = 0;
   btn.ImageIndex = 2;
 }
 static void Main() 
 {
     Application.Run(new ImageLists());
 }

}


 </source>


LinkLabel.ImageIndex

<source lang="csharp">

using System; using System.Drawing; using System.Windows.Forms; using System.Collections; public class ImageLists : Form {

 ImageList imgList;
 Label lbl;
 LinkLabel lnk;
 Button btn;
 public ImageLists()
 {
      Size = new Size(300,300);
   imgList = new ImageList();
   Image img;
   String[] arFiles = {"1.ico","2.ico","3.ico"};
   for (int i = 0; i < arFiles.Length; i++)
   {
     img = Image.FromFile(arFiles[i]);
     imgList.Images.Add(img);
   }
   imgList.ImageSize = new Size(32, 32);
   lbl = new Label();
      lbl.Parent = this;
      lbl.Text = "Label";
      lbl.Location = new Point(0,0);
      lbl.Size = new Size(lbl.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lbl.BorderStyle = BorderStyle.Fixed3D;
   lbl.ImageList = imgList;
   lbl.ImageIndex = 0;
   lbl.ImageAlign = ContentAlignment.MiddleRight;
      int yDelta = lbl.Height + 10;
    
      lnk = new LinkLabel();
      lnk.Parent = this;
      lnk.Text = "LinkLabel";
      lnk.Size = new Size(lnk.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lnk.Location = new Point(0, yDelta);
   lnk.ImageList = imgList;
   lnk.ImageIndex = 0;
   lnk.ImageAlign = ContentAlignment.MiddleRight;
   btn = new Button();
   btn.Parent = this;
   btn.ImageList = imgList;
   btn.ImageIndex = imgList.Images.Count - 1;
   btn.Location = new Point(0, 2 * yDelta);
   btn.Size = new Size(3 * imgList.ImageSize.Width, 
             2 * imgList.ImageSize.Height);
   lbl.ImageIndex = 1;
   lnk.ImageIndex = 0;
   btn.ImageIndex = 2;
 }
 static void Main() 
 {
     Application.Run(new ImageLists());
 }

}


 </source>


LinkLabel.ImageList

<source lang="csharp">

using System; using System.Drawing; using System.Windows.Forms; using System.Collections; public class ImageLists : Form {

 ImageList imgList;
 Label lbl;
 LinkLabel lnk;
 Button btn;
 public ImageLists()
 {
      Size = new Size(300,300);
   imgList = new ImageList();
   Image img;
   String[] arFiles = {"1.ico","2.ico","3.ico"};
   for (int i = 0; i < arFiles.Length; i++)
   {
     img = Image.FromFile(arFiles[i]);
     imgList.Images.Add(img);
   }
   imgList.ImageSize = new Size(32, 32);
   lbl = new Label();
      lbl.Parent = this;
      lbl.Text = "Label";
      lbl.Location = new Point(0,0);
      lbl.Size = new Size(lbl.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lbl.BorderStyle = BorderStyle.Fixed3D;
   lbl.ImageList = imgList;
   lbl.ImageIndex = 0;
   lbl.ImageAlign = ContentAlignment.MiddleRight;
      int yDelta = lbl.Height + 10;
    
      lnk = new LinkLabel();
      lnk.Parent = this;
      lnk.Text = "LinkLabel";
      lnk.Size = new Size(lnk.PreferredWidth + imgList.ImageSize.Width, 
     imgList.ImageSize.Height + 10);
      lnk.Location = new Point(0, yDelta);
   lnk.ImageList = imgList;
   lnk.ImageIndex = 0;
   lnk.ImageAlign = ContentAlignment.MiddleRight;
   btn = new Button();
   btn.Parent = this;
   btn.ImageList = imgList;
   btn.ImageIndex = imgList.Images.Count - 1;
   btn.Location = new Point(0, 2 * yDelta);
   btn.Size = new Size(3 * imgList.ImageSize.Width, 
             2 * imgList.ImageSize.Height);
   lbl.ImageIndex = 1;
   lnk.ImageIndex = 0;
   btn.ImageIndex = 2;
 }
 static void Main() 
 {
     Application.Run(new ImageLists());
 }

}


 </source>


LinkLabel.LinkArea

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class LinkLabelTextPropertyLinkArea : Form {

   LinkLabel lnkMsft = new LinkLabel();
 public LinkLabelTextPropertyLinkArea(){
   Size = new Size(300,250);
 
   lnkMsft.Parent = this;
   lnkMsft.Text = "www.nfex.ru";
   lnkMsft.Location = new Point(0,0);
   lnkMsft.AutoSize = true;
   lnkMsft.BorderStyle = BorderStyle.None;
   lnkMsft.LinkArea = new LinkArea(4,9);
   lnkMsft.LinkClicked +=   new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkMsft_LinkClicked);
 }
 static void Main() 
 {
   Application.Run(new LinkLabelTextPropertyLinkArea());
 }
 private void lnkMsft_LinkClicked(object sender, 
                  LinkLabelLinkClickedEventArgs e)
 {
     lnkMsft.Links[lnkMsft.Links.IndexOf(e.Link)].Visited = true;
      System.Diagnostics.Process.Start(lnkMsft.Text);
 }

}

 </source>


LinkLabel.LinkBehavior

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class LinkLabelMoreLinks : Form {

 public LinkLabelMoreLinks(){
   Size = new Size(300,250);
 
   LinkLabel lnkMulti = new LinkLabel();
   lnkMulti.Parent = this;
   lnkMulti.Text = "nfex.ru";
   lnkMulti.Location = new Point(0,75);
   lnkMulti.AutoSize = true;
   lnkMulti.BorderStyle = BorderStyle.None;
   lnkMulti.LinkBehavior = LinkBehavior.HoverUnderline;
   lnkMulti.Links.Add(0,4,"www.nfex.ru");
   lnkMulti.Links.Add(6,5,"www.google.ru");
   lnkMulti.Links.Add(13,2, "www.vw.ru");
   lnkMulti.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkGeneric_LinkClicked);
 }
 static void Main() 
 {
   Application.Run(new LinkLabelMoreLinks());
 }
 private void lnkGeneric_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
   LinkLabel lnk = new LinkLabel();
   lnk = (LinkLabel)sender;
     lnk.Links[lnk.Links.IndexOf(e.Link)].Visited = true;
      System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
 }

}

 </source>


LinkLabel.LinkClicked

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class LinkLabelLinkToAFile : Form {

 public LinkLabelLinkToAFile(){
   Size = new Size(300,250);
 
   LinkLabel lnkTxtFile = new LinkLabel();
   lnkTxtFile.Parent = this;
   lnkTxtFile.Text = "Address";
   lnkTxtFile.Location = new Point(0,100);
   lnkTxtFile.AutoSize = true;
   lnkTxtFile.BorderStyle = BorderStyle.None;
   lnkTxtFile.LinkBehavior = LinkBehavior.NeverUnderline;
   lnkTxtFile.ActiveLinkColor = Color.Green;
   lnkTxtFile.LinkColor = Color.Red;
   lnkTxtFile.VisitedLinkColor = Color.Orange;
   lnkTxtFile.Links.Add(0,lnkTxtFile.Text.ToString().Length,@"c:\test.txt");
   lnkTxtFile.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkGeneric_LinkClicked);
 }
 static void Main() 
 {
   Application.Run(new LinkLabelLinkToAFile());
 }
 private void lnkGeneric_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
   LinkLabel lnk = new LinkLabel();
   lnk = (LinkLabel)sender;
     lnk.Links[lnk.Links.IndexOf(e.Link)].Visited = true;
      System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
 }

}

 </source>


LinkLabel.Links.Add

<source lang="csharp">

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

}


 </source>


LinkLabel.Links.IndexOf

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class LinkLabelLinkToAFile : Form {

 public LinkLabelLinkToAFile(){
   Size = new Size(300,250);
 
   LinkLabel lnkTxtFile = new LinkLabel();
   lnkTxtFile.Parent = this;
   lnkTxtFile.Text = "Address";
   lnkTxtFile.Location = new Point(0,100);
   lnkTxtFile.AutoSize = true;
   lnkTxtFile.BorderStyle = BorderStyle.None;
   lnkTxtFile.LinkBehavior = LinkBehavior.NeverUnderline;
   lnkTxtFile.ActiveLinkColor = Color.Green;
   lnkTxtFile.LinkColor = Color.Red;
   lnkTxtFile.VisitedLinkColor = Color.Orange;
   lnkTxtFile.Links.Add(0,lnkTxtFile.Text.ToString().Length,@"c:\test.txt");
   lnkTxtFile.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkGeneric_LinkClicked);
 }
 static void Main() 
 {
   Application.Run(new LinkLabelLinkToAFile());
 }
 private void lnkGeneric_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
   LinkLabel lnk = new LinkLabel();
   lnk = (LinkLabel)sender;
     lnk.Links[lnk.Links.IndexOf(e.Link)].Visited = true;
      System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
 }

}

 </source>


LinkLabel.LinkVisited

<source lang="csharp"> 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());
     }

}


 </source>