Csharp/CSharp Tutorial/GUI Windows Forms/LinkLabel

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

Add Image in an ImageList to a LinkLabel

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

LinkLabels: Add a link

using System;
using System.Drawing;
using System.Windows.Forms;
public class LinkLabelAddLink : Form {
    LinkLabel lnkLA = new LinkLabel();
  public LinkLabelAddLink(){
    Size = new Size(300,250);
  
    lnkLA.Parent = this;
    lnkLA.Text = "nfex.ru";
    lnkLA.Location = new Point(0,25);
    lnkLA.AutoSize = true;
    lnkLA.BorderStyle = BorderStyle.None;
    lnkLA.Links.Add(0,7,"www.nfex.ru");
    lnkLA.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkLA_LinkClicked);
  }
  static void Main() 
  {
    Application.Run(new LinkLabelAddLink());
  }
  private void lnkLA_LinkClicked(object sender,LinkLabelLinkClickedEventArgs e)
  {
      lnkLA.LinkVisited = true;
       System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
  }
}

LinkLabels: link to text file

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

LinkLabels: multiple links and generic handler

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

LinkLabels: Text property and LinkArea

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

LinkLabels: use generic Add and generic handler

using System;
using System.Drawing;
using System.Windows.Forms;
public class LinkLabelGenericAddHandler : Form {
  public LinkLabelGenericAddHandler(){
    Size = new Size(300,250);
  
    LinkLabel lnkSterSol = new LinkLabel();
    lnkSterSol.Parent = this;
    lnkSterSol.Text = "www.nfex.ru";
    lnkSterSol.Location = new Point(0,50);
    lnkSterSol.AutoSize = true;
    lnkSterSol.BorderStyle = BorderStyle.None;
    lnkSterSol.Links.Add(0,lnkSterSol.Text.ToString().Length,"www.nfex.ru");
    lnkSterSol.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(lnkGeneric_LinkClicked);
  }
  static void Main() 
  {
    Application.Run(new LinkLabelGenericAddHandler());
  }
  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());
  }
}