I need an event for my CF application, that would trigger after user has pressed an held his finger on the control for 2 seconds. What event can i use, since keyDown event is already used.
Well, KeyDown is pretty irrelevant for capturing the length of time a finger is pressed. The use of the finger relates to the events Click, MouseDown, MouseUp and MouseMove.
To get the behaviour you're after, the events you should be interested in are MouseDown and MouseUp.
I suggest the best way to do this would be to create your own control base class. Here's one I made earlier (not tested, but should give you a general idea of what to do):
public partial class BaseControl : UserControl
{
public BaseControl()
{
InitializeComponent();
base.MouseDown += new MouseEventHandler(BaseControl_MouseDown);
base.MouseUp += new MouseEventHandler(BaseControl_MouseUp);
MouseHeldTimer = new Timer();
MouseHeldTimer.Interval = 2000;
MouseHeldTimer.Tick += new EventHandler(mouseHeldTimer_Tick);
}
protected Timer MouseHeldTimer;
protected bool MouseIsDown;
void mouseHeldTimer_Tick(object sender, EventArgs e)
{
this.MouseHeldTimer.Enabled = false;
if (this.MouseHeldDown != null)
{
this.MouseHeldDown(sender, e);
}
}
void BaseControl_MouseDown(object sender, MouseEventArgs e)
{
this.MouseHeldTimer.Enabled = true;
}
void BaseControl_MouseUp(object sender, MouseEventArgs e)
{
this.MouseHeldTimer.Enabled = false;
}
public event MouseHeldDownHandler MouseHeldDown;
public delegate void MouseHeldDownHandler(object sender, EventArgs e);
}
Basically, the MouseHeldTimer will start with an interval of 2 seconds the moment the user touches their finger to the screen. If the user lifts their finger the timer is stopped. If the user's finger is down for longer than 2 seconds, the delegate event MouseHeldDown will fire. You can then capture this event on your form by doing the following:
control.MouseHeldDown+= new EventHandler(control_MouseHeldDown);
Alternatively, if you only care about the form, you can just use the Form's DoubleClick event as that will fire after holding the mouse down for a second or two.
Related
I'm drawing something on my panel with a method and when I call this method via a button it does draw, but when I call this method on Form_Load it doesn't draw anything. When I debug it, it actually goes through the code without any problem, but still it won't draw anything.
below you can see the Form_Load and the Button_Click events:
private: System::Void SelectElementForm_Load(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
Index = 0;
DrawLinesInLayout();
}
}
and the button
private: System::Void btnLeft_Click(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
if (Index + 1 > 1)
{
Index--;
DrawLinesInLayout();
}
else
{
Index = ElementList->Count - 1;
DrawLinesInLayout();
}
}
}
When I use the Paint-event it works for when my form pops up. But then I got the code twice in my program which is kinda pointless.
So my questions are:
Why isn't my Form_load using my method correctly and my button is?
Can I call the Paint event on a button click?
As Hans Passant mentioned, you can't draw on something that isn't there yet.
Solution: draw after it is created.
Invalidate(); doesn't work since there are variables that change in the drawing method (that's why there is an Indexchange in each call event).
Instead of using the Load event, use the Shown event. This will draw the lines on the form:
private: System::Void SelectElementForm_Shown(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
Index = 0;
DrawLinesInLayout();
}
}
I'm converting a Windows Phone 7 app to Windows Store, so I'm moving over to Xaml. I have a method that runs at a certain point to update the data on the screen. It either assigns or removes an event handler delegate to the CompositionTarget.Rendering event. The message I get is No overload for 'OnCompositionTargetRendering' matches delegate 'System.EventHandler' '
Here's what I have:
private void CheckCompleted()
{
Color completeColor;
if (this.DecryptedText.ToString().ToUpper() == this.ThisPuzzle.QuoteText.ToUpper())
{
// We're done!!! ...
CompositionTarget.Rendering -= this.OnCompositionTargetRendering;// new EventHandler(this.OnCompositionTargetRendering);
...
}
else
{
...
CompositionTarget.Rendering += this.OnCompositionTargetRendering;// new EventHandler(this.OnCompositionTargetRendering);
...
}
}
protected void OnCompositionTargetRendering(object sender, EventArgs args)
{
this.DisplayTime();
if (ThisPuzzle != null)
{
foreach (UIElement thisElement in Letters.Children)
{
...
}
}
}
If you check the documentation CompositionTarget.Rendering is of type EventHandler<object> in Windows Store apps and not of type EventHandler as in Silverlight.
This means you need to change the signature of your event handler accordingly to:
protected void OnCompositionTargetRendering(object sender, object args)
I am trying to call a function whose parameters are object sender and RoutedEventsArg e. I need those parameters since I have created a button on the main window related to this function and when I click the button it links to my function.
protected void StartRecord(object sender,RoutedEventsArg e)
{
// some stuff that creates a button and then does stuff
}
In another function, I need to call the above function stated above, but this second function has a parameter of AllFramesReadyArg e, not RoutedEventsArg e. So how do i call out the first function
void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
this.StartRecord(sender, e);
// does not work since parameter calls for RoutedEventArgs
}
Your StartRecord is not part of the Kinect Toolbox. You appear to have written it and given it those two arguments. It doesn't need them, nor do you necessarily need the function.
You also do not want to be calling StartRecord in AllFramesReady. The AllFramesReady callback is fired every time all the frames are ready for processing (hence the function name), which happens roughly 30 times a second. You only need to tell it to record once.
Per your other question, StartRecord is a callback to a button -- it shouldn't be called in code. It is called when the user hits the associated button.
Just looking at the Kinect Toolbox code and the callbacks, your code should look something like this:
KinectRecorder _recorder;
File _outStream;
bool _isRecording = false;
private void KinectSetup()
{
// set up the Kinect here
_recorder = new KinectRecorder(KinectRecordOptions.Skeleton, _outStream);
// some other stuff to setup
}
private void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
SkeletonFrame skeleton;
if (_isRecording && skeleton != null)
{
_recorder.Record(skeleton);
}
}
public void StartRecord(object sender, RoutedEventsArg e)
{
_isRecording = !_isRecording;
}
I am developing an application in windows 8 Visual studio 11, and I want to define an event handler for a DispatcherTimer instance as below:
public sealed partial class BlankPage : Page
{
int timecounter = 10;
DispatcherTimer timer = new DispatcherTimer();
public BlankPage()
{
this.InitializeComponent();
timer.Tick += new EventHandler(HandleTick);
}
private void HandleTick(object s,EventArgs e)
{
timecounter--;
if (timecounter ==0)
{
//disable all buttons here
}
}
.....
}
But I get the following Error :
Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>'
I am a novice developer to widows 8 apps.
Would you please help me ?
almost had it :) You don't need to instantiate a new eventhandler object, you only need to point to the method that handles the event. Hence, an eventhandler.
int timecounter = 10;
DispatcherTimer timer = new DispatcherTimer();
public BlankPage()
{
this.InitializeComponent();
timer.Tick += timer_Tick;
}
protected void timer_Tick(object sender, object e)
{
timecounter--;
if (timecounter == 0)
{
//disable all buttons here
}
}
Try to read up on delegates to understand events Understanding events and event handlers in C#
Your code is expecting HandleTick to have two Object params. Not an object param and an EventArg param.
private void HandleTick(object s, object e)
NOT
private void HandleTick(object s,EventArgs e)
This is a change that took place for Windows 8.
WinRT makes use of Generics more than the standard .NET Runtime. DispatcherTimer.Tick as defined in WinRT is here:
public event EventHandler<object> Tick
While the WPF DispatcherTimer.Tick is here
public event EventHandler Tick
Also note that you don't have to use the standard named method to create an Event Handler. You can use a lambda to do it in place:
int timecounter = 10;
DispatcherTimer timer = new DispatcherTimer();
public BlankPage()
{
this.InitializeComponent();
timer.Tick += (s,o)=>
{
timecounter--;
if (timecounter == 0)
{
//disable all buttons here
}
};
}
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
datePicker1.SelectedDateChanged -= datePicker1_SelectedDateChanged;
datePicker1.SelectedDate = DateTime.Today;
datePicker1.SelectedDateChanged += datePicker1_SelectedDateChanged;
}
private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("datePicker1_SelectedDateChanged");
}
}
}
Why message box appears?
datePicker1.SelectedDateChanged -= datePicker1_SelectedDateChanged;
//Defines you use the RemoveHandler function. you might want to remove event handlers during the application lifetime
datePicker1.SelectedDate = DateTime.Today;
// You are Assigning to Datepicker Selected date
datePicker1.SelectedDateChanged += datePicker1_SelectedDateChanged;
// the event handlers on the relevant object are ready for user-initiated events at run time.
So The Message Box Came .
Third Line You Have Call The Eventhadler That Y it will come