Net Core教程

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

 

这篇关于C# WinForm 获取带有焦点的控件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!