Csharp/C Sharp by API/System.Windows.Forms/Control — различия между версиями

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

Текущая версия на 12:09, 26 мая 2010

Control.GetType

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class CustomCheckBox: Form
{
     public static void Main()
     {
          Application.Run(new CustomCheckBox());
     }
     public CustomCheckBox()
     {
          int      cyText = Font.Height;
          int      cxText = cyText / 2;
          FontStyle[] afs = { FontStyle.Bold,      FontStyle.Italic, 
                              FontStyle.Underline, FontStyle.Strikeout };
   
          Label label    = new Label();
          label.Parent   = this;
          label.Text     = "Sample Text";
   
          for (int i = 0; i < 4; i++)
          {
               FontStyleCheckBox chkbox = new FontStyleCheckBox();
               chkbox.Parent = this;
               chkbox.Text = afs[i].ToString();
               chkbox.fontstyle = afs[i];
               chkbox.Location = new Point(2 * cxText, 
                                               (4 + 3 * i) * cyText / 2);
               chkbox.Size = new Size(12 * cxText, cyText);
               chkbox.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
          }
     }
     void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
     {
          FontStyle fs = 0;
          Label     label = null;
   
          for (int i = 0; i < Controls.Count; i++)
          {
               Control ctrl = Controls[i];
   
               if (ctrl.GetType() == typeof(Label))
                    label = (Label) ctrl;
               else if (ctrl.GetType() == typeof(FontStyleCheckBox))
                    if (((FontStyleCheckBox) ctrl).Checked)
                         fs |= ((FontStyleCheckBox) ctrl).fontstyle;
          }
          label.Font = new Font(label.Font, fs);
     }
}
class FontStyleCheckBox: CheckBox
{
     public FontStyle fontstyle;
}


Control.HasChildren

 
using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlParent : Form
{
  private Button btn;
  public ControlParent()
  {
    btn = new Button();
    btn.Location = new Point(50,50);
    btn.Size = new Size(100,23);
    btn.Text = "Relationships";
        //Controls.Add(btn);
    btn.Parent = this;
    MessageBox.Show("Button Parent:  " + btn.Parent.ToString() + "\n" +
      "Button HasChildren:  " + btn.HasChildren.ToString() + "\n" + 
      "TopLevelControl:  " + btn.TopLevelControl.ToString() + "\n" + 
      "Form HasChildren:  " + this.HasChildren.ToString() + "\n" + 
      "Form Controls Count:  " + this.Controls.Count.ToString(),
      "Button Relationships");
  }
  static void Main() 
  {
    Application.Run(new ControlParent());
  }
}


Control.Name

  

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
{
   Label Label1;
   TextBox TextBox1;
   Button Button1;
  public Form1()
  {
        InitializeComponent();
  }
   private void ctrlClick(System.Object sender, EventArgs e)
   {
     Control ctrl = (Control)sender;
     MessageBox.Show("You clicked: " + ctrl.Name);
   }
  private void InitializeComponent()
  {
    this.Label1 = new System.Windows.Forms.Label();
    this.TextBox1 = new System.Windows.Forms.TextBox();
    this.Button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // Label1
    // 
    this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Label1.Location = new System.Drawing.Point(14, 97);
    this.Label1.Name = "Label1";
    this.Label1.Size = new System.Drawing.Size(112, 24);
    this.Label1.TabIndex = 8;
    this.Label1.Text = "Label1";
    this.Label1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // TextBox1
    // 
    this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.TextBox1.Location = new System.Drawing.Point(12, 61);
    this.TextBox1.Name = "TextBox1";
    this.TextBox1.Size = new System.Drawing.Size(156, 21);
    this.TextBox1.TabIndex = 7;
    this.TextBox1.Text = "TextBox1";
    this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Button1
    // 
    this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Button1.Location = new System.Drawing.Point(12, 21);
    this.Button1.Name = "Button1";
    this.Button1.Size = new System.Drawing.Size(96, 28);
    this.Button1.TabIndex = 6;
    this.Button1.Text = "Button1";
    this.Button1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(367, 281);
    this.Controls.Add(this.Label1);
    this.Controls.Add(this.TextBox1);
    this.Controls.Add(this.Button1);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Control Medley";
    this.ResumeLayout(false);
    this.PerformLayout();
  }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


Control.Parent

 
using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlParent : Form
{
  private Button btn;
  public ControlParent()
  {
    btn = new Button();
    btn.Location = new Point(50,50);
    btn.Size = new Size(100,23);
    btn.Text = "Relationships";
        //Controls.Add(btn);
    btn.Parent = this;
    MessageBox.Show("Button Parent:  " + btn.Parent.ToString() + "\n" +
      "Button HasChildren:  " + btn.HasChildren.ToString() + "\n" + 
      "TopLevelControl:  " + btn.TopLevelControl.ToString() + "\n" + 
      "Form HasChildren:  " + this.HasChildren.ToString() + "\n" + 
      "Form Controls Count:  " + this.Controls.Count.ToString(),
      "Button Relationships");
  }
  static void Main() 
  {
    Application.Run(new ControlParent());
  }
}


Control.TopLevelControl

 
using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlParent : Form
{
  private Button btn;
  public ControlParent()
  {
    btn = new Button();
    btn.Location = new Point(50,50);
    btn.Size = new Size(100,23);
    btn.Text = "Relationships";
        //Controls.Add(btn);
    btn.Parent = this;
    MessageBox.Show("Button Parent:  " + btn.Parent.ToString() + "\n" +
      "Button HasChildren:  " + btn.HasChildren.ToString() + "\n" + 
      "TopLevelControl:  " + btn.TopLevelControl.ToString() + "\n" + 
      "Form HasChildren:  " + this.HasChildren.ToString() + "\n" + 
      "Form Controls Count:  " + this.Controls.Count.ToString(),
      "Button Relationships");
  }
  static void Main() 
  {
    Application.Run(new ControlParent());
  }
}