page contents

C# winform 控件是否已获取焦点

本文讲述了C# winform 控件是否已获取焦点!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-11-XIv2Q1uv63801eeb70c3d.jpg本文讲述了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);
        }
    }
}

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-11-25 09:48
  • 阅读 ( 785 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
王昭君
王昭君

209 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1038 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章