11 Replies - 734 Views - Last Post: 03 March 2013 - 03:30 AM Rate Topic: -----

#1 Z.K.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-May 08

User control click even on main form

Posted 01 March 2013 - 09:13 PM

I have a user control that I created and I need to be able to click on the button on the user control and have something happen on the main form. Now, I did this several years ago, but I don't remember how I did this now. I think I need to use a delegate, but I have searched the Internet without finding an exact answer. How to I create a click event for the user control on the form's code file so that it will cause say the background of the form to turn red. If you know a good tutorial site on the subject that would be great, but I have not found one yet.

:?:/>

Is This A Good Question/Topic? 0
  • +

Replies To: User control click even on main form

#2 Z.K.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-May 08

Re: User control click even on main form

Posted 01 March 2013 - 10:19 PM

Well, I got it kind of working, but now the button on the user control seems to fire twice. The code in the test project for the button displays a MessageBox, but it displays it twice.


User control code:
namespace WindowOptionsUserCtrl
{
    public partial class WindowOptionsUserControl : UserControl
    {
        public delegate void ButtonclickedEventHandler(object sender, EventArgs e);
        public event ButtonclickedEventHandler OnUserControlButtonclicked;

        public WindowOptionsUserControl()
        {
            InitializeComponent();
            btnApply.Click += new EventHandler(btnApply_Click);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (OnUserControlButtonclicked != null)
                OnUserControlButtonclicked(this, e);

            
        }
		
	}
	
}



Test interface code:
namespace usercontroltest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            
        }

        private void windowOptionsUserControl1_Load(object sender, EventArgs e)
        {
            
        }

        private void windowOptionsUserControl1_OnUserControlButtonclicked(object sender, EventArgs e)
        {
            
            MessageBox.Show("Clicked!");
        }

        
    }
}



Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,015
  • Joined: 12-June 08

Re: User control click even on main form

Posted 01 March 2013 - 10:19 PM

A simple "raiseevent" statement can be called and the main form capture it like any other event...
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: User control click even on main form

Posted 02 March 2013 - 04:19 AM

The message box would display twice if you had subscribed to the btnApply.Click event twice. I'm guessing that you double clicked on the button in the designer, which subscribed to the Click event, and then you manually added the subscription in the WindowOptionsUserControl constructor, thereby leaving you with two subscriptions. So, I suggest you try removing the subscription you added to the constructor.

Also, you could tidy your code up a bit here and there. For example, you don't have to define your own delegate, as there is a built in delegate - EventHandler<TEventArgs> - that will do the job.

Spoiler


See this tutorial for advice on this exact scenario.

This post has been edited by CodingSup3rnatur@l-360: 02 March 2013 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,745
  • Joined: 02-June 10

Re: User control click even on main form

Posted 02 March 2013 - 07:02 AM

[*]Q: ...get Form/class 'A' to make a change or talk to Form/class 'B'
Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 Z.K.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-May 08

Re: User control click even on main form

Posted 02 March 2013 - 12:24 PM

View Postmodi123_1, on 01 March 2013 - 10:19 PM, said:

A simple "raiseevent" statement can be called and the main form capture it like any other event...


I am not sure I quite understand. Raiseevent seems to be only a VB ability and does not seem to be available in C#.
Was This Post Helpful? 0
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: User control click even on main form

Posted 02 March 2013 - 12:38 PM

RaiseEvent statements are a VB.NET feature. The equivalent in C# is basically how you raised the event. I think modi123_1 just forgot what forum he was in (easily done when switching between forums) :).
Was This Post Helpful? 0
  • +
  • -

#8 Z.K.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-May 08

Re: User control click even on main form

Posted 02 March 2013 - 01:12 PM

View PostCodingSup3rnatur@l-360, on 02 March 2013 - 04:19 AM, said:

The message box would display twice if you had subscribed to the btnApply.Click event twice. I'm guessing that you double clicked on the button in the designer, which subscribed to the Click event, and then you manually added the subscription in the WindowOptionsUserControl constructor, thereby leaving you with two subscriptions. So, I suggest you try removing the subscription you added to the constructor.

Also, you could tidy your code up a bit here and there. For example, you don't have to define your own delegate, as there is a built in delegate - EventHandler<TEventArgs> - that will do the job.

Spoiler


See this tutorial for advice on this exact scenario.


Well, I removed the delegate like you said, but now nothing happens. The tutorial link you gave though looks good and I guess I will go through that first before I do anything else.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,745
  • Joined: 02-June 10

Re: User control click even on main form

Posted 02 March 2013 - 03:46 PM

View PostCodingSup3rnatur@l-360, on 02 March 2013 - 01:38 PM, said:

RaiseEvent statements are a VB.NET feature. The equivalent in C# is basically how you raised the event. I think modi123_1 just forgot what forum he was in (easily done when switching between forums) :)/>.


RaiseEvent also exists in C# - If you are working in WPF and not still back in WinForms.

Posted Image
Was This Post Helpful? 1
  • +
  • -

#10 Z.K.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-May 08

Re: User control click even on main form

Posted 02 March 2013 - 06:25 PM

View PosttlhIn`toq, on 02 March 2013 - 03:46 PM, said:

View PostCodingSup3rnatur@l-360, on 02 March 2013 - 01:38 PM, said:

RaiseEvent statements are a VB.NET feature. The equivalent in C# is basically how you raised the event. I think modi123_1 just forgot what forum he was in (easily done when switching between forums) :)/>/>/>.


RaiseEvent also exists in C# - If you are working in WPF and not still back in WinForms.

Posted Image


Yes, I am using WinForms. I found my answer though on a different forum at Event Handling

So, I revised my code somewhat as below and now it works.

user control:
namespace WindowOptionsUserCtrl
{
    public partial class WindowOptionsUserControl : UserControl
    {
        public event EventHandler UCButtonclicked;

        public WindowOptionsUserControl()
        {
            InitializeComponent();
            
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (UCButtonclicked != null)
                UCButtonclicked(this, e);
            
        }
    }
}



Main Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            
        }

        private void windowOptionsUserControl2_UCButtonclicked(object sender, EventArgs e)
        {
            MessageBox.Show("Clicked!");
        }
        
    }
}



Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,745
  • Joined: 02-June 10

Re: User control click even on main form

Posted 02 March 2013 - 06:42 PM

I'm happy you found your answer, even if it was on another site.
But its a shame we spend all that time making tutorials if people can't be bothered working them even when we provide them as an answer to their issue. (see post #3)
Was This Post Helpful? 0
  • +
  • -

#12 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: User control click even on main form

Posted 03 March 2013 - 03:30 AM

View PosttlhIn`toq, on 02 March 2013 - 10:46 PM, said:

RaiseEvent also exists in C# - If you are working in WPF and not still back in WinForms.


Good point.

Just to clear it up for anybody reading this, UIElement.RaiseEvent() is a library method that is also available in VB.NET WPF projects. It is used to raise WPF Routed Events, which are basically more advanced, extended versions of the standard CLR events (although, they aren't complete replacements for the standard CLR events by any means, they are just events that work better with UI control trees, and other WPF specific UI mechanisms).

UIElement.RaiseEvent() is completely separate to the VB.NET specific RaiseEvent statement language feature. The RaiseEvent statement is typically used to raise standard CLR events, but can be used to raise routed events if you override the default behavior of the event raising mechanism to call UIElement.RaiseEvent().

This post has been edited by CodingSup3rnatur@l-360: 03 March 2013 - 04:24 AM
Reason for edit:: Removed image from quote.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1