So, I'm trying to build kind of a sampler. Just for fun. I'm kinda at a loss here;
I've created a grid of 4x4 in wpf and filled them with buttons. Each button has a corresponding mediaplayer (so they would not cut off the other audio when triggered)
I've added a clickevent for each button and a keydown event as wel. Now, the click events all work fine. But the keydown event only works for the last button I clicked on. The rest is unresponsive. Can anyone tell me how to fix this?
a keydown event:
private void Pad_1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad7)
{
playPad(mediaPlayer1, mp1);
}
}
and my playPad method:
public void playPad(MediaPlayer mediaPlayer, string mp)
{
if (mp != null)
{
mediaPlayer.Open(new Uri(mp));
mediaPlayer.Play();
}
}
Thanks in advance!
Related
I handled an event "LostMouseCapture" on my wpf text box . Now I need to implement functionality that if this event got fire because of click of one specific button then I want to do something and if this event got fire because of some thing else then i want to do some other thing.
I am blank how to achieve this functionality
private void txtEcode_LostMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
{
//if the event is fired because of click of "btnEcode" then do this
{
MCondsViewModel.McondsNo = MCondsViewModel.EcodeValueOnMouseFocus;
}
else
{
//do this
}
}
I want a button to function like this:
if button is down(mouse button is hold down)
{
bool trySmthing = true;
}
if button is up
{
bool trySmthing = false;
}
I tried some stuff with KeyUp and KeyDown events but they don't give the right result.
Apparently the focus must be in your application; a tutorial on handling the mouse events via the .NET framework in C# can be found here. The MouseDown event, as documented here and the MouseUp event, as documented here, are of particular interest..
This should help: Micosoft Library for C# Mouse-events
void MouseUpHandler(Object sender, RoutedEventArgs args)
{
// This method is called whenever the PreviewMouseUp event fires.
}
void MouseDownHandler(Object sender, RoutedEventArgs args)
{
// This method is called whenever the PreviewMouseDown event fires.
}
I have created event for KeyDown which also triggers the mouse event. I dont want the mouse event. What can i do. Here is my sample code,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString().Equals("Up"))
{
//...
}
if (e.KeyCode.ToString().Equals("Down"))
{
//...
}
}
This works fine, but whenever i scrool the scrollbar of the mouse it triggers the respective events. What can i do now.
Thanks in advance...
I have seen some topics about this subject in Objective-C. I read a lot of them, spent 2 days on it on trying to find a solution and none of them worked for me. I am mainly coding in C#. Since my problem behaviour (fire only when leaving/re-enter button) and context (C#) is a bit different. So, I will try my chance by asking my question here.
I will try to keep it simple.
Here is a sample of code:
private UIButton _buttonTest;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_buttonTest = new UIButton(new RectangleF(10, 70, 50, 50));
_buttonTest.SetTitle("0", UIControlState.Normal);
_buttonTest.TouchUpInside += HandleButtonTestTouchUpInside;
_buttonTest.BackgroundColor = UIColor.Red;
this.View.AddSubview(_buttonTest);
}
void HandleButtonTestTouchUpInside (object sender, EventArgs e)
{
string textNumber = _buttonTest.Title(UIControlState.Normal);
// Increment Number
_buttonTest.SetTitle((int.Parse(textNumber)+1).ToString(), UIControlState.Normal);
}
There is a button and the title text is set to "0".
When the user click on it, it increments the number (title) by 1.
This code usually works very well!
However, it does not work in some of my classes for some unknown reasons...
Here is the problem:
TouchUpInside does not fire. ... However, if I hold the button with a finger and keep holding the finger while leaving the button and then re-entering the button then release the button, then.... it will fire the TouchUpInside, .... so the finger need to leave and re-renter the button while holding the button to make the TouchUpInside fires. Usually, this code works very well.
Things Checked:
if I replace TouchUpInside by TouchDown, then TouchDown works.
'User Interaction Enabled' is set to True for all superviews.
All views frames (among the superviews) seem to be inside their superview frames.
I moved the _buttonTest around and I noticed that the _buttonTest TouchUpInside does not fire correctly in the View, the SuperView, the SuperSuperView.... but it fires correctly in the SuperSuperSuperView.
Any suggestion?
In the SuperSuperView, I had this tap gesture that was entering in conflict with the Button event.
// Tap Gesture
UITapGestureRecognizer tapPageGestureRecognizer = new UITapGestureRecognizer();
tapPageGestureRecognizer.AddTarget(this, new Selector ("HandleTapPageGestureRecognizer:"));
this.View.AddGestureRecognizer(tapPageGestureRecognizer);
The idea is to disable the gesture SuperSuperView gesture when the button event, TouchDown was fired, ... and to re-enable it when the TouchUpInside is fired.
So here is one solution for the problem:
private void SetGrandParentViewGestureEnabled(bool enabled)
{
foreach(UIGestureRecognizer g in this.View.Superview.Superview.GestureRecognizers)
{
g.Enabled = enabled;
}
}
void HandleButtonSubmitTouchDown (object sender, EventArgs e)
{
SetGrandParentViewGestureEnabled(false);
}
void HandleButtonSubmitTouchUpInside (object sender, EventArgs e)
{
// => Do treatments here!
SetGrandParentViewGestureEnabled(true);
}
However, I could have used EventHandler or Action to enable/disable the tap gesture.
EDIT: Here is another function that need to be added as well to re-enable the gesture.
void HandleButtonSubmitTouchUpOutside (object sender, EventArgs e)
{
SetGrandParentViewGestureEnabled(true);
}
I had a similar problem, I ended up using a tap recognizer together with the long press recognizer like this for the button TopRightButton.
var longPressGesture = new UILongPressGestureRecognizer(Action);
TopRightButton.AddGestureRecognizer(longPressGesture);
var tapRecognizer = new UITapGestureRecognizer();
tapRecognizer.AddTarget(() =>
{
//method
});
tapRecognizer.NumberOfTapsRequired = 1;
this.TopRightButton.AddGestureRecognizer(tapRecognizer);
private void Action(){ };
I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".
It just feels like there is a better way to do this. So the question is, is there?
I thinks that's the way to catch Key.Enter.
Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.
If you only care about catching Key.Enter for a single control then your approach is correct.
You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").
Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.
This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) SomeButton_Click(this, null);
}
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
{
cmd.Execute(null);
}
}
I use the following implementation when using the command pattern:
private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty);
if (b != null)
b.UpdateSource();
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
cmd.Execute(null);
}
}
When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.
Of course you can catch the event on a higher level than the textbox.
Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.
And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.