How can a UserControl trigger an selectionChanged event to a Window? - c#

I want to make a ColorPicker userControl, and put inside a window.
When the user click in any rectangle of the usercontrol, then I want to return this information to the Window. How can I do this please? Thanks!

Your Color Picker UserControl has to implement an event that is raised every time the user clicks on one of the rectangles of the UserControl. If you don't know how to implement an event, just comment this answer and I give you an example.
Here is the example: You declare your own event args (if needed) to provide some information in the event:
class RectangleClickedEventArgs : EventArgs
{
public int SomeValue { get; set; }
}
In your usercontrol you declare the event:
public event EventHandler<RectangleClickedEventArgs> RectangleClicked;
In some condition you raise the event in this way (the thread-safe way:
var temp = RectangleClicked;
if (temp != null)
{
var e = new RectangleClickedEventArgs{ SomeValue = 42};
temp(this, e);
}
In your form you subscribe the event:
userControl.RectangleClicked += OnRectangleClicked;
And in the event routine you do your desired action:
private void OnRectangleClicked(object sender, RectangleClickedEventArgs e)
{
// Do what you want to do
}
Hope that helps...

Related

Is there a way to activate a Button that exists within another class?

I am using C# and Xamarin. I have two separate classes. One class is essentially the user interface and another class is acting as a custom built generic entry for users to input data and search for results by clicking a button.
Main UI Class:
Class MainPage
{
public MainPage
{
Content = new StackLayout
{
Children =
{
new InputClass // This is my custom built user entry class
{
}.Invoke(ic => ic.Clicked += WhenButtonPressedMethod) // The problem is here, I can't figure out how to call the button within the input class to fire a clicked event.
}
}
}
}
public async void WhenButtonPressedMethod (object sender, EventArgs e)
{
// Supposed to do stuff when the button is pressed
}
InputClass:
public class InputClass : Grid
{
public delegate void OnClickedHandler(object sender, EventArgs e);
public event OnClickHandler Clicked;
public InputClass
{
Children.Add(
new Button {}
.Invoke(button => button.Clicked += Button_Clicked)
)
}
private void Button_Clicked(object sender, EventArgs e)
{
Clicked?.Invoke(this, e);
}
}
The "InputClass" is a grid that holds a title text label, an entry and a button that a user can press to submit and search data. The button in this class is what I'm trying to actually access to invoke/cause a click event so that the method in the main UI class can be called. But, when I try to invoke a click event on the "InputClass" I can't access the button inside of it, I can only access "InputClass" itself which is just a grid with no useful event properties.
Any solutions or ideas?
If you are running into the same problem as mentioned here, follow the code on this page and read through the comments, it covers enough to be able to piece it together. My mistake was attaching Invokes to the wrong objects.
Don't know why fluent Invoke didn't work correctly.
Add the event handlers this way:
public MainPage
{
var ic = new InputClass();
ic.Clicked += WhenButtonPressedMethod;
Content = new StackLayout
{
Children = { ic }
}
}
public InputClass
{
var button = new Button;
button.Clicked += Button_Clicked;
Children.Add(button);
}

WPF passing data from Window to a running User Control [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm new in WPF world. I have a UserControl and a Button inside it. When button clicked, a selector Window open in new Window and running with UserControl at same time.
I want in second window when user select a value, pass this value back to UserControl then close window. How can I do this? is DataBinding with INotifyPropertyChangedclass the best way? How can I implement this?
solution:
From Microsoft document and delegate and EventHandler meanings, I do the following. I have a UserControl with name BuyFactor. When user click Add Item new Window with name AddItem raised. When new item selected and when Add This clicked, I want to send Item Id back to BuyFactor with EventHandler:
BuyFactor UserControl:
public partial class BuyFactor: UserControl
{
Dialogs.AddItem publisher;
public TaqehBuyFactor()
{
InitializeComponent();
publisher = new Dialogs.AddItem();
publisher.RaiseCustomEvent += HandleCustomEvent;
}
void HandleCustomEvent(object sender, Dialogs.CustomEventArgs e)
{
//Should change text when button clicked on Window (publisher)
ProductName.Text = e.Message;
}...}
AddItem Window:
public partial class SelectTaqehDialog : Window
{
public event EventHandler<CustomEventArgs> RaiseCustomEvent;
public void DoSomething()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));
}
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
// Event will be null if there are no subscribers
if (handler != null)
{
// Format the string to send inside the CustomEventArgs parameter
e.Message += String.Format(" at {0}", DateTime.Now.ToString());
// Use the () operator to raise the event.
handler(this, e);
}
}
private void addToFactor_Click(object sender, RoutedEventArgs e)
{
// Fire when Add This button clicked
DoSomething();
}
And My CustomEventArgs:
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
Pay attention that call publisher.Show() for creating new window.
// in MainWindow or somewhere
myUserControl.someBtn.Click += (se, a) => {
var mw = new MyWindow();
mw.Show();
mw.myEvent += (myEventSender, myComboBoxFomMyWindow) => MessageBox.Show(myComboBoxFromMyWindow.SelectedItem as string);
};
// MyWindow
public event EventHandler<ComboBox> MyEvent;
public MyWindow() {
myComboBox.SelectionChanged += (se, a) => MyEvent?.Invoke(this, myComboBox);
}
Hope it works!

How to trigger an event when user click on the item of the list view?

I will like to ask that I will to trigger an event which when the user click on the item in the list view then do the thing that I wished.
What is the suitable event that I can use???
Is Mouse Down event suitable???
as sthotakura stated, you would use ListView.SelectionChanged event. Link to msdn: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectionchanged(v=vs.110).aspx
example:
public class doSomething
{
public void SomeMethod()
{
...
// delegate event handler
ListView.SelectionChanged += delegateEventHandler;
// or Lambda Expression
ListView.SelectionChanged += (sender, args)
=> {
// Apply Logic
};
...
}
public void delegateEventHandler(object sender, EventArgs eventArgs)
{
// Apply Logic...
}
}
Yes, it is suitable, then again GaussZ is correct. What about an user selecting this thing using his keyboard by tabbing and pressing enter or some other UI event?

How to add events in C#? [duplicate]

This question already has answers here:
How to add an event to a UserControl in C#?
(6 answers)
Closed 8 years ago.
I am learning event handling in C# and have just learnt the basic usage of
delegates
I was looking for a way to add some events to my GUI App. for ex, check the following code:-
private void label1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World") ;
}
This function displays the MessageBox with the content HelloWorld whenever I click on the label label1. I wanted to know , how can we add various other events like hovering over the label and other such events. Also, what is the use of the parameter
sender and e
label1.OnMouseHover += myDelegate will add your delegate to the mouseover event.
See list of events.
label1.Click += new System.EventHandler(label1_Click);
You'll get it better if you create a class and add your own event to it.
The default "e" is a an instance of System.EventArgs
You can derived from that to have your own
e.g.
public class MyEventArgs : System.EventArgs
{
public string EventData {get; private set;}
public MyEventArgs(String argEventData)
{
EventData = argEventData;
}
}
Then to use the above in a class
public class SomeFellaWithAnEvent
{
public event EventHandler<MyEventArgs> OnMyEvent;
private int _value;
public int Value
{
get {return _value;}
set
{
if (_value != value)
{
_value = value;
DoEvent(_value.ToString();
}
}
}
protected void DoEvent(String argData)
{
if (OnMyEvent != null)
{
OnMyEvent(this,new MyEventArgs(argData))
}
}
}
So now you have something where if Value get's changed it will raise an event if you've given it a handler
e.g
SomeFellaWithAnEvent o = new SomeFellaWithAnEvent()
o.OnMyEvent += someThingChanged();
o.Value = 22;
private void somethingChanged(Object sender, MyEventArgs e)
{
// do something with it.
// debug this and you'll see sender is the o instance above, and e is the instance
// of MyEventArgs created in the DoEvent method and has a property set to "22"
}
To add more event handlers to existing controls from the tool box. Click the events tab (lightning button) in the property inspector and then double click in the value.
Or in the code view type label1.Click +=
and then press tab twice.

generate OnCheckedChanged for custom checkbox

I have a custom checkbox control that inherited from System.Windows.Forms.Control
and it hasn't CheckedChanged event. I want to implement CheckedChange same as dot net native CheckBox. How can I do it well ?
You are inheriting fromn Control, not CheckBox, so the solution is similar to the one proposed by Frigik, but it's not exactly that one.
First of all you have to define the event in your class, i.e.:
public event EventHandler CheckedChanged;
In this way every developer using your control can subscribe/unsubscribe to the event. This is not enough, since the event will never be triggered. To do so, you have to define a method to trigger it, and the call this method whenever the state of your control changes:
private void RaiseCheckedChanged()
{
if (CheckedChanged!= null)
CheckedChanged(this, EventArgs.Empty);
}
Where this method will be called depends on the structure of your control. For instance if you have a property Checked, you could call the method in its setter:
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
RaiseCheckedChanged();
}
}
Try this code :
CheckBox chkList1 = new CheckBox();
chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
// Do your stuff
}
Try this:
public class YourCheckBox:CheckBox
{
public event EventHandler<EventArgs> OnCheckedChangedCustom;
protected override void OnCheckedChanged(EventArgs e)
{
if (OnCheckedChangedCustom!=null)
{
OnCheckedChangedCustom(this, EventArgs.Empty);
}
base.OnCheckedChanged(e);
}
}

Categories