Action on both button press and release - c#

I can't get this straight: I have one button that I want one action for the press event and one action for release, I've searched everywhere and can't find an answer.
KeyDown, KeyUp or MouseLeftButtonDown doesn't work with button on windows phone 7.
First I tried combining GotFocus and Click clickmode release like this:
(As you can see I want Image1 to be shown while pressing button, and hidden when releasing the button)
xaml:
Button Click="button1_Click" ClickMode="Release" GotFocus="button1_GotFocus" Content="byt" Height="72" Margin="0,500,6,0" Name="button1" VerticalAlignment="Top"
private void button1_GotFocus(object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
}
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Visible;
}
This works only one time and could work all the time if I could loose focus from the button when releasing it (tried searching for that as well)
The other thing I tried was changing the clickmode while pressing the button, but didn't get that to work either..
something like this:
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
button1.SetValue(Button.ClickModeProperty, ClickMode.Release);
Image1.Visibility = System.Windows.Visibility.Visible;
}
(I know that the syntax is wrong somehow in the second one)
Would be grateful for help!

MouseLeftButtonDown / MouseLeftButtonUp do work on WP7. Obviously not named the best, but they do work on the device.
<TextBlock x:Name="ApplicationTitle" MouseLeftButtonDown="ApplicationTitle_MouseLeftButtonDown" MouseLeftButtonUp="ApplicationTitle_MouseLeftButtonUp"
Style="{StaticResource PhoneTextNormalStyle}"
Text="MY APPLICATION" />
You'll see Down gets fired, and then immediately Up.
private void ApplicationTitle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
private void ApplicationTitle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}

Perhaps all you need is MouseLeftButtonDown/Up.

I used
Click="{x:Bind UpArrow_Click}"
ClickMode="Press"
PointerCaptureLost="Button_Release"
on a button. When button is pressed UpArrow_Click event occurs... when button is released Button_Release event occurs

Related

Programmaticaly click a button with it's UserControls

Like you can see here, i got a button with Name, etc. and i got in this button a usercontrol. When i click on the button everything is alright, but when i programmaticaly click this button. The UserControls will not be performed... why?
<Button x:Name="btnHome" Style="{DynamicResource PopupButtonStyle}" MouseEnter="btnHome_MouseEnter" MouseLeave="btnHome_MouseLeave" Click="btnHome_Click">
<ctls:MenuItem GroupName="MenuItem" IndicatorBrush="{DynamicResource PrimaryBlueColor}" Icon="{DynamicResource home}" IconWidth="16" Text="Home" VerticalAlignment="Center"/>
</Button>
This is the code for clicking the button, am i wrong here?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
btnHome.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void btnHome_Click(object sender, RoutedEventArgs e)
{
}

How to click a Button inside of a Button? C# WPF

I created some buttons with another button inside. Then I want to click the inner button but it only throws me the outer button click event, not even both. I tried the solutions in this question but didn't achieve anything. The exact goal that I have is to only load the click method of the inner button when i click it, and if I click in wherever else in the outer button it throws me the respective method.
//Outer button click event
void newBtn_Click(object sender, EventArgs e) {
//Stuff
}
newBtn.Click += new RoutedEventHandler(newBtn_Click);
//Inner button click event
void editButton_Click(object sender, RoutedEventArgs e)
{
//Stuff
editButton.Click += new RoutedEventHandler(editButton_Click);
e.Handled = true;
}
//stuff
It would be better to share your code asking a question.
By the way this sample should work:
<Button Background="Red" Width="100" Height="50" Click="Button_Click">
<Button Background="Green" Margin="10" Width="50" Height="50" Click="Button_Click_1" />
</Button>
In the event handler of the inner button simply add this, as suggested by the post you mentioned:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
This will stop the chain of Routed Events (Bubbling and Tunneling)

Windows phone 8.1 textbox multiple click

If a textbox has focus and I want to be able to select it again is there a way to do this.
So first click the background turns blue and while it is still selected I press again the background turns green. How do I catch the second press even though its already selected?
You can subscribe to the PointerEntered and the SelectionChanged events. The first one is always fired when the pointer hits the TextBox. However if it contains text and you tap on it you will eventually select the text. The SelectionChanged handler will take care for that.
Your XAML markup looks as follows:
<TextBox x:Name="tb"
Text="Test"
PointerEntered="TextBox_PointerEntered"
SelectionChanged="TextBox_SelectionChanged"
GotFocus="TextBox_GotFocus"/>
The code behind file contains the following code:
private void TextBox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Blue);
}
You will have to adjust the code to your needs and take care of special cases where both SelectionChanged AND PointerEntered are fired (at this point both handlers do the same, so there's no problem).

Programatically set Focus to a TextBox in Windows Phone

In a Windows Phone app I have an TextBox and a Button. The user writes some text to the TextBox and taps the Button, the text from the TextBox is added to a list. The TextBox loses focus after the Button is tapped.
What I want to do is to set the focus back to the TextBox after the Button is tapped so the user can continue writing another text without needing to tap the TextBox.
I tried calling the Focus() method of the TextBox in the Button handler but this does not work. is there another, if any, way to do this?
When Button clicked try to add bollean flag = true. Then check this flag on event OnTextBoxLostFocus.
<TextBox x:Name="tb" Grid.Row="1" LostFocus="Tb_OnLostFocus"/>
<Button x:Name="btn" Click="Btn_OnClick" />
public partial class MainPage : PhoneApplicationPage
{
private bool flag;
public MainPage()
{
InitializeComponent();
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
flag = true;
tb.Focus();
}
private void Tb_OnLostFocus(object sender, RoutedEventArgs e)
{
if (!flag) return;
tb.Focus();
flag = false;
}
}
Hope its help.
I have tried a lot of solutions, but this is the only one that works for me (Windows Phone 8.1 app).
First catch your TextBox's Loaded event, then call Focus(FocusState.Keyboard).
private void myTextBox_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus(FocusState.Keyboard);
}
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
What happens if you call:
yourTextBox.Select(0,0)
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
The way that it worked best for me on the phone was, if I wanted to focus on a particular textbox when the page loaded:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());
}
or if I just wanted it at a certain point. Just repeat these two lines:
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());

Silverlight C# - Simulate a Click event on a button

I have an element with a Click method.
I would like to activate that method (or: fake a click on this element) within another function.
Is this possible?
No, there currently isnt a way to do this. See links:
https://stackoverflow.com/questions/4137528/ui-automation-for-wp7
windows phone 7: How to simulate control click programmatically
EDIT
This doesnt do exactly what you wanted to do, but you can do something like this and get the same outcome. I do this type of pattern alot in my code to do get the same outcome.
XAML
<Button Name="testClick" Click="testClick_Click">
<TextBlock Text="Click Me"></TextBlock>
</Button>
C#
private void testClick_Click(object sender, RoutedEventArgs e)
{
TestFunction(sender as Button);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Call same function as button
TestFunction(testClick);
}
private void TestFunction(Button bt)
{
//do stuff
}
Somewhere in your class initiate a RoutedEventArgs:
private RoutedEventArgs evnt;
Then later call this:
element_Click(element, evnt);
That should do it.

Categories