我有一个包含GroupBox1的表单,GroupBox1还包含Label1,TextBox1和Button1。 GroupBox1.Enables是假的! 我可以在TextBox1上捕获或模拟Mouse_Click吗?
答案 0 :(得分:2)
我最好发一个答案而不是发表评论。由于您禁用了其父项,因此您不会获得TextBox的任何事件。从技术上讲,您可以实现IMessageFilter接口来解决这个问题。但是你不要试图解决这个问题是非常重要的,这一切都是按设计进行的,你应该从不对点击一个禁用的控件感兴趣。
您的用户在一百万年内永远不会猜到这样的点击会做任何有用的事情。一个禁用的控件使非常明显点击它是毫无意义的。正如它应该。如果有任何点击,那可能是一个意外。您既不想实施事故,也不想暴露完全无法发现的代码功能。
答案 1 :(得分:1)
好的,我提出了一个解决方案,任何评论都表示赞赏,希望它有助于或激励他人!
using System;
using System.Windows.Forms;
using System.Drawing;
//$/t:winexe
//& RunInOwnWindow
namespace PowerAPP
{
public class MainForm : Form
{
#region Initialization
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private static void Main(string [] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
MainForm()
{
groupBox1 = new System.Windows.Forms.GroupBox();
button1 = new System.Windows.Forms.Button();
textBox1 = new System.Windows.Forms.TextBox();
label1 = new System.Windows.Forms.Label();
textBox2 = new System.Windows.Forms.TextBox();
label2 = new System.Windows.Forms.Label();
// button1
button1.Name = "Button1";
button1.Location = new System.Drawing.Point(101, 90);
button1.Size = new System.Drawing.Size(75, 24);
button1.Text = "button1";
// textBox1
textBox1.Name = "TextBox1";
textBox1.Location = new System.Drawing.Point(76, 25);
textBox1.Size = new System.Drawing.Size(100, 20);
// label1
label1.Name = "Label1";
label1.Bounds = new Rectangle(35, 22, 146, 28);
label1.BackColor = System.Drawing.Color.Yellow;
label1.Text = "label1";
// textBox2
textBox2.Name = "TextBox2";
textBox2.Location = new System.Drawing.Point(76, 55);
textBox2.Size = new System.Drawing.Size(100, 20);
// label2
label2.Name = "Label2";
label2.Bounds = new Rectangle(35, 52, 146, 28);
label2.BackColor = System.Drawing.Color.Yellow;
label2.Text = "label2";
groupBox1.Name = "GroupBox1";
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label1);
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(label2);
groupBox1.Location = new System.Drawing.Point(35, 34);
groupBox1.Size = new System.Drawing.Size(200, 128);
groupBox1.Text = "groupBox1";
// MainFORm
Name = "MainFOrm";
ClientSize = new System.Drawing.Size(292, 266);
Controls.Add(groupBox1);
Text = "Click Fields to include";
MouseClick += new System.Windows.Forms.MouseEventHandler(MainForm_MouseClick);
groupBox1.Enabled = false;
}
#endregion
private void MainForm_MouseClick(object sender, MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
Locate_Point_in_Control_Bounds(this, pt);
}
private void Locate_Point_in_Control_Bounds(Control ctl, Point pt)
{
Rectangle r;
if (ctl is Form || ctl.HasChildren)
{
foreach (Control c in ctl.Controls)
{
if (c.HasChildren) Locate_Point_in_Control_Bounds(c, pt);
r = c.Bounds;
r.Offset(ctl.Left, ctl.Top);
if (r.Contains(pt))
MessageBox.Show(c.Name);
}
}
else
{
r = ctl.Bounds;
r.Offset(ctl.Left, ctl.Top);
if (r.Contains(pt))
MessageBox.Show(ctl.Name);
}
}
}
}
答案 2 :(得分:0)
是的,您可以通过以下代码行轻松完成此操作:
private void button1_Click(object sender, EventArgs e)
{
//Disable the groupBox with textBox1 in it
groupBox1.Enabled = false;
//Simulate the Click on textBox1
textBox1_MouseClick(this, new MouseEventArgs(MouseButtons.Left, 1,0,0,0));
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Test");
}
上面显示的代码也可以在另一个控件中,我只使用了button1。
更新1: 如果组框已启用并不重要,如果您通过textBox1_MouseClick(...)直接调用事件,它始终会捕获事件。
问候