Csharp/CSharp Tutorial/GUI Windows Forms/Label

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

Add Image in an ImageList to a Label

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

Add image to Label

using System;        
using System.Drawing;
using System.Windows.Forms;
public class LabelImageAdding : Form
{
  Label lblEcho;
  TextBox txt;
  public LabelImageAdding()
  {
    Size = new Size(300,250);
    lblEcho = new Label();
    lblEcho.Parent = this;
    lblEcho.Text = "test";
    lblEcho.Location = new Point(0,0);
    lblEcho.AutoSize = true;
    lblEcho.BorderStyle = BorderStyle.Fixed3D;
    int yDelta = lblEcho.Height + 10;
    
    Image img = Image.FromFile("YourFile.bmp");
    Label lblImage = new Label();
    lblImage.Parent = this;
    lblImage.Location = new Point(250, 0);
    lblImage.Image = img;
    lblImage.Anchor = AnchorStyles.Top | AnchorStyles.Right;
    lblImage.Size = new Size(img.Width, img.Height);
    Label lblCaption = new Label();
    lblCaption.Parent = this;
    lblCaption.Text = "&Enter Text Here:";
    lblCaption.Size = new Size(lblCaption.PreferredWidth, lblCaption.PreferredHeight);
    lblCaption.Location = new Point(0, yDelta);
    lblCaption.BorderStyle = BorderStyle.FixedSingle;
    txt = new TextBox();
    txt.Parent = this;
    txt.Size = new Size(100,23);
    txt.Location = new Point(lblCaption.Width + 5, yDelta);
    txt.TextChanged += new System.EventHandler(txt_TextChanged);
  }
  static void Main() 
  {
    Application.Run(new LabelImageAdding());
  }
  private void txt_TextChanged(object sender, EventArgs e)
  {
    lblEcho.Text = txt.Text;
  }
}

Generic event for a Label

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class LabelWithGenericEvent : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label lblTitle;
  public LabelWithGenericEvent()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.lblTitle = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblTitle
    // 
    this.lblTitle.Location = new System.Drawing.Point(40, 24);
    this.lblTitle.Name = "lblTitle";
    this.lblTitle.Size = new System.Drawing.Size(150, 25);
    this.lblTitle.TabIndex = 0;
    this.lblTitle.Text = "Events Demonstrator";
    this.lblTitle.Click += new System.EventHandler(this.GenericEventHandler);
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(242, 173);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {  this.lblTitle});
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new LabelWithGenericEvent());
  }
  private void GenericEventHandler(object sender, EventArgs e)
  {
    MessageBox.Show("Generic event handler","Event Demo");
  }
}

Label Text Change

using System;
using System.Drawing;
using System.Windows.Forms;
public class LabelTextChange : Form
{
  Label lblEcho;
  TextBox txt;
  public LabelTextChange()
  {
    Size = new Size(300,250);
    lblEcho = new Label();
    lblEcho.Parent = this;
    lblEcho.Text = "test";
    lblEcho.Location = new Point(0,0);
    lblEcho.AutoSize = true;
    lblEcho.BorderStyle = BorderStyle.Fixed3D;
    int yDelta = lblEcho.Height + 10;
    
    Image img = Image.FromFile("YourFile.bmp");
    Label lblImage = new Label();
    lblImage.Parent = this;
    lblImage.Location = new Point(250, 0);
    lblImage.Image = img;
    lblImage.Anchor = AnchorStyles.Top | AnchorStyles.Right;
    lblImage.Size = new Size(img.Width, img.Height);
    Label lblCaption = new Label();
    lblCaption.Parent = this;
    lblCaption.Text = "&Enter Text Here:";
    lblCaption.Size = new Size(lblCaption.PreferredWidth, lblCaption.PreferredHeight);
    lblCaption.Location = new Point(0, yDelta);
    lblCaption.BorderStyle = BorderStyle.FixedSingle;
    txt = new TextBox();
    txt.Parent = this;
    txt.Size = new Size(100,23);
    txt.Location = new Point(lblCaption.Width + 5, yDelta);
    txt.TextChanged += new System.EventHandler(txt_TextChanged);
  }
  static void Main() 
  {
    Application.Run(new LabelTextChange());
  }
  private void txt_TextChanged(object sender, EventArgs e)
  {
    lblEcho.Text = txt.Text;
  }
}