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;
Related
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
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
}
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.
I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}
using c# winforms vs2008
I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents.
My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired.
If the textbox does not have focus on form close then the form closes fine.
If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.
How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?
The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.
Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
if (closeButton.Focused)
return;
// Update the other text boxes here
}
}
The LostFocus event happens to fire after the new control receives focus.
Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.
Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.
Another approach:
Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to #Powerlord for pointing this out).
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.CausesValidation = false;
this.Close();
}
You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
}
you can check to see which control has just got focus.
private void textBox1_Leave(object sender, EventArgs e)
{
if (btnClose.Focused)
return;
// go from here
}
Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.
private void GuiltyTextBox_Leave(object sender, EventArgs e) {
Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;
.......
}
I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.
Write Following line of code in text box leave event on top
if me.closing then
return
end if