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 expose an event in user control

how to expose an event in user control

Scheduled Pinned Locked Moved C#
questionwinformstutorial
4 Posts 3 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
    nuttynibbles
    wrote on last edited by
    #1

    i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did. public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } } In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??

    realJSOPR D 2 Replies Last reply
    0
    • N nuttynibbles

      i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did. public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } } In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Well, your user control is private, so you'll have to implement a custom event that will reflect the button press back to the main form. Alternatively, just make the user control public, and then you can add an event to your mainform: myMain4.userControl1.myButton.Click += ...

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      1 Reply Last reply
      0
      • N nuttynibbles

        i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did. public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } } In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??

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

        Handle the Button's Click event in the user control and raise your own event:

        public partial class UserControl1 : UserControl
        {
            public event EventHandler Button1Click;
        
            public UserControl1()
            {
                InitializeComponent();
                button1.Click += new EventHandler(button1\_Click);
            }
        
            void button1\_Click(object sender, EventArgs e)
            {
                OnButton1Click(e);
            }
        
            protected virtual void OnButton1Click(EventArgs e)
            {
                EventHandler eh = Button1Click;
                if (eh != null)
                    eh(this, e);
            }
        }
        

        Now the new Button1Click event is available for the Form/Container and can be handled like any other event.

        Dave
        Tip: Passing values between objects using events (C#) 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 1 Reply Last reply
        0
        • D DaveyM69

          Handle the Button's Click event in the user control and raise your own event:

          public partial class UserControl1 : UserControl
          {
              public event EventHandler Button1Click;
          
              public UserControl1()
              {
                  InitializeComponent();
                  button1.Click += new EventHandler(button1\_Click);
              }
          
              void button1\_Click(object sender, EventArgs e)
              {
                  OnButton1Click(e);
              }
          
              protected virtual void OnButton1Click(EventArgs e)
              {
                  EventHandler eh = Button1Click;
                  if (eh != null)
                      eh(this, e);
              }
          }
          

          Now the new Button1Click event is available for the Form/Container and can be handled like any other event.

          Dave
          Tip: Passing values between objects using events (C#) 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
          nuttynibbles
          wrote on last edited by
          #4

          hey both. tks i shall try and get back again :-D

          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