Button Click event is getting called for holding event - c#

I have used command binding for the click event of the button. Now i also have a holding event on the button. So whenever i do holding on the button, click is also getting called along with holding event handler. I have tried setting
e.handled = true;
but that doesn't work. Any suggestions on why both the events are getting detected. If i use Tapped event instead of command binding, everything work fine. But my requirement is to use command binding for click event.
Edit: Below are some code behind
Code:
xaml :
Button Command={Binding ButtonClicked} Holding="Button_Holding"
xaml.cs
private void Button_Holding(object sender, HoldingRoutedEventArgs e)
{
}
ViewModel has the ButtonClicked Command

I am not an Expert in this stuff but i guess that Holding is a bubbling event and ButtonClick is a direct event(If somebody knows that for sure i would love to read a comment)
So your e.handled = true; doesn't apply to the ButtonClicked event.
I would say that you have to let your button clicked command know that the hold event was fired first and then ignore that event via the CanExecute method, or whatever you named it, or at least do something like
public void YourButtonClickedMethod()
{
if(SomeObject.IsHoldAllreadyExecuted)
{
SomeObject.IsHoldAllreadyExecuted = false; //set it to false for the next run
}
else
{
//DO the stuff you want to
}
}

Related

Cannot get KeyPressEventHandler in usercontrol

From the site that I saw on how to create a user control of keypad Click here for the link
. I successfully added the control in the tools. and drag one in the Form UI. when i start clicking the number. the RaiseButton is triggered:
private void btn1_click(object sender,eventargs e)
{
RaiseButton('1');
}
and on the other usercontrol.
There is this method
public event KeyPressHandler IsPressed;
public void RaiseButton(char Tosend)
{
KeyPressEventHandler handle = IsPressed;
handle(this,new KeypressEventArgs(Tosend));
}
The Class KeyPressHandler Ispressed value is null
so it cannot write to textbox. Now my question is, did I miss something that makes keypresseventhandler null?
Please advise thanks
The IsPressed event is null because nothing is registered for it. Once you register for that event it will no longer be null. Check for the event being null first. Also, you don't need to assign it to a variable.
public void RaiseButton(char Tosend)
{
if (IsPressed != null)
{
IsPressed(this,txtbox.text);
}
}
I solved it now. I was so stupid the event that I used in the form is not the same event that I used in the user control. The public event KeyPressEventHandler IsPressed event in the user control must be use also in your form. To be able to register this one, On your form where your you want to use the control. Click the user Control and look in the event property or just click the thunder symbol in your property. Look for the event IsPressed and now your good to go. double click and put the code that you want to do for the event. #wlemond thanks for giving me the example.
Cheers :)

C# TreeView SetFocus firing leave event twice

I have this simple code, where when the user leaves the TextBox control, TreeView gets focused:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add("A");
this.treeView1.Nodes[0].Nodes.Add("A.A");
this.treeView1.Nodes.Add("B");
this.treeView1.Nodes[0].Nodes.Add("B.A");
}
private void textBox1_Leave(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Leave..");
this.treeView1.Focus();
}
}
If we execute this code the Leave event is fired twice:
Leave..
Leave..
But if we set focus to other control, only one Leave event is fired.
Is that a problem of the TreeView? Do you know any workaround? Should we report this to Microsoft?
Thanks,
RG
this.treeView1.Focus();
Do not use the Focus() method in an event handler that's called because of a focusing event, like Leave. If you need to prevent a focus change then use the Validating event instead. Setting e.Cancel = true stops it.
But do note that this isn't very logical to do so for a TreeView, there isn't anything the user can do to alter the state of the control. You'll trap the user. Maybe that was the intention, do make sure the user can still close the window. If not then you might need the FormClosing event to force e.Cancel back to false.
Given that there is no code there to wire up the event I'm guessing you did it from the designer which means a line of code such as
textBox1.Leave += new EventHandler(textBox1_Leave);
will have been added to the Form1.designer.cs, check this file to ensure the line doesn't exist more than once as for each time this line is run you will get an event trigger, so if you run the line 3 times the Leave event will fire 3 times when you leave the textbox!
HTH
OneShot

How to stop textbox leave event firing on form close

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

ContextMenuStrip opened event doesn't fire after opeing event

HI everyone,
I have a UserControl with a ContextMenuStrip attached to it.
I want to display ContextMenu base on which object was clicked on the surface of the control.
The issue is that, at the first launch, when I right-click on the control's surface, the contextMenuStrip doesn't show up!
I set breakpoint inside both contextMenuStrip_opening and contextMenuStrip_opened event but it seems that only contextMenuStrip_opening get fired.
What happened?
In what situation the contextMenuStrip doesn't show up?
Please help.
I didn't do such things as marking e.Cancel = true or not assigning the control. If it was, I had fought out by debugging.
I don't know why, but I add e.Cancel = false at the beginning of ContextMenuStrip_Opening event handler then it works fine.
I had some problem with UserControl, but e.Cancel = false dont work for me
I use next
private void itemMenu_Opened(object sender, EventArgs e)
{
this.itemMenu.Focus();
}
If the symptom is that the ContextMenuString does not show at all, I would bet that your code assigns true to the e.Cancel property in the Opening event handler.
If the menu shows, but the Opened event handler is not invoked my guess would be that the event handler for the Opened event is not attached for some reason.
Although the problem has been a long time ago, it is hoped that those who need it in the future will be able to see it.
I had the same problem.
According to the source of ContexMenuStrip opening event:
CancelEventArgs openEventArgs = new CancelEventArgs(/*cancel=*/(DisplayedItems.Count == 0));
OnOpening(openEventArgs);
openingEventCancelled = openEventArgs.Cancel;
if (!openingEventCancelled) {
// do the actual work to open the window.
if (TopLevel) {
ReparentToActiveToolStripWindow();
}
//...other logics
finally{
if (!openingEventCancelled) {
OnOpened(new EventArgs());
}
}
when DisplayedItems.Count == 0, the opening eventArgs e.Cancel = true.
so it will be occurred when you set some ContextMenuItems visible to true in opening event, also set all ContextMenuItems visible to false in closing event or other after opening event.

DataGridView and the CellEndEdit Event

I have a DataGridView, and would like to hook into the CellEndEdit event. I've been able to successfully hook into the CellContentClick event, but am having issues with CellEndEdit.
I added the following code to my Form1.cs file:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1[0, 0].Value = "Changed";
}
With that code, nothing happens when I am done editing a cell. Is there anything else that I need to do to successfully hook into this event? I see that CellContentClick has a
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
line of code in the Form1.Designer.cs file, but I tried to mimic this for CellEndEdit, and received a compile error
(No overload for 'dataGridView1_CellEndEdit' matches delegate
'System.Windows.Forms.DataGridViewCellEventHandler')
You could implement this yourself.
In your constructor you could have a HookEvents() method which wires up such events.
Or, within the form designer, click the gridview to select it, go to the properties window and click the yellow thunderbolt to find a list of events. Then, scroll down and find the CellEndEdit event and double click it - this will wire up the event for you.
To wire it up yourself, it may look like:
class A : Form
{
public A()
{
Initialize();
HookEvents();
}
private void HookEvents()
{
dataGridView1.CellEndEdit += dataGridView1_CellEndEdit;
}
}
I doubt very much that your solution would work.
It's not a matter of where you place the subscription, is how you do it.
Brandon, you are declaring an EventHandler, that is the function responsible of doing what you want to do in case of that event "dataGridView1_CellEndEdit" but you are not subscribing to the event. Also in your function you are passing the wrong parameters.
The easy solution is either subscribe from the designer window or by code doing this:
write "dataGridView1.CellEndEdit +=" and then press the TAB buton twice. That shoud create the code for subscription to the event and the correct delegate to handle it.

Categories