Best way to hide a popup menu? - c#

I have a control that creates a popup menu on right click, and I need the best way to close it on left click. Besides handling the mouseDown event of everycontrol in my application, what is the best way to close the popup menu?
Like a global mouse click event....
private void lbKeywords_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
listBoxMenu.ShowPopup(Cursor.Position);
}
}

As an answer: try asking DevExpress or looking at their support pages - i.e. I found this after a quick search: devexpress.com/Support/Center/p/CQ14210.aspx

Related

Perform mouse click on TextBox or window

I know that this is not a good practice, but I would like to simulate a mouse click event on a textbox, or a window in my wpf application.
I need this, because I am calling this application with some kind of interceptor and the window doesn't get the full focus, it is in front, textbox has a focus but u can't directlly type on textbox.
on window load I call these methods
this.Activate();
this.Topmost = true;
TextBox1.Focus();
Keyboard.Focus(TextBox1);
Thank you.
I had the same issue in VB. i just wanted the focus in the SpinEdit1 (or any control) when the form load. Here is:
private void formX_Load(object sender, EventArgs e)
{
this.ActiveControl = SpinEdit1;
}

Remap left click to right click c#

Hey folks having issues finding any info on how I might remap the left click event in C# to be a right click event.
For example, everytime the user left clicks on a DataGrid in my application I want to be able to capture the left click and map it to a right click. So how do I simulate a right click every time the left click on the mouse is used. I've found some old .net2.0 examples where some C++ was ported over to C# to allow simulating clicks but I don't think it would be suitable and I don't truly understand what it was doing.
Any help appreciated. Thanks!
Just handle the mouse click event of the Datagrid
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
contextMenuStrip1.Show();
}
else if (e.Button == MouseButtons.Right)
{
//What you want it to do
}
}
When handling the event, check if it is a right click or a left one. No need to map the mouse clicks.

How do I handle right click on RichTextBox?

How do I handle right click in RichTextBox in C#?
For example:
When I right-click mouse over a RichTextBox the program will display a MessageBox.
You have to catch Mouse_Down event, not Mouse_Click as follows:
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("you got it!");
}
}
Subscribe to the MouseClick event and check the MouseEventArgs.Button property to see which button was clicked.
What have you tried for yourself?
click RichTextBox in design mode .Right click and select properties and Then navigate to MouseDown Event.Double click that event and you will be directed to Code behind page and there you can write your code.

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

Click and drag event handler

I have an app with a custom window (transparency and no borders). I made a header with a dragmove behavior on left mouse button down. This allows me to drag the window to the top so it maximizes. Now I want to write the code so that when I click the header and drag it, it restores the windowstate to normal...
Is there a click & drag event handler, or another way?
EDIT: Platform C#, in WPF
You need to use Window.StateChanged Event
The best way to handle Maximalization and Minimalization is to manipulate WindowState Property. It saves the Window.RestoreBounds property with previous size. If you need more sophisticated solution
here is an example
Ps. Similar to Win 7 feature. Maybe there is no need to do so? :)
Edit: in UIElement there is MoveMove event
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
MainWindow1.WindowState = WindowState.Normal;
}
}
this is a bit messy since event is going to fire everytime you move it

Categories

Resources