Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to make a Custom Textbox Controll caputure the Parent's clickEvent [modified]

How to make a Custom Textbox Controll caputure the Parent's clickEvent [modified]

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Demon
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • N Natural_Demon

      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

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      The Parent property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overrided OnHandleCreated 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)

      N 2 Replies Last reply
      0
      • D DaveyM69

        The Parent property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overrided OnHandleCreated 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)

        N Offline
        N Offline
        Natural_Demon
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D DaveyM69

          The Parent property isn't set until the handle for the control is created AFAIK. Try creating the event handler in an overrided OnHandleCreated 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)

          N Offline
          N Offline
          Natural_Demon
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups