I have a custom set of UserControls: NavigationBar and NavigationItem.
I'd like that whenever the user clicks anywhere in the NavigationItem, an event is fired. I don't know how to set this up.
http://i.stack.imgur.com/ocP2D.jpg
I've tried this:
public partial class NavigationBar : UserControl
{
public NavigationBar()
{
InitializeComponent();
SetupEvents();
}
public List<NavigationItem> NavigationItems { private get; set; }
public NavigationItem SelectedItem { get; set; }
private void SetupEvents()
{
navigationItem1.Click += new EventHandler(navigationItemClick);
}
void navigationItemClick(object sender, EventArgs e)
{
MessageBox.Show("Clicked on " + sender.ToString());
}
}
But that event only fires when the user specifically clicks on the NavigationItem control, but not when he clicks on the picture or text. (Those are PictureBox and Label).
What would be the best course of action? I'd like to create something well, not hacky code. Thanks!
Put something like this into your class:
public event EventHandler NavigationItemClick;
This creates a new event in your class named NavigationItemClick. The form designer will even see it.
In your method navigationItemClick you can do this to call the event.
EventHandler handler = this.NavigationItemClick;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
It is important to save the event into the handler variable to avoid race conditions. EventHandler is a delegate, so you call it like a method, hence the line in the if statement. The if itself makes sure that someone has attached to your event.
Related
I tried like this but it will return null values,
public event EventHandler<EventArgs> myevent;
public void Btn_Click(object sender, EventArgs e)
{
if (myevent!= null) //Here I get null value.
myevent(null, new EventArgs());
}
How to achieve the event fire?
Edit:
I have a UserControl in that user control which contain button event,inside the ButtonClick method I created this event.
I have a Form. In that form i m using this UserControl. So i need to Fire a event from User Control button click event to Form page particular function.
I wrote up a very simple, and basic solution for you. Please read the comments in the code to make sense of the solution. If anything is unclear, please ask questions.
Below sample, will cause the event to fire, if the person's name changes:
Here is the Person object:
public class Person
{
//Event to register to, when you want to capture name changed of person
public event EventHandler<NameChangedEventArgs> nameChangedEvent;
//Property
private string _name;
public string Name
{
get { return _name; }
set
{
this._name = value;
//Call the event. This will trigger the OnNameChangedEvent_Handler
OnNameChangedEvent_Handler(new NameChangedEventArgs() {NewName = value});
}
}
private void OnNameChangedEvent_Handler(NameChangedEventArgs args)
{
//Check if event is null, if not, invoke it.
nameChangedEvent?.Invoke(this, args);
}
}
//Custom event arguments class. This is the argument type passed to your handler, that will contain the new values/changed values
public class NameChangedEventArgs : EventArgs
{
public string NewName { get; set; }
}
Here is the code that instantiates and uses the Person object:
class Program
{
static void Main(string[] args)
{
//Instantiate person object
var person = new Person();
//Give it a default name
person.Name = "Andrew";
//Register to the nameChangedEvent, and tell it what to do if the person's name changes
person.nameChangedEvent += (sender, nameChangedArgs) =>
{
Console.WriteLine(nameChangedArgs.NewName);
};
//This will trigger the nameChanged event.
person.Name = "NewAndrewName";
Console.ReadKey();
}
}
can you please try the below way of calling event :
public event EventHandler myevent;
myevent += new EventHandler(Button1_Click);
if (myevent != null) //Here I get null value.
myevent(1, new EventArgs());
I am trying to implement the MVP pattern into my WinForms project. However, the method 'Activate' in my Presenter that is subscribed to my 'ActivatedForm' event from my View, does not seem to fire when i load the form. I have tested it simply by printing someting in the 'Activate' method. Why is this not working properly?
I have posted my code below.
I think it has something to do with the fact that I am creating the Presenter with the concrete View even though the _view attribute is of the interface type 'IHomeScreenView'.
I know that the 'HomeScreenView_Activated' event occurs, because I have put a print in there too and that worked.
The 'ActivatedForm' event just always returns null there which means that nothing is subscribed to the event.
IHomeScreenView.cs
public interface IHomeScreenView
{
List<string> ExistingAssessments { get; set; }
event EventHandler<EventArgs> ActivatedForm;
event EventHandler<EventArgs> CreatingNewAssessment;
event EventHandler<EventArgs> AddingNewStandard;
event EventHandler<EventArgs> OpeningAssessment;
}
HomeScreenView.cs
public partial class HomeScreenView : Form, IHomeScreenView
{
private HomeScreenPresenter homeScreenPresenter;
public List<string> ExistingAssessments
{
get { return recentAssessments.Items.Cast<string>().ToList(); }
set { recentAssessments.DataSource = value; }
}
public event EventHandler<EventArgs> ActivatedForm;
public event EventHandler<EventArgs> CreatingNewAssessment;
public event EventHandler<EventArgs> AddingNewStandard;
public event EventHandler<EventArgs> OpeningAssessment;
// Initialize homescreen.
public HomeScreenView()
{
InitializeComponent();
}
// Fires the activating form event.
private void HomeScreenView_Activated(object sender, EventArgs e)
{
ActivatedForm?.Invoke(this, EventArgs.Empty);
}
HomeScreenPresenter.cs
public class HomeScreenPresenter
{
private IHomeScreenView _view;
private AssessmentsModel _assessmentsModel;
public HomeScreenPresenter(IHomeScreenView view)
{
_assessmentsModel = new AssessmentsModel();
_view = view;
_view.ActivatedForm += Activate;
_view.CreatingNewAssessment += CreateNewAssessment;
_view.AddingNewStandard += AddNewStandard;
_view.OpeningAssessment += OpenAssessment;
}
public void Activate(object sender, EventArgs e)
{
Debug.Print("hi");
HashSet<string> items = new HashSet<string>(_assessmentsModel.GetDataList("Assessments", "assessment_name"));
List<string> assessments = items.ToList();
_view.ExistingAssessments = assessments;
}
I hope someone can help, thank you.
The Form.Activated event is only fired when the form is visible. See the documentation.
When the application is active and has multiple forms, the active form is the form with the input focus. A form that is not visible cannot be the active form. The simplest way to activate a visible form is to click it or use an appropriate keyboard combination.
If your form is already visible when the presenter is being created, then the activated event has already fired. You could call Form.Activate() once the presenter is created and the eventhandler is hooked up.
I'm struggling to bubble an event correctly.
I have a master page with a user control, and a page that is a child of the master page.
The user control and the page share common data, so when the user control updates, it updates the apage and vice versa.
The user control exposed an event to the master page. This is the format I use.
outside of class:
public delegate void OfferBookmarkRemoved(int OfferID);
inside class:
public event OfferBookmarkRemoved OfferBookmarkRemoved;
protected void LV_Bookmarks_ItemCommand(object source, ListViewCommandEventArgs e)
{
if (e.CommandName == "RemoveOffer")
{
var offerId = (int)e.CommandArgument;
OnOfferBookmarkRemoved(offerId);
}
}
void OnOfferBookmarkRemoved(int offerId)
{
offerId.ThrowDefault("offerId");
if (OfferBookmarkRemoved != null)
{
OfferBookmarkRemoved(offerId);
}
}
Now this can be used in the master page ok. I don't do anything in the master page and want to expose the event so that the aspx page can use it, like this:
Master.OfferBookmarkRemoved += OnBookmarkRemoved;
void OnBookmarkRemoved(int offerId)
{
offerId.ThrowDefault("offerId");
OfferList1.UpdateBookmark(offerId);
}
So the missing bit is to listen for the event in the master and make it available to the page.
Can anyone help?
You need to define this event in the master page also like that:
public event EventHandler<OfferEventArgs> OfferBookmarkRemoved
{
add
{
userControl.OfferBookmarkRemoved += value;
}
remove
{
userControl.OfferBookmarkRemoved -= value;
}
}
This way any page that registers to the master event will be registered to the usercontrol event.
By the way, you are not following the event pattern. Your event should look like:
public class OfferEventArgs : EventArgs
{
public int OfferID { get; set; }
}
public event EventHandler<OfferEventArgs> OfferBookmarkRemoved;
and when invoked:
OfferBookmarkRemoved(new OfferEventArgs() { OfferID = offerId });
Can someone tell me how can I have a feature in my UserControl, that can let the host windowsform know what is the control is doing?
For example my usercontrol has a filebrowser, and if user uses this file browser to open a file I want in the statusstrip bar of my form to write "Loading file(s)".
Will this require using events? if so, how can I have a single event inside usercontrol to report anything it does (then I guess I have to call this event in all methods in the usercontrol).
Simple
Yes, expose an event on the user control that the Form can subscribe to. You should use the standard event pattern:
class MyUserControl : UserControl
{
public event EventHandler<EventArgs> FileOpened;
protected virtual void OnFileOpened(EventArgs e)
{
EventHandler<EventArgs> handler = FileOpened;
if (handler != null)
handler(this, e);
}
}
Then when the file is opened you call OnFileOpened(EventArgs.Empty) which fires the event.
With custom EventArgs
Now the Form probably needs to know what file was opened. You could expose a property on the user control that the Form can use to find out, or you can provide that information in your event like so:
public class FileOpenedEventArgs : EventArgs
{
private string filename;
public FileOpenedEventArgs(string filename)
{
this.filename = filename;
}
public string Filename { get { return filename; } }
}
class MyUserControl : UserControl
{
public event EventHandler<FileOpenedEventArgs> FileOpened;
protected virtual void OnFileOpened(FileOpenedEventArgs e)
{
EventHandler<FileOpenedEventArgs> handler = FileOpened;
if (handler != null)
handler(this, e);
}
}
Then you fire the event with OnFileOpened(new FileOpenedEventArgs(filename)).
Optimal
When you create an event handler public event delegate Name;, you are allocating storage for the delegate on your object. Objects (especially Controls) often have a huge number of events that are never subscribed to. That's a whole lot of allocated storage not being used. There's an optimization built into the framework in the form of a EventHandlerList. This handy object stores event handlers only when they are actually used. All System.Windows.Forms.Control objects derive from System.ComponentModel.Component and it already provides an (protected) EventHandlerList that you can access in your derived Control.
To use it, you first create a static object that uniquely identifies your event, and then you provide the add {} and remove {} methods manually. Like so:
class MyUserControl : UserControl
{
private static readonly object FileOpenedKey = new Object();
public event EventHandler<FileOpenedEventArgs> FileOpened
{
add { Events.AddHandler(FileOpenedKey, value); }
remove { Events.RemoveHandler(FileOpenedKey, value); }
}
protected virtual void OnFileOpened(FileOpenedEventArgs e)
{
var handler = (EventHandler<FileOpenedEventArgs>)Events[FileOpenedKey];
if (handler != null)
handler(this, e);
}
}
Yes, you will need to create an event and subscribe to it. One suggestion following the standard pattern for events:
enum ControlStatus {Idle, LoadingFile, ...}
class StatusChangedEventArgs : EventArgs
{
public ControlStatus Status {get; private set;}
public StatusChangedEventArgs(ControlStatus status)
: base()
{
this.Status = status;
}
}
partial class MyControl : UserControl
{
public ControlStatus Status {get; private set;}
public event EventHandler<StatusChangedEventArgs> StatusChanged;
protected virtual void OnStatusChanged(StatusChangedEventArgs e)
{
var hand = StatusChanged;
if(hand != null) hand(this, e);
}
void LoadFiles()
{
...
Status = ControlStatus.LoadingFiles;
OnStatusChanged(new StatusChangedEventArgs(this.Status));
...
Status = ControlStatus.Idle;
OnStatusChanged(new StatusChangedEventArgs(this.Status));
}
}
partial class MyHostWindowsForm : Form
{
public MyHostWindowsForm()
{
var ctl = new MyControl();
...
ctl.StatusChanged += ctl_StatusChanged;
}
void ctl_StatusChanged(object sender, StatusChangedEventArgs e)
{
switch(e.Status)
{
case ControlStatus.Idle:
statusStripBar.Text = null;
break;
case ControlStatus.LoadingFiles:
statusStripBar.Text = "Loading file(s)";
break;
...
}
}
}
I have a user control with several buttons, which need to take different actions depending on the class using it.
The problem is that I don't know how to implement those handlers because when using my user control from the final app I don't have direct access to the buttons to specify which handler handles which events.
How would you do that?
Another way to do this is to expose the events through events in your UserControl :
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event RoutedEventHandler Button1Click;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Button1Click != null) Button1Click(sender, e);
}
}
This gives your usercontrol a Button1Click event that hooks up to that button within your control.
I would create a command for each button and delegate for each "handler". Than you can expose delegates to the user (final app) and internally call them on Execute() method on the commands. Something like this:
public class MyControl : UserControl {
public ICommand FirstButtonCommand {
get;
set;
}
public ICommand SecondButtonCommand {
get;
set;
}
public Action OnExecuteFirst {
get;
set;
}
public Action OnExecuteSecond {
get;
set;
}
public MyControl() {
FirstButtonCommand = new MyCommand(OnExecuteFirst);
FirstButtonCommand = new MyCommand(OnExecuteSecond);
}
}
Of cource, "MyCommand" needs to implement ICommand. You also need to bind your commands to coresponding buttons. Hope this helps.