CarouselPage scrolling prevents ImageButton Released event firing - c#

I have a page which is a child of a CarouselPage. The page has ImageButtons on it which have pressed and released events.
Something happens when the pressed event fires and it stops when the released event fires.
The problem I have is when you push a button down and then slide left or right causing the carousel animation to take effect the released event from the currently held button no longer fires.
This is not an issue if the user switches to another page as I can use the page changed event to tidy up.
The problem is if you start sliding to the next page and then stop it pings back to the current one and no event fires at all.
I have looked for events that may fire in this scenario but have not found anything yet.
I need a way to either get the released events to still fire or a means of detecting a partial carousel scroll so that I can do the same from another event.
I am using the latest version of everything, testing in the emulator and developing solely for Android.
ImageButton definition
<ImageButton Grid.Row="0" Grid.Column="1" Source="up.png" Pressed="ImageButton_OnPressed" Released="ImageButton_OnReleased" BackgroundColor="Transparent" CommandParameter="U"></ImageButton>
Code behind
private void ImageButton_OnPressed(object sender, EventArgs e)
{
// Action start action
}
private void ImageButton_OnReleased(object sender, EventArgs e)
{
// Action stop action
}
Page Change event on CarouselPage
private void MainPage_OnCurrentPageChanged(object sender, EventArgs e)
{
// Action stop all actions on page
}

Related

How to allow button click events if window in the background

I have an application, ParentApp, which launches another application, ChildApp.
ParentApp displays a form while the ChildApp is running - just a kind of Childapp is currently running display with a Cancel button which kills ChildApp.
I want ParentApp to be unusable while ChildApp is running, so whenever someone clicks on ParentApp I want to bring the ChildApp to the foreground, which is fine.
So I've added this event handler to the ParentApp which responds to the Activated event of the form.
private void ParentAppForm_Activated(object sender, EventArgs e)
{
IntPtr childHwnd = _childApp.MainWindowHandle;
SetForegroundWindow(childHwnd );
}
(GotFocus didn't seem to work)
Unfortunately, if the user clicks the Cancel button on the ParentAppForm, the event handler for that button is never hit, because the Activated event fires first and sets the foreground window to another process.
Is there around this - to allow the button event to fire even though the application is not in the foreground?
As a quick workaround, you could check whether the mouse pointer in inside the area described by the Button.Bounds when the Form is activated.
You can translate the mouse pointer position to the Form.Bounds coordinates using PointToClient with the Cursor.Position coordinates.
Something like this:
private void ParentAppForm_Activated(object sender, EventArgs e)
{
if (this.button1.Bounds.Contains(this.PointToClient(Cursor.Position)))
ChildForm.Close(); //Or ChildApp.Kill()
else
ChildForm.BringToFront(); //Or SetForegroundWindow(ChildApp.Handle)
}

WPF Press any button to continue to next page

Got a simple 3 page program, but would like to make a welcome screen that users have to press a button to move to the next page (and start the workflow).
Users will mostly be using a barcode scanner (captures string & hits enter), so I'm trying to avoid buttons that need to be clicked with a mouse.
The setup is basically one MainWindow with a DockPanel in it with a Frame in it.
Pages get loaded to the frame on events(the enter being hit on a textbox). Pages have Grids in them with the standard controls.
I've got the frame in the window navigating to the welcome page, but can't seem to capture a keypress.
What would set I the event on? The Window, the frame, the dockpanel, the page, the grid?
Also, is there a way to specify "any key or click" event, maybe listen for any input action?
I've tried experimenting and looking at documentation but I can't seem to get it.
I created an empty WPF app with a frame and was able to catch a keypress event. Once the app starts I focus the frame. You can do the same once your page loads or when your workflow starts over.
<Frame Loaded="FrameworkElement_OnLoaded" KeyUp="UIElement_OnKeyUp">
<Frame.Content>
<Page>
<Label Content="Hello"></Label>
</Page>
</Frame.Content>
</Frame>
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
((Frame)sender).Focus();
}
private void UIElement_OnKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("Key pressed");
}
Add this to your designer:
this.myForm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.myForm_KeyDown);
And add this to your Form's code
private void myForm_KeyDown(object sender, KeyPressEventArgs e)
{
if(KeysDown().Any())
{
currentForm.Hide();
nextForm,Show();
}
}

Button clicked event not triggering after lost focus event

I have a Winforms c# form with some comboBoxes , cancel and save buttons that work fine.
I now need to capture when the user has finished entering text into a comboBox.
I add an empty ( for now) lostFocus (or Leave) event to the combbox , which triggers fine. However if the cause of that event was a cancel or save button press , the corresponding event is no longer triggered. These buttons still work fine if pressed at other times.
Should these two event be firing in sequence or is there some better way to capture completed text entry?
The Leave and/or LoseFocus events do not get triggered because you do not leave the combobox and because it doesn't lose focus when you press Enter or Escape.
Therefore the best way is to add the function you are triggering in the LoseFocus event, also to the Button click events of the Cancel- and the Accept-Buttons.
Adding a call to the leave event itself: comboBox1.Leave(null, null); would be the simplest way.
To make sure that the function is called only once, I check who has focus in the ButtonClick events:
private void acceptButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(acceptButton, null);
// do accept stuff here..
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(cancelButton, null);
// do cancel stuff here..
}
private void comboBox1_Leave(object sender, EventArgs e)
{
// do leave stuff here..
Console.WriteLine(sender);
}
I also pass in the Button so you could check the sender to see how the Leave was triggered..
I'm answering my own question here as I feel it might be useful to other newbies.
The breakpoint I had set in my empty lostFocus event was stopping button click event from occurring. When I removed the breakpoint the problem went away.
However when I added code to my lostFocus event, a form redraw was sometimes moving the buttons and preventing their events from firing. To solve this problem I adapted TaWs very useful answer and fired the button event from within the lostFocus event.
private void comboBox1_LostFocus(object sender, EventArgs e)
{
bool saving = btnSave.ContainsFocus;
// form redraw stuff here..
if (saving)
btnSave_Click(btnSave, null);
}

Stop action when button is released

In my WP8 app that controls Lego Mindstorms I have a Button with UIElement.Hold Event that triggers method runMotor() When I release the Button motor keeps on going but I would like it to stop. Method for stopping is stopMotor(), I've already tried to assign it to KeyUp Event but it doesn't work. Any solutions?
You can try to call stopMotor() in ManupulationCompleted event. Note that ManipulationCompleted event will get invoked after any gesture manipulation including Tap, Double Tap, Hold, and other gesture. Take that into account. If application scenario is still simple, checking if motor already running before calling stopMotor in ManipulationCompleted event handler maybe enough :
private void MyButton_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
if(isMotorRunning) stopMotor();
}

Handling PointerReleased events in Flipview

I'm trying to create an application containing a flipview that automatically flips to the next page when the user hasn't interacted with it for a while. This works fine using a basic DispatcherTimer that gets restarted when the flipview's selection changes.
So far so good, but I also don't want the timer to run when the user is interacting with the current item in the flipview, like a listview or something. I figured that I could just wire up a PointerPressed and PointerReleased handler to the page and stop the timer whenever a pointer is pressed, and restart it when a pointer is released.
This works, except when the pointer is on the flipview: the pressed handler gets executed, but FlipView gobbles up all the other pointer events, so the PointerReleased handler never gets executed.
I can't figure out how to get this to work. In WPF, I'd just use a tunneling event, but that whole concept seems to have disappeared with WinRT? Any advice on how to get this to work?
Update with code
Sure. I have a page containing a flipview and a dispatchertimer:
public sealed partial class MainPage : Page
{
private DispatcherTimer slideScrollTimer;
public MainPage()
{
// Set up a timer that'll flip to the next page every 5 seconds.
this.slideScrollTimer= new DispatcherTimer()
{
Interval = TimeSpan.FromSeconds(5)
};
slideScrollTimer.Tick += slideScrollTimer_Tick;
slideScrollTimer.Start();
}
void slideScrollTimer_Tick(object sender, object e)
{
// When the timer runs out, go to the next page, or back
// to the first.
if (flipView.SelectedIndex < flipView.Items.Count - 1)
{
flipView.SelectedIndex++;
}
else
{
flipView.SelectedIndex = 0;
}
}
private void flipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// restart the timer if someone flips to a different page
if (this.slideScrollTimer != null)
{
this.slideScrollTimer.Start();
}
}
}
Basically what I want is for the timer to reset whenever someone touches the application. I tried adding a PointerPressed/PointerReleased handler using AddHandler, but released only fires if you're not on the flipview, or just tapping it instead of scrolling it or manipulating its contents.
You can use UIElement.AddHandler to handle events that have already been marked as handled elsewhere.
Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element. Specify handledEventsToo as true to have the provided handler be invoked even if the event is handled elsewhere.

Categories

Resources