How to make a Custom Textbox Controll caputure the Parent's clickEvent [modified]
-
i would like to know to capture 'any' click event outside the custom TextBox control. but not use lostfocus, mousehover, mouseout, etc, etc i stumbled over this, afterr seeing this in other control from this page. but they use it different. kind regards public partial class exTextBox : TextBox { protected exTextBox parent; public exTextBox Parent { get { return parent; } } public exTextBox() { InitializeComponent(); TextBoxParent(this.parent); } public void TextBoxParent(exTextBox parent) { parent.Click += new EventHandler(parent_Click); } void parent_Click(object sender, EventArgs e) { MessageBox.Show("click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } } -------------------------------------------------------------- errro: Object reference not set to an instance of an object. public exTextBox() { this.Parent.Click +=new EventHandler(parent_Click); } void parent_Click(object sender, EventArgs e) { MessageBox.Show("click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); }
Bad = knowing 2 much
modified on Wednesday, September 23, 2009 3:46 PM
-
i would like to know to capture 'any' click event outside the custom TextBox control. but not use lostfocus, mousehover, mouseout, etc, etc i stumbled over this, afterr seeing this in other control from this page. but they use it different. kind regards public partial class exTextBox : TextBox { protected exTextBox parent; public exTextBox Parent { get { return parent; } } public exTextBox() { InitializeComponent(); TextBoxParent(this.parent); } public void TextBoxParent(exTextBox parent) { parent.Click += new EventHandler(parent_Click); } void parent_Click(object sender, EventArgs e) { MessageBox.Show("click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } } -------------------------------------------------------------- errro: Object reference not set to an instance of an object. public exTextBox() { this.Parent.Click +=new EventHandler(parent_Click); } void parent_Click(object sender, EventArgs e) { MessageBox.Show("click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); }
Bad = knowing 2 much
modified on Wednesday, September 23, 2009 3:46 PM
The
Parent
property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overridedOnHandleCreated
method. This works for me.using System;
using System.Windows.Forms;namespace TestApp
{
public partial class Form1 : Form
{
ExtendedTextBox extendedTextBox1;public Form1() { InitializeComponent(); extendedTextBox1 = new ExtendedTextBox(); Controls.Add(extendedTextBox1); } } public class ExtendedTextBox : TextBox { public ExtendedTextBox() { } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); Parent.Click += new EventHandler(Parent\_Click); } void Parent\_Click(object sender, EventArgs e) { MessageBox.Show( "click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
}
Dave
Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
The
Parent
property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overridedOnHandleCreated
method. This works for me.using System;
using System.Windows.Forms;namespace TestApp
{
public partial class Form1 : Form
{
ExtendedTextBox extendedTextBox1;public Form1() { InitializeComponent(); extendedTextBox1 = new ExtendedTextBox(); Controls.Add(extendedTextBox1); } } public class ExtendedTextBox : TextBox { public ExtendedTextBox() { } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); Parent.Click += new EventHandler(Parent\_Click); } void Parent\_Click(object sender, EventArgs e) { MessageBox.Show( "click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
}
Dave
Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)hey, thanx indeed, i copy this to a new project, it works fine. but when i copy the following lines to my old project,it's doesn't. protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); Parent.Click += new EventHandler(Parent_Click); } void Parent_Click(object sender, EventArgs e) { MessageBox.Show( "click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } the extended Textbox class gets compiled to dll.
Bad = knowing 2 much
-
The
Parent
property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overridedOnHandleCreated
method. This works for me.using System;
using System.Windows.Forms;namespace TestApp
{
public partial class Form1 : Form
{
ExtendedTextBox extendedTextBox1;public Form1() { InitializeComponent(); extendedTextBox1 = new ExtendedTextBox(); Controls.Add(extendedTextBox1); } } public class ExtendedTextBox : TextBox { public ExtendedTextBox() { } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); Parent.Click += new EventHandler(Parent\_Click); } void Parent\_Click(object sender, EventArgs e) { MessageBox.Show( "click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
}
Dave
Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)this is the whole class somehow 'SendMessage(...)' blocks it. move the 'MessageBox' 2 lines higher @ 'protected override void OnHandleCreated' and it gets triggered. put it baack below and it doesn't public partial class exTextBox : TextBox { public exTextBox() { SendMessage(this.Handle, EM_SETCUEBANNER, 0, this.WaterMarkText); } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); //this.Parent.Click +=new EventHandler(Parent_Click); Parent.Click += new EventHandler(Parent_Click); // just for testing MessageBox.Show( "OnHandleCreated", "OnHandleCreated", MessageBoxButtons.OK, MessageBoxIcon.Information); } void Parent_Click(object sender, EventArgs e) { MessageBox.Show( "click", "click", MessageBoxButtons.OK, MessageBoxIcon.Information); } #region Attributes //[DefaultValue(typeof(String), "set the default value"), Category("Appearance"), Description("Set the default value.")] private const int EM_SETCUEBANNER = 0x1501; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SendMessage (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); private string _waterMarkText = "Water Mark"; public string WaterMarkText { get { return _waterMarkText; } set { SendMessage(this.Handle, EM_SETCUEBANNER, 0, value); _waterMarkText = value; Invalidate(); } } #endregion public bool notempty() { if ((this.TextLength > 0) && (this.Text != this.WaterMarkText)) { base.BackColor = Color.White; return true; } else { base.BackColor = Color.Red; this.Text = this.WaterMarkText; return false; } } }
Bad = knowing 2 much