User control click even on main form
Page 1 of 111 Replies - 232 Views - Last Post: 03 March 2013 - 03:30 AM
#1
User control click even on main form
Posted 01 March 2013 - 09:13 PM
Replies To: User control click even on main form
#2
Re: User control click even on main form
Posted 01 March 2013 - 10:19 PM
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!");
}
}
}
#3
Re: User control click even on main form
Posted 01 March 2013 - 10:19 PM
#4
Re: User control click even on main form
Posted 02 March 2013 - 04:19 AM
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.
See this tutorial for advice on this exact scenario.
This post has been edited by CodingSup3rnatur@l-360: 02 March 2013 - 07:00 AM
#5
Re: User control click even on main form
Posted 02 March 2013 - 07:02 AM
#6
Re: User control click even on main form
Posted 02 March 2013 - 12:24 PM
modi123_1, on 01 March 2013 - 10:19 PM, said:
I am not sure I quite understand. Raiseevent seems to be only a VB ability and does not seem to be available in C#.
#8
Re: User control click even on main form
Posted 02 March 2013 - 01:12 PM
CodingSup3rnatur@l-360, on 02 March 2013 - 04:19 AM, said:
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.
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.
#9
Re: User control click even on main form
Posted 02 March 2013 - 03:46 PM
CodingSup3rnatur@l-360, on 02 March 2013 - 01:38 PM, said:
RaiseEvent also exists in C# - If you are working in WPF and not still back in WinForms.
#10
Re: User control click even on main form
Posted 02 March 2013 - 06:25 PM
tlhIn`toq, on 02 March 2013 - 03:46 PM, said:
CodingSup3rnatur@l-360, on 02 March 2013 - 01:38 PM, said:
RaiseEvent also exists in C# - If you are working in WPF and not still back in WinForms.

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!");
}
}
}
#11
Re: User control click even on main form
Posted 02 March 2013 - 06:42 PM
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)
#12
Re: User control click even on main form
Posted 03 March 2013 - 03:30 AM
tlhIn`toq, on 02 March 2013 - 10:46 PM, said:
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.
|
|

New Topic/Question
Reply




MultiQuote








|