how to create a KeyUp event in a legacy C++ CLR:syntax application - delegates

I am trying to change a legacy C++ CLR:oldsyntax application to set up an KeyUp event without success.
This is the relevant part of the code:
// declare delegate
__delegate void KeyUpFuncDelegate(Object */*sender*/, System::Windows::Forms::KeyEventArgs * e);
// try to hook up the event
this->SuperMatricula->KeyUp+= KeyEventHandler(this,KeyUpFuncDelegate);
I get the following error:
error C2275: 'GrFingerSampleCPPdotNET2005::FormSup::KeyUpFuncDelegate' : illegal
use of this type as an expression
Any clue?
Thanks in advance!

I think you just need to change it to delegate and make sure the event uses that delegate. An example ripped out of some of code for comparison.
.h
private ref class StatusMgr sealed
{
public:
delegate void StatusTextChangedEventHandler( System::Object^ sender, StatusTextChangedEventArgs^ args );
event StatusTextChangedEventHandler^ StatusTextChanged;
.cpp
statusMgr->StatusTextChanged +=
gcnew StatusMgr::StatusTextChangedEventHandler( this, &StatusBarTextItem::onStatusTextChanged );

Related

rxBindings - How to know what consumer type should be when debouncing click events?

using rxBindings im trying to slow down a click event but i would like to know what the parameter is need.
For example, here is a call i am doing on a imageview. So ImageView v;
RxView.clicks(v)
.throttleFirst(400, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
#Override
public void accept(#io.reactivex.annotations.NonNull Object v) throws Exception {
showBottomSheet(getAdapterPosition());
}
});
but im im not sure what the parameter in accept should be ?
I was expecting i would get the view here but when i try changing the type to View i get an error of no such method.
If you look at the source code of the Observable generate using RxView.clicks(), you will see that when the click happens, the following code is triggered:
observer.onNext(Notification.INSTANCE);
that is defined in the library, as:
public enum Notification {
INSTANCE
}
It is just a convenient way for indicating that the event happened, it doesn't carry any extra information.

Mono GTK# - PropertyNotifyEvent of window or widget

I want to be able to react if a property of a given window or widget has been changed and found the Gtk.Widget.PropertyNotifyEvent event. The (mono) documentation says that it will be fired if any property has been changed. So I've tried to make use of it but my event handler method is never invoked:
protected void DoSomething()
{
Gtk.Window __Window = new Gtk.Window(Gtk.WindowType.Toplevel);
Gtk.Button __Button = new Gtk.Button();
__Window.Add(__Button);
__Button.PropertyNotifyEvent += this.ButtonPropertyChangedEventHandler;
... // Show the window
__Button.Label = Mono.Unix.Catalog.GetString("This is a test button");
}
protected void ButtonPropertyChangedEventHandler(object o, PropertyNotifyEventArgs e)
{
// Handle the event
}
Do I miss something? Or did I understand something very basic wrong? Or is there another approach to achieve my goal?
Property change notifications need to be explicitly enabled by setting the GDK_PROPERTY_CHANGE_MASK flag, as explained here:
https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-property-notify-event
In Gtk# you just need to add the following line to your code:
__Button.Events |= Gdk.EventMask.PropertyChangeMask;
And the handler assigned to the PropertyNotifyEvent event will start to receive property change notifications. Gtk# enables some events implicitly but all others should be enabled from user code or from the stetic designer.

PhotoChooserTask.Completed not fired

I'm trying to using PhotoChooserTask for our purposes.
After calling photoChooserTask.Show() chooser is showed but when I choose a picture it's closing and event Completed not fired !
Why?
And more, after that PhotoChooserTask not showed next time when calling Show.
P.S. if i try this code in new solution - it will work fine, but why it doesn't work in our project?
PhotoChooserTask photoChooserTask;
private void button2_Click(object sender, System.Windows.RoutedEventArgs e)
{
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
photoChooserTask.Show();
// TODO: Add event handler implementation here.
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
//Bla bla bla
}
I solved this problem.
So, project CAN NOT have more than one photo chooser.
You can't declare PhotoChooserTask in Page1 and Page2 with different logic of processing.
Hope this will helpfull for someone.
You should make sure that you respect the guidelines for creating and initializing the object:
To ensure that your application receives the result of the
PhotoChooserTask, the object must be declared with class scope
within the PhoneApplicationPage class and you must call the chooser
constructor and assign the Completed event delegate within the page’s
constructor.
Source

Subscription to DTE events doesn't seem to work - Events don't get called

I've made an extension inside a package and I am calling the following code (occurs when a user presses a button in the toolbar):
DocumentEvents documentEvents = (DTE2)GetService(typeof(DTE));
_dte.Events.DebuggerEvents.OnEnterBreakMode += DebuggerEvents_OnEnterBreakMode;
_dte.Events.DebuggerEvents.OnEnterDesignMode += DebuggerEvents_OnEnterDesignMode;
_dte.Events.DebuggerEvents.OnContextChanged += DebuggerEvents_OnContextChanged;
_dte.Events.DocumentEvents.DocumentSaved += new _dispDocumentEvents_DocumentSavedEventHandler(DocumentEvents_DocumentSaved);
_dte.Events.DocumentEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(DocumentEvents_DocumentOpened);
void DocumentEvents_DocumentOpened(Document Document)
{
}
void DocumentEvents_DocumentSaved(Document Document)
{
}
void DebuggerEvents_OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
{
}
void DebuggerEvents_OnContextChanged(Process NewProcess, Program NewProgram, Thread NewThread, StackFrame NewStackFrame)
{
}
private void DebuggerEvents_OnEnterDesignMode(dbgEventReason reason)
{
}
The first and the major problem is that the subscription to the event doesn't work. I've tried:
Opening new documents
Detaching from debug (thus supposedly triggering OnEnterDesignMode
Saving a document
None of these seem to have any effect and the callback functions were never called.
The second issue is that the subscription to the event line works USUALLY (the subscription itself, the callback doesn't work as described above) but after a while running the subscription line, e.g:
_dte.Events.DebuggerEvents.OnEnterBreakMode -= DebuggerEvents_OnEnterBreakMode;
Causes an exception:
Exception occured!
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.StubHelpers.StubHelpers.StubRegisterRCW(Object pThis, IntPtr pThread)
at System.Runtime.InteropServices.UCOMIConnectionPoint.Unadvise(Int32 dwCookie)
at EnvDTE._dispDebuggerEvents_EventProvider.remove_OnEnterDesignMode(_dispDebuggerEvents_OnEnterDesignModeEventHandler A_1)
Any ideas will be welcome
Thanks!
Vitaly
Posting an answer that I got from MSDN forums, by Ryan Molden, in case it helps anyone:
I believe the problem here is how the
CLR handles COM endpoints (event
sinks). If I recall correctly when
you hit the
_applicationObject.Events.DebuggerEvents
part of your 'chain' the CLR will
create a NEW DebuggerEvents object for
the property access and WON'T cache
it, therefor it comes back to you, you
sign up an event handler to it (which
creates a strong ref between the
TEMPORARY object and your object due
to the delegate, but NOT from your
object to the temporary object, which
would prevent the GC). Then you don't
store that object anywhere so it is
immediately GC eligible and will
eventually be GC'ed.
I changed the code to store DebuggerEvents as a field and it all started to work fine.
Here is what #VitalyB means using code:
// list where we will place events.
// make sure that this variable is on global scope so that GC does not delete the evvents
List<object> events = new List<object>();
public void AddEvents(EnvDTE dte)
{
// create an event when a document is open
var docEvent = dte.Events.DocumentEvents;
// add event to list so that GC does not remove it
events.Add(docEvent );
docEvent.DocumentOpened += (document)=>{
Console.Write("document was opened!");
};
// you may add more events:
var commandEvent = dte.Events.CommandEvents;
events.Add(commandEvent );
commandEvent.AfterExecute+= etc...
}

SWT: How do you register KeyUp events when no Control have focus?

Im am making an Java SWT program that is required to run on both Linux and Windows.
I use the following Code to listen for KeyUp events:
Control.addListener(SWT.KeyUp, new Listener() {
public void handleEvent(Event arg0) {
System.out.println("Event");
}
});
But this does not trigger when no control has focus.
Do anyone know of a place i can add a listener that acts as a Catch-all?
The only way of doing this that I'm aware of is by placing a filter on the Display. Take note that multiple Shells may operate on one Display, so you should be careful!
shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(final Event event) {
System.out.println(event);
}
});
try following method in Display class:
public void addListener ( int eventType, Listener listener )
I have not been able to find a solution to this. I suspect none exist

Resources