Well, so first of all I am pretty new to programming in Windows Form Application, so if I lack knowledge I hope you'll understand.
The program I am currently doing is a virtual piano in Windows Form Application. There are buttons on the screen, each button represent a piano "button", if one is clicked - it plays a sound. Now, what I wanted to know is if there is any way that I can program my piano into detecting a continuous click and play sound until the button is "unclicked". For example, if one keeps on clicking on the "G" chord button it will keep playing the sound until he stops clicking.
If I didn't provide any necessary information I would love to know. Thanks in advance to all the answers.
The thing you are looking for is "hold button event".
You could handle the MouseDown and MouseUp events, something like this:
private bool buttonDown;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
buttonDown = true;
int num = 0;
do
{
num++;
label1.Text = num.ToString();
Application.DoEvents();
} while (buttonDown);
}
private void btn1_MouseUp(object sender, MouseEventArgs e)
{
buttonDown = false;
}
This will starts executing the code when you click the button and keeps executing it until you release the button
You need to use mousedown event. When mouse will enter the button the you need to raise the button click event unit the mouseup event fire.
Related
I would like to create a function like in Whatsapp for a WPF App with Win10 and with C#. I habe a normal button with some Text on it. As long as the button is pressed with hand touch or a stylus pen, the applikation should record and as soon as the button is untouched, it should stop the recording.
thats my code so far: I may add the I always youse the Preview-Versions of these Events
private void ButtonLiveDown(object sender, touchEventArgs e)
{
ButtonLiveOn();
}
private void ButtonLiveUp(object sender, TouchEventArgs e)
{
ButtonLiveOff();
}
private void ButtonLiveOn()
{
SetMicVolume(100);
isRecording = true;
}
private void ButtonLiveOff()
{
SetMicVolume(0);
isRecording = false;
}
for the recording, or better live announcement I use function in Windows systemsettings where you can say which microphone input schould be sent directly to the speaker
Now my Problem: I can start the speaking as soon as I press the button, but it doesn't matter if I hold the button or release him just directly after, The untouch event seems not to work, the Microphone is not set to 0. So I can't stop the live announcement. I want the function like in WhatsApp, the voice message is recording untill the button is released. What do I do wrong?
I have a TextBox in a WPF project where I am trying to detect a mouse click anywhere on the Application other than in the TextBox.
Here is the code that I have so far.
System.Windows.Input.MouseButtonEventHandler clickOutsideHandler;
public MyClass() {
clickOutsideHandler = new System.Windows.Input.MouseButtonEventHandler(HandleClickOutsideOfControl);
}
private void StartCapture() {
System.Windows.Input.Mouse.Capture(TextBox1, System.Windows.Input.CaptureMode.SubTree);
AddHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler, true);
}
private void HandleClickOutsideOfControl(object sender, System.Windows.Input.MouseButtonEventArgs e) {
ReleaseMouseCapture();
RemoveHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler);
}
The problem I'm having is that the event handler never gets called. I've tried capturing the return value of the Capture() function, but it shows as true. Can anyone show me what I'm doing wrong?
You could instead use LostFocus / LostKeyboardFocus but there has to be another element on the window that can get focus.
Second approach that does more what you what exactly ( yet doesn't make total sense) would be to attach to the global mouse down. Intercept every mouse click to WPF application
then on that one do a hittest and determine what is underneath.https://msdn.microsoft.com/en-us/library/ms608753(v=vs.110).aspx
WinForms application.
User touches a button (touchscreen) that causes authentication and the screen (A) is changed to another screen (B). If user continues to hold the finger on the screen (just for a second) the screen B has a button that overlaps with the button on screen (A) and the button on screen B inadvertently gets a touch and invokes an action that is not supposed to happen...
How do I do to prevent this from happening?
That is weird behavior. I do not have a touch device to test this but in a regular scenario the click only triggers when the button is pressed and then released while the button is still in focus. If you press the button and do not release, the click event will not fire.
Anyhow perhaps what you can do is only show screen B when the button has been pressed and then it has lost focus. Like this:
private bool buttonIsClicked = false;
private void button1_Click(object sender, EventArgs e)
{
this.buttonIsClicked = true;
}
private void button1_Leave(object sender, EventArgs e)
{
if (this.buttonIsClicked)
{
this.buttonIsClicked = !this.buttonIsClicked;
// show screen B
}
}
Please use a variable like I have used buttonIsClicked so the code in Leave event handler is not executed every time the button loses focus even if it is not clicked.
I am using following code in C# to add a Button
Button TextLabel = new Button(); //local variable
TextLabel.Location = new Point(0, 0);
TextLabel.Visible = true;
TextLabel.Enabled = true;
TextLabel.AutoSize = true;
TextLabel.Click += click;
this.Controls.Add(TextLabel);
And its click handler is
protected void click(object o, EventArgs e)
{
MessageBox.Show("hello");
}
Though the Button is visible and responding to mouse hover, but nothing is happening on its click. What could be wrong or missing?
If I write this same code in an independent project, it works!!!!! Strange. but why????
Form Properties: (if required)
1. Show in taskbar: false
2. Borderless
3. 50% Opaque
Today I realised that just registering click event for a control will not make any event to work unless its parent (in my case its form) on which that control is still active.
Parent control will receive event notification earlier than its child controls. This is a simple and obvious observation, but if not paid attention will make undesirable effects.
That's the mistake I did, I made another form active on my form activated event, hence any control in it didn't received events like mouse clicks.
Talking of 'hover effects are working', then yes, even if a form is inactive, hover works.
So I just removed the line of code that made another form active and everything is working fine now.
private void Form1_Activated(object sender, EventArgs e)
{
//if (form2!=null) form2.BringToFront(); //commented this
}
I am using Microsoft Visual C# 2010 Express to write a Window Form Application. I am writing a Sudoku program. I have written this program once before and got it working. I lost the source code due to a hard drive failure. I got a grid drawn on the form. I got the mouse event to work. I got key press event to work. I then I added several buttons to the form and got them to work. But then a problem occurs. After I added the buttons and got them working, the key press event stops working. Why is there a conflict between the button event and the key press event?
Here is the Code for a demo program that has the same problem.
‘code’
private void doMouseDown(object sender, MouseEventArgs e)
{
int i;
i = 0;
}
private void DoKeyPress(object sender, KeyPressEventArgs e)\\ This worked until I added
{
int i;
i = 1;
}
private void doClickButton(object sender, EventArgs e) \\ This
{
int i;
i = 2;
}
‘code’
The mouse event and keypress event was added to the form.
Looks like I need a way to set the focus on the form. The program will need to go back and forth between the mouse and the keyboard before the button is used.
Set your forms KeyPreview property to true
this.KeyPreview = true;