I'm trying to create an event handler, that when some button is tapped on Content Dialog, the main Page or the Page that has called the Content Dialog receive the event.
I have found some examples about it in Stackoverflow as in other forums, but the one I'm using to get what I've told is this one. I'm having some problemas to create the event raiser and because it's my first time trying to do something like that I'm a bit lost on the implementation.
This is my code:
someClass.cs
public class updatedDB
{
public enum updateType
{
Delete, Update, None
}
private updateType _updateType;
private bool _isUpdated;
private int _ID;
private bool IsUpdated
{
get { return _isUpdated; }
set
{
_isUpdated = value;
ValueChanged?.Invoke(value);
}
}
public void NotifyUpdate(int ID, updateType UpdateType)
{
if (UpdateType == updateType.None)
return;
else
{
IsUpdated = true;
_ID = ID;
_updateType = UpdateType;
}
}
public updateType GetUpdateType()
{
return _updateType;
}
public int GetUpdatedID()
{
return _ID;
}
public event ValueChangedEventHandler ValueChanged;
}
public delegate void ValueChangedEventHandler(bool value);
And this is the event handler in another .cs file (Page.cs):
public Page()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
UpdateEvent.ValueChanged += UpdateSpots;
}
private ValueChangedEventHandler UpdateSpots(bool value)
{
ValueChangedEventHandler handler = new ValueChangedEventHandler(value);
return handler;
}
Currently I have two part of this code underlined in red and I don't know how to correct them:
UpdateEvent.ValueChanged += **UpdateSpots**;
UpdateSpots is giving me "has an incorrect type of value " (translated to english from my VS GUI language).
And,
ValueChangedEventHandler handler = new ValueChangedEventHandler(**value**);
value is giving me "waiting name of method" error.
Thank you in advance.
Related
I have created various properties inside of a User Control, and have had great success with accessing and editing them. I'm now trying to set up events for a number of these to be raised when one of these properties is changed. I have tried the MSDN example code for doing this (see here), but it is giving me this error when I try to build the solution:
Severity Code Description Project File Line Suppression State
Error CS0079 The event 'AbilityScoreDisplay.AbilityTitleChanged' can only appear on the left hand side of += or -= DnD Character Sheet C:\Users\bradley beasley\Documents\Visual Studio 2019\Projects\DnD Character Sheet\DnD Character Sheet\AbilityScoreDisplay.Designer.cs 199 Active
Another issue that I am having is that I am struggling to figure out how to get that event to appear in the Visual Studio 2019 Designer Properties window.
Here is the code that I have added to the designer file:
namespace DnD_Character_Sheet
{
partial class AbilityScoreDisplay : UserControl
{
public string AbilityTitle
{
get
{
return AbiltyTitleLabel.Text;
}
set
{
AbiltyTitleLabel.Text = value;
Invalidate();
}
}
public int AbilityModifier
{
get
{
return Convert.ToInt32(AbilityModifierTextBox.Text);
}
private set
{
if (value >= 0) AbilityModifierTextBox.Text = String.Format("+{0}", value);
else AbilityModifierTextBox.Text = value.ToString();
Invalidate();
}
}
public int AbilityScore
{
get
{
return Convert.ToInt32(AbilityScoreLabel.Text);
}
set
{
AbilityModifier = (int)(Math.Floor((double)(value) / 2)) - 5;
Invalidate();
}
}
private EventHandler onAbilityTitleChanged { get; set; }
private EventHandler onAbilityScoreChanged { get; set; }
public event EventHandler AbilityTitleChanged
{
add
{
onAbilityTitleChanged += value;
}
remove
{
onAbilityTitleChanged -= value;
}
}
public event EventHandler AbilityScoreChanged
{
add
{
onAbilityScoreChanged += value;
}
remove
{
onAbilityScoreChanged -= value;
}
}
protected virtual void OnAbilityTitleChanged(EventArgs e)
{
AbilityTitleChanged?.Invoke(this, e);
}
protected virtual void OnAbilityScoreChanged(EventArgs e)
{
AbilityScoreChanged?.Invoke(this, e);
}
}
}
The aim is to enable an event to be raised whenever a property is changed so that it can do other stuff elsewhere in the form that the controls will be in. I'm fairly certain that I am missing some very important stuff, or that my code is not that effective at all, but I am learning this kind of code for the first time, and I have tried many different things that have just not worked.
Any help at all would be greatly appreciated :)
I think you are confusing a few concepts. Let's do it step by step.
First, you need to be able to track event handlers:
private EventHandler _onAbilityTitleChanged;
You expose this event through a public property:
public event EventHandler AbilityTitleChanged
{
add
{
_onAbilityTitleChanged += value;
}
remove
{
_onAbilityTitleChanged -= value;
}
}
Finally, you need to fire the event so that all subscribed handlers can react to it. You can do so when the title changes (setter):
public string AbilityTitle
{
get
{
return AbiltyTitleLabel.Text;
}
set
{
AbiltyTitleLabel.Text = value;
//Raising the event!
_onAbilityTitleChanged?.Invoke(this, new EventArgs());
}
}
Other classes can then subscribe to your event:
var control = new AbilityScoreDisplay();
control.AbilityTitleChanged += SomeHandlerForWhenTitleChanges;
private void SomeHandlerForWhenTitleChanges(object sender, EventArgs e)
{
//....
}
You might want to read up a bit on the INotifyPropertyChanged interface as well.
You typically do this by implementing INotifyPropertyChanged. This allows you to use one single event for all the properties. The property name is passed in the event arguments.
partial class AbilityScoreDisplay : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
...
}
In the properties do this (with AbilityModifier as an example):
private int _abilityModifier;
public int AbilityModifier
{
get { return _abilityModifier; }
private set {
if (value != _abilityModifier) {
_abilityModifier = value;
AbilityModifierTextBox.Text = value >= 0
? String.Format("+{0}", value)
: value.ToString();
OnPropertyChanged(nameof(AbilityModifier));
}
}
}
Assuming this event handler
private void ScoreDisplay_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
...
}
You can subscribe the event with
PropertyChanged += ScoreDisplay_PropertyChanged;
You need to use the add/remove syntax only in rare cases. Typically, when you create your own event store, because you have a lot of events and don't want to consume space for unsubscribed events.
You can use INotifyPropertyChanged together with data binding to immediately update the UI when changes are made to the data. To do this you would create a class with properties and the INotifyPropertyChanged implementation. In the form you then assign an instance of this class to the DataSource of a BindingSource. The controls are then bound to this BindingSource.
Then you can drop all the code used to read from or to write to text boxes or labels etc., as the binding mechanism does it automatically for you.
I use C# and I want to trigger an event from within a class :
So if the Price property of a class was changed then an event onPriceChanged (outside the class) should be fired.
However, I get an error:
The name 'onPriceChanged' does not exist in the current context
How could I fix this?
(I guess that I could pass the eventhandler to the class via constructor...but if possible I would prefer not to pass the eventhandler to the class)
Here is my code :
using System;
public delegate void delEventHandler();
class clsItem
{
//private static event delEventHandler _show;
private delEventHandler _show;
private int _price;
public clsItem() //Konstruktor
{
_show += new delEventHandler(Program.onPriceChanged); // error here : The name 'onPriceChanged' does not exist in the current context
}
public int Price
{
set
{
_price = value;
_show.Invoke(); //trigger Event when Price was changed
}
}
}
class Program
{
static void Main()
{
clsItem myItem = new clsItem();
myItem.Price = 123; //this should trigger Event "onPriceChanged"
}
//EventHandler
public static void onPriceChanged()
{
Console.WriteLine("Price was changed");
}
}
You're doing this the wrong way round - you're trying to attach the event handler from the class, and clearly that cannot have access to the Program.onPriceChanged method!
You should expose your event, and attach the event handler from the client code (Program).
class clsItem
{
//private static event delEventHandler _show;
private delEventHandler _show;
private int _price;
public clsItem() //Konstruktor
{
}
public event delEventHandler Show
{
add { _show += value; }
remove { _show -= value; }
}
public int Price
{
set
{
_price = value;
_show?.Invoke(); //trigger Event when Price was changed
}
}
}
And:
clsItem myItem = new clsItem();
myItem.Show += onPriceChanged;
myItem.Price = 123; //this now does trigger Event "onPriceChanged"
Live example: http://rextester.com/WMCQQ40264
The way you're dealing with events is not a good practice. the reason why we use Events is to decouple the objects we create from the methods they need to call.
For example if you want to create another object of the same type(clsItem) and get it to call another method once its price changed, you get into trouble. So I'd suggest this code rather than the current one:
using System;
public delegate void delEventHandler();
class clsItem
{
public event delEventHandler PriceChanged;
private int _price;
public clsItem() //Konstruktor
{
}
public int Price
{
set {
if(value!=_price) // Only trigger if the price is changed
{
_price = value;
if(PriceChanged!=null) // Only run if the event is handled
{
PriceChanged();
}
}
}
}
}
class Program
{
static void Main()
{
clsItem myItem = new clsItem();
myItem.PriceChanged += new delEventHandler(onPriceChanged);
myItem.Price = 123; //this should trigger Event "PriceChanged" and call the onPriceChanged method
}
//EventHandler
public static void onPriceChanged()
{
Console.WriteLine("Price was changed");
}
}
Here is the more traditional way of doing what you want:
public delegate void delEventHandler();
class clsItem
{
public event delEventHandler Show;
private int _price;
public clsItem() //Konstruktor
{
}
public int Price
{
set
{
_price = value;
Show?.Invoke(); //trigger Event when Price was changed
}
}
}
class Program
{
static void Main()
{
clsItem myItem = new clsItem();
myItem.Show += onPriceChanged;
myItem.Price = 123; //this should trigger Event "onPriceChanged"
}
//EventHandler
public static void onPriceChanged()
{
Console.WriteLine("Price was changed");
}
}
Notice that clsItem no longer knows who is subscribing to its event. All it cares about is notifying any listeners who happens to be subscribed. There is no longer a dependency between clsItem and the onPriceChanged method.
I'm trying to forward events from one class to objects contained within it (as described here: Forwarding events in C#). However, the events are of different type.
For example, I have a class Item which exposes a ValueChanged event handler of type EventHandler. The class ItemHaver exposes an EventHandler<StatusEventArgs>, which should fire whenever Item.ValueChanged does, but should also provide additional information. How do I properly implement add/remove to the ItemValueChanged event declaration?
In the below code, would the lambda function in the add method perform the correct action, and if so, what's the proper way to handle the remove?
class Item
{
public event EventHandler ValueChanged;
}
class ItemHaver
{
private int _status;
private Item _item;
public event EventHandler<StatusEventArgs> ItemValueChanged
{
add
{
_item.ValueChanged += value; // Wrong type
_item.ValueChanged +=
(obj, e) => value(obj, new StatusEventArgs(this._status));
}
remove
{
_item.ValueChanged -= // Does this even work?
(obj, e) => value(obj, new StatusEventArgs(this._status));
}
}
}
class StatusEventArgs : EventArgs
{
int Status { get; private set; }
StatusEventArgs(int status) { Status = status; }
}
I'd try using a dictionary in which I map the handlers.
class ItemHaver
{
private int _status;
private Item _item;
private Dictionary<EventHandler<StatusEventArgs>, EventHandler> handlersMap = new Dictionary<EventHandler<StatusEventArgs>, EventHandler>();
public event EventHandler<StatusEventArgs> ItemValueChanged
{
add
{
// _item.ValueChanged += value; // Wrong type
handlersMap.Add(value, (obj, e) => value(obj, new StatusEventArgs(this._status)));
_item.ValueChanged += handlersMap[value];
}
remove
{
_item.ValueChanged -= handlersMap[value];
}
}
}
I have a DGV that has its datasource set to a BindingList. There is also a ContextMenu assigned to a column in the DGV. There is a MenuItem set on the ContextMenu that calls a MessageBox on the click event.
Everything works fine and the Methods are called and the MessageBox with YesNo responses do what they are susppose to.
The problem that I am having is that when the MessageBox's click event occurs (Yes or No) it does it's job and goes away. If the same routine is called a second time, it again does it's job with no problem, then reappears. If I click Yes or No it goes away. If I call it a third time the MessageBox appears again does its job and reappears twice. As if everytime it's being called its iterating and calling itself again that amount of times. This will occur for everytime it's called.
The BindingList is built using a Class with nested properties and all data elements are present.
I tried using just a blank MessageBox with no DialogResults and no change. I even tried using the DGV's RaiseListChangedEvents=false in the ContextMenu click event and the DGV's Cell Enter Click Event.
I've stepped through my code and and no matter what the Class with the nested properties always gets called and causes the ContextMenu's click event to be called again and again... I figure this is by design since a BindingList will always AutoUpdate when a cell's value is accessed or changed.
ContextMenu's Column is a Button and is readonly.
So how do I either catch the MessageBox after it's run the first time or stop the BindingList from auto updating. My List draws its data from a Web Reference and I handle updates through the methods provided from the API. The only reason I'm using a BindingList is because the DGV doesn't work with just a List .
Thank you for any help or guidance. (First time posting, but have gathered and used a lot of info from here)
Here's some code:
_requestsView.AutoGenerateColumns = false;
_edit.DataPropertyName = "RequestId";
_patient.DataPropertyName = "Patient";
_dateSubmitted.DataPropertyName = "Date";
_completedBy.DataPropertyName = "CompletedBy";
_completedOn.DataPropertyName = "CompletedOn";
_procedure.DataPropertyName = "Procedure";
_stat.DataPropertyName = "Stat";
_viewReport.DataPropertyName = "ViewReport";
_selectedSpecialist.DataPropertyName = "SelectedSpecialist";
_status.DataPropertyName = "Status";
_rate.DataPropertyName = "Rating";
_requestsView.DataSource = _requestsBinding;
// _cancelRequest_Click is ContextMenu MenuItem
void _cancelRequest_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void _requestsView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (_requestsView.CurrentRow != null)
if (_requestsView.CurrentRow.Cells["_viewReport"].Selected)
try
{
var requestNumber = (int)_requestsView.CurrentRow.Cells ["_viewReport"].Value;
var letter = Api.Client.getCompletedLetter(UseSession.SessionId, requestNumber);
var convertedLetter = Convert.FromBase64String(letter);
var requestNumberToString = Convert.ToString(requestNumber);
var tmpfile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), requestNumberToString + #".pdf");
var view = new ViewLetter(requestNumberToString, tmpfile);
File.WriteAllBytes(tmpfile, convertedLetter);
view._pdf.LoadFile(tmpfile);
view._pdf.PerformLayout();
view._pdf.Refresh();
view._pdf.setShowToolbar(true);
view._pdf.setZoom(100);
view.Show();
view.Activate();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
if (_requestsView.CurrentRow != null)
if (_requestsView.CurrentRow.Cells["_edit"].Selected)
_edit.ContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
if (_requestsView.CurrentRow != null)
if (_requestsView.CurrentRow.Cells["_rate"].Selected)
_rate.ContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
}
public class Requests
{
private int _requestId;
private DateTime _date;
private string _patient;
private string _completedBy;
private string _completedOn;
private string _procedure;
private string _stat;
private int _viewReport;
private Specialists _selectedSpecialist;
private string _status;
private int _rating;
public Requests()
{ }
public Requests(string stat)
{
_stat = stat;
}
public int RequestId
{
get { return _requestId; }
set { _requestId = value; }
}
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
public string Patient
{
get { return _patient; }
set { _patient = value; }
}
public string CompletedBy
{
get { return _completedBy; }
set { _completedBy = value; }
}
public string CompletedOn
{
get { return _completedOn; }
set { _completedOn = value; }
}
public string Procedure
{
get { return _procedure; }
set { _procedure = value; }
}
public string Stat
{
get { return _stat; }
set { _stat = value; }
}
public int ViewReport
{
get { return _viewReport; }
set { _viewReport = value; }
}
public Specialists SelectedSpecialist
{
get { return _selectedSpecialist; }
set { _selectedSpecialist = value; }
}
public string Status
{
get { return _status; }
set { _status = value; }
}
public int Rating
{
get { return _rating; }
set { _rating = value; }
}
}
Just wanted to update this and close it. I figured out a work around that sets a boolean true or false during different stages of events being called. If the boolean is set to true I just do a return to get out of the methods.
I have field string in struct,
and i want learn real-time changed this field.
struct example {
public string ex;
}
examp = new example();<BR>
examp.ex = "test";
////// then program work and eamp.ex = "bing";
I need method
on_ex_changed()
{
if examp.ex changed then .....
}
online and simple plz
You can put an event at the setter as follows.
The event will be fired every time the setter is called.
public class MyObj
{
private RectangleF mRectangle;
public event EventHandler RectangleChanged;
public RectangleF Rectangle
{
get
{
return mRectangle;
}
set
{
mRectangle = value;
OnRectangleChanged();
}
}
protected virtual void OnRectangleChanged()
{
if (RectangleChanged != null)
{
RectangleChanged(this, EventArgs.Empty);
}
}
}