1. 程式人生 > 其它 >C# WinForm 獲取帶有焦點的控制元件

C# WinForm 獲取帶有焦點的控制元件

1 ContainerControl.ActiveControl 屬性
2 
3 獲取或設定容器控制元件上的活動控制元件。
4 
5 名稱空間:   System.Windows.Forms

示例:

 1 namespace _10_獲取帶有焦點的控制元件
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的設計器變數。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null
; 9 10 /// <summary> 11 /// 清理所有正在使用的資源。 12 /// </summary> 13 /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null
)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗體設計器生成的程式碼 24 25 /// <summary> 26 /// 設計器支援所需的方法 - 不要修改 27 /// 使用程式碼編輯器修改此方法的內容。 28 /// </summary> 29 private
void InitializeComponent() 30 { 31 this.components = new System.ComponentModel.Container(); 32 this.textBox1 = new System.Windows.Forms.TextBox(); 33 this.textBox2 = new System.Windows.Forms.TextBox(); 34 this.textBox3 = new System.Windows.Forms.TextBox(); 35 this.textBox4 = new System.Windows.Forms.TextBox(); 36 this.timer1 = new System.Windows.Forms.Timer(this.components); 37 this.SuspendLayout(); 38 // 39 // textBox1 40 // 41 this.textBox1.Location = new System.Drawing.Point(94, 41); 42 this.textBox1.Name = "textBox1"; 43 this.textBox1.Size = new System.Drawing.Size(159, 21); 44 this.textBox1.TabIndex = 1; 45 // 46 // textBox2 47 // 48 this.textBox2.Location = new System.Drawing.Point(94, 83); 49 this.textBox2.Name = "textBox2"; 50 this.textBox2.Size = new System.Drawing.Size(159, 21); 51 this.textBox2.TabIndex = 1; 52 // 53 // textBox3 54 // 55 this.textBox3.Location = new System.Drawing.Point(94, 136); 56 this.textBox3.Name = "textBox3"; 57 this.textBox3.Size = new System.Drawing.Size(159, 21); 58 this.textBox3.TabIndex = 1; 59 // 60 // textBox4 61 // 62 this.textBox4.Location = new System.Drawing.Point(94, 203); 63 this.textBox4.Name = "textBox4"; 64 this.textBox4.Size = new System.Drawing.Size(159, 21); 65 this.textBox4.TabIndex = 1; 66 // 67 // timer1 68 // 69 this.timer1.Enabled = true; 70 this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 71 // 72 // Form1 73 // 74 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 75 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 76 this.ClientSize = new System.Drawing.Size(352, 293); 77 this.Controls.Add(this.textBox4); 78 this.Controls.Add(this.textBox3); 79 this.Controls.Add(this.textBox2); 80 this.Controls.Add(this.textBox1); 81 this.Name = "Form1"; 82 this.Text = "Form1"; 83 this.ResumeLayout(false); 84 this.PerformLayout(); 85 86 } 87 88 #endregion 89 90 private System.Windows.Forms.TextBox textBox1; 91 private System.Windows.Forms.TextBox textBox2; 92 private System.Windows.Forms.TextBox textBox3; 93 private System.Windows.Forms.TextBox textBox4; 94 private System.Windows.Forms.Timer timer1; 95 } 96 }
View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _10_獲取帶有焦點的控制元件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Console.WriteLine(this.ActiveControl.Name);
        }
    }
}
View Code