How to create an mouse eventhandler in C++/CX - event-handling

I am creating button control in my Windows 8 metro application made in C++/CX. I'd like to make an event which is triggered when the button is pressed. But I have no clue how to add an event to a button in C++/CX.
If you want to do this in C# it is as following:
Button btnDoSomething = new Button();
btnDoSomething.MouseClick += new MouseEventHandler(iGotClickedByTheButton);
void iGotClickedByTheButton(object sender, MouseEventArgs e)
{
MessageBox.Show("Hello I got clicked!");
}
So my approach was doing something like this:
Button^ btnDoSomething = ref new Button();
btnDoSomething->Tapped += ref new TappedEventHandler(sender, iGotClickedByTheButton);
void iGotClickedByTheButton(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
MessageDialog^ msgDlg = ref new MessageDialog("Hello I got clicked!");
msgDlg->ShowAsync();
}
This however resulted in an error at this place:
btnDoSomething->Tapped += ref new TappedEventHandler(sender, iGotClickedByTheButton);
It displayed the following error:
Error: invalid delegate initializer -- function does not match the
delegate type.

Solved it by doing the following:
Button^ btnDoSomething = ref new Button();
btnDoSomething->Click += ref new Windows::UI::Xaml::RoutedEventHandler(this, &MyProjectName::MainPage::iGotClickedByTheButton);

Related

How to add an event trigger listener(callback)

Basically how you would create a public void MethodToDoStuff(), attach a monobehaviour script and link the method on a button, so that when its clicked, said method "MethodToDoStuff" is called.
Now I want to do that via an editor script.
Add an event trigger component
On the event trigger component, add a PointerDown and PointerUp event
On the PointerUp and PointerDown, link a public method on another script to be run (doesHandler.HidePanel()) "see code below"
I could do this manually but having an editor script is super effiecient.
Here is what I have so far:
All help is appreciated, Thanks!
EDITOR SCRIPT:
void OnWizardCreate()
{
doesHandler = GameObject.FindWithTag("WhatItDoes").GetComponent<WhatThisDoes>();
GameObject selection = Selection.activeGameObject;
EventTrigger trig=(EventTrigger)selection.AddComponent(typeof(EventTrigger));
EventTrigger.Entry onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
EventTrigger.Entry onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}
OTHER SCRIPT:
public void HidePanel()
{
whatItDoesPanel.SetActive(false);
}
On runtime you would usually call e.g.
onPointerDown.AddListener(doesHandler.HidePanel);
however this would only add the listener temporarily.
Adding a persistent listener is a bit more complex but luckily there now is a tool for this: UnityEventTools.AddPersistentListener so afaik you would only have to add
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
Additional afaik you should before use Undo.RecordObject in order to mark the changed object and the scene as dirty and add a Undo/Redo entry like
Undo.RecordObject(selection, "Added event triggers");
So probably something like
void OnWizardCreate()
{
doesHandler = Object.FindObjectOfType<WhatThisDoes>();
if(!doesHandler)
{
Debug.LogWarning("No WhatThisDoes found in the scene -> Ignored");
return;
}
var selection = Selection.activeGameObject;
if(!selection)
{
Debug.LogWarning("Nothing selected -> Ignored")
return;
}
if(selection.GetComponent<EventTrigger>())
{
Debug.LogWarning($"The selected object {selection} already has an EventTrigger attached! -> Ignored");
return;
}
// log the undo before making changes
Undo.RecordObject(selection, "Added event triggers");
var onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
var onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
var trig = selection.AddComponent<EventTrigger>();
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}

How do I connect a custom function to the clicked action of a GTK Button?

I am working my way through the Vala GTK+3 tutorial provided by Elementary OS. I understand that this code:
var button_hello = new Gtk.Button.with_label ("Click me!");
button_hello.clicked.connect (() => {
button_hello.label = "Hello World!";
button_hello.set_sensitive (false);
});
uses a Lambda function to change the button's label when it's clicked. What I want to do is call this function instead:
void clicked_button(Gtk.Button sender) {
sender.label = "Clicked. Yippee!";
sender.set_sensitive(false);
}
I've tried this:
button.clicked.connect(clicked_button(button));
But I get this error from the Vala compile when I try to compile:
hello-packaging.vala:16.25-16.46: error: invocation of void method not allowed as expression
button.clicked.connect(clicked_button(button));
^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
I'm new to both Vala and Linux so please be gentle but can someone point me in the right direction?
You need to pass a reference to the function, rather than the result of the function. So it should be:
button.clicked.connect (clicked_button);
When the button is clicked GTK+ will invoke the clicked_button function with the button as an argument.
The error message invocation of void method not allowed as expression is telling you you are calling (invoking) the method and it has no result (void). Adding parentheses, (), to the end of a function name invokes that function.
Managed to get it working. Here's the code in case others need it:
int main(string[] args) {
// Initialise GTK
Gtk.init(ref args);
// Configure our window
var window = new Gtk.Window();
window.set_default_size(350, 70);
window.title = "Hello Packaging App";
window.set_position(Gtk.WindowPosition.CENTER);
window.set_border_width(12);
window.destroy.connect(Gtk.main_quit);
// Create our button
var button = new Gtk.Button.with_label("Click Me!");
button.clicked.connect(clicked_button);
// Add the button to the window
window.add(button);
window.show_all();
// Start the main application loop
Gtk.main();
return 0;
}
// Handled the clicking of the button
void clicked_button(Gtk.Button sender) {
sender.label = "Clicked. Yippee!";
sender.set_sensitive(false);
}

xamarin listview checkboxes

I added custom listview. And I added checbox in rows. I have to use ItemChecked event but I have an error->"cannot resolve symbol 'ItemChecked'"
and "cannot resolve symbol 'ItemCheckedEventArgs '"
ziyaret_listesi = FindViewById<ListView>(Resource.Id.list_ziyaret_kayitlari);
ziyaret_listesi.Adapter = new CustomAdapterZiyaretRapor(this, list_ziyaret_rapor);
ziyaret_listesi.ItemChecked += ListView_ItemChecked;
private void ziyaret_listesi_ItemChecked(object sender, ItemCheckedEventArgs e)
{
// the checked state of an item has changed
}
Why cannot resolve itemchecked event in c# Xamarin?

GWT/MVP: detecting change events in a table with proper MVP pattern

We're using gwt-presenter, but not really a question specific to that...
I've got a table with users in it. As I build the table in the view (from the data provided by the presenter), I need to add two action buttons ("Edit", and "Delete") at the end of the row.
What's the best way to assign click handlers to these buttons so the presenter knows which was clicked? Previous to this, we could pass a private field from the view to the presenter and attach a discrete click handler to that button. However, this method is rather rigid and doesn't work in this scenario very well.
Thanks in advance.
How about having the view allowing the subscription for edit/delete click events, registering internally the individual row click events, and then delegating the event handling to the ones registered by the view?
I mean something like the following pesudo code:
View:
addRowEditClickHandler(ClickHandler handler) {
this.rowEditClickHandler = handler;
}
addRowDeleteClickHandler(ClickHandler handler) {
this.rowDeleteClickHandler = handler;
}
//... somewhere when setting up of the grid...
rowEditButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowEditClickHandler.onClick(args)
}
rowDeleteButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowDeleteClickHandler.onClick(args)
}
Presenter:
View view = new View();
view.addRowEditClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});
view.addRowDeleteClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});

.Net CF Prevent Overzealous, Impatient Clicking (while screen is redrawing)

.Net Compact Framework
Scenario: User is on a screen. Device can't finds a printer and asks the user if they want to try again. If they click "No", the current screen is closed and they are returned to the parent menu screen. If they click the "No" button multiple times, the first click will be used by the No button and the next click will take effect once the screen has completed redrawing. (In effect clicking a menu item which then takes the user to another screen.)
I don't see a good place to put a wait cursor...there isn't much happening when the user clicks "No" except a form closing. But the CF framework is slow to redraw the screen.
Any ideas?
you can skip pending clicks by clearing the windows message queue with
Application.DoEvents();
We use the following custom Event class to solve your problem (preventing multiple clicks and showing a wait cursor if necessary):
using System;
using System.Windows.Forms;
public sealed class Event {
bool forwarding;
public event EventHandler Action;
void Forward (object o, EventArgs a) {
if ((Action != null) && (!forwarding)) {
forwarding = true;
Cursor cursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
Action(o, a);
} finally {
Cursor.Current = cursor;
Application.DoEvents();
forwarding = false;
}
}
}
public EventHandler Handler {
get {
return new EventHandler(Forward);
}
}
}
You can verify that it works with the following example (Console outputs click only if HandleClick has terminated):
using System;
using System.Threading;
using System.Windows.Forms;
class Program {
static void HandleClick (object o, EventArgs a) {
Console.WriteLine("Click");
Thread.Sleep(1000);
}
static void Main () {
Form f = new Form();
Button b = new Button();
//b.Click += new EventHandler(HandleClick);
Event e = new Event();
e.Action += new EventHandler(HandleClick);
b.Click += e.Handler;
f.Controls.Add(b);
Application.Run(f);
}
}
To reproduce your problem change the above code as follows (Console outputs all clicks, with a delay):
b.Click += new EventHandler(HandleClick);
//Event e = new Event();
//e.Action += new EventHandler(HandleClick);
//b.Click += e.Handler;
The Event class can be used for every control exposing EventHandler events (Button, MenuItem, ListView, ...).
Regards,
tamberg
Random thoughts:
Disable the some of the controls on the parent dialog while a modal dialog is up. I do not believe that you can disable the entire form since it is the parent of the modal dialog.
Alternatively I would suggest using a Transparent control to catch the clicks but transparency is not supported on CF.
How many controls are on the parent dialog? I have not found CF.Net that slow in updating. Is there any chance that the dialog is overloaded and could be custom drawn faster that with sub controls?
override the DialogResult property and the Dispose method of the class to handle adding/remvoing a wait cursor.