ContextMenuStrip opened event doesn't fire after opeing event - c#

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.

Related

C# Winform panel Shown event

Is there an event for panel that is equivalent to form event Shown?
I had a few couple of panel switching within a form which will never be closed.
However i couldn't find anything close to an event like Shown which is used in form.
The closes i had is Paint event. However i only wish to update the panel once every time it is shown.
Form.Shown is not raised every time the form is shown, rather it Occurs whenever the form is first displayed. This being said, there is no Panel.Shown event, and no event which is raised "whenever a panel is first displayed".
You can simulate this behavior with the Panel.Paint event, using a flag to keep track of whether it's been "shown" once before. This will make it behave similar to Form.Shown.
private bool panel1Painted = false;
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (!panel1Painted)
{
// do your shown stuff here
panel1Painted = true;
}
}
To keep in the spirit of Form.Shown, you may want to reset the flag if the Panel is reconstructed. This is not the same as shown.
You could listen on the VisibleChanged event and only act on when visibility = true.
https://msdn.microsoft.com/en-us/library/system.windows.forms.panel_events%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
You could also experiment with the Enter and Invalidated events to see if these give you the results you want.
Or if disabling the panel when leaving it is an option, you might be able to use the EnabledChanged event in your toolbox.

Button Click event is getting called for holding event

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
}
}

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

C# Form Move Stopped Event

Is there any event in C# that fires when the form STOPS being moved. Not while its moving.
If there is no event for it, is there a way of doing it with WndProc?
The ResizeEnd event fires after a move ends. Perhaps you could use that.
This is not a failsafe solution, but it's pure .NET and it's dead simple. Add a timer to your form, set it to a relatively short delay (100-150 ms seemed OK for me). Add the following code for the Form.LocationChanged and Timer.Tick events:
private void Form_LocationChanged(object sender, EventArgs e)
{
if (this.Text != "Moving")
{
this.Text = "Moving";
}
tmrStoppedMoving.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
tmrStoppedMoving.Start();
this.Text = "Stopped";
}
If you want more exact handling (knowing exactly when the mouse button is release in the title bar and such) you will probably need to dive into monitoring windows messages.
I had the same problem with a user control, but it does not have the ResizeEnd event. The solution, which worked is to override the WndProc method and listen for EXITSIZEMOVE.
See example here
Just set a flag to true when onmove events are fired. If a mouseup event happens and the flag is true, the form stopped being moved.
I admit this probably won't work in the case of a user moving a form via the keyboard, but that's pretty rare.
I tested ResizeChanged event, and it works fine, however I don't know relation between move and resize, but it works for me

Categories