Custom event not added to event section of properties panel

This is a discussion on Custom event not added to event section of properties panel within the C# Programming forums, part of the General Programming Boards category; This is visual c# so I'm not sure if this thread should go here. I am attempting to add a ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    Custom event not added to event section of properties panel

    This is visual c# so I'm not sure if this thread should go here.

    I am attempting to add a custom event to the event section of properties panel for my custom control. The problem is no event is being added to the panel for me to subscribe to.

    I attempted to create the event with,
    Code:
    public event System.EventHandler PositionChanged;
    and then built the solution but still no event appeared in the panel.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace LabelTextBox
    {    
        public partial class ctlLabelTextbox : UserControl
        {
            public enum PositionEnum
            {
                Right,
                Below
            }
    
            private PositionEnum position = PositionEnum.Right;
            private int textboxMargin = 0;
            private string labelText = "";
    
            public event System.EventHandler PositionChanged;
    
            public string LabelText
            {
                get { return labelCaption.Text; }
                set
                {
                    labelCaption.Text = labelText = value;
                    MoveControls();
                }
            }
            
            public string TextboxText
            {
                get { return textBoxText.Text; }
                set
                {
                    textBoxText.Text = value;
                }
            }
            
            public PositionEnum Position
            {
                get { return position; }
                set
                {
                    position = value;
                    MoveControls();
                }
            }
    
            public int TextboxMargin
            {
                get { return textboxMargin; }
                set
                {
                    textboxMargin = value;
                    MoveControls();
                    if (PositionChanged != null)
                        PositionChanged(this, new EventArgs());
                }
            }
    
            public ctlLabelTextbox()
            {
                InitializeComponent();
            }
    
    
    
            private void ctlLabelTextbox_Load(object sender, EventArgs e)
            {
                labelCaption.Text = labelText;
                Height = textBoxText.Height > labelCaption.Height ? textBoxText.Height : labelCaption.Height;
                MoveControls();
            }
    
            private void ctlLabelTextbox_SizeChanged(object sender, EventArgs e)
            {
                MoveControls();
            }
    
            private void MoveControls()
            {
                switch (position)
                {
                    case PositionEnum.Below:
                        textBoxText.Top = labelCaption.Bottom;
                        textBoxText.Left = labelCaption.Left;
                        textBoxText.Width = Width;
                        Height = textBoxText.Height + labelCaption.Height;
                        break;
                    case PositionEnum.Right:
                        textBoxText.Top = labelCaption.Top;
                        if (textboxMargin == 0)
                        {
                            int width = Width - labelCaption.Width - 3;
                            textBoxText.Left = labelCaption.Right + 3;
                            textBoxText.Width = width;
                        }
                        else
                        {
                            textBoxText.Left = textboxMargin + labelCaption.Width;
                            textBoxText.Width = Width - textBoxText.Left;
                        }
                        Height = textBoxText.Height > labelCaption.Height ?
                        textBoxText.Height : labelCaption.Height;
                        break;
                }
            }
    
            private void textBoxText_KeyDown(object sender, KeyEventArgs e)
            {
                OnKeyDown(e);
            }
    
            private void textBoxText_KeyPress(object sender, KeyPressEventArgs e)
            {
                OnKeyPress(e);
            }
    
            private void textBoxText_KeyUp(object sender, KeyEventArgs e)
            {
                OnKeyUp(e);
            }
        }
    }
    I downloaded the source code for this since it is from a book and the source code didn't have the a event handler for the event and the event wasn't in the event section of properties for the custom control.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Add the Browsable-attribute to the event
    BrowsableAttribute Class (System.ComponentModel)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    I have changed
    Code:
    public event System.EventHandler PositionChanged;
    to
    Code:
    [field:Browsable(true)]
    public event System.EventHandler PositionChanged;
    I still don't see the event under the events panel. Perhaps I am looking for the wrong event name (PositionChanged) in the events panel?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write custom file properties in MSWindows
    By soJM in forum Windows Programming
    Replies: 0
    Last Post: 09-16-2010, 03:01 AM
  2. event log name
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-26-2008, 02:14 AM
  3. Intercept mouse event and change to different event
    By ByThaBay in forum Windows Programming
    Replies: 1
    Last Post: 03-19-2008, 05:44 PM
  4. ignored event
    By hannibar in forum Windows Programming
    Replies: 0
    Last Post: 10-12-2005, 04:21 AM
  5. event question
    By MicroFiend in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21