I am using windows forms. I have a problem in handling mouse events. In my application there is menu bar on top. In menu bar I have several toolstripmenu items. I want that when toolstripmenu item is highlighted, it shows some description in label. I am totally confused. What event I used to handle this problem. I used mouse enter, mouse move & mouse leave event but when mouse enters in the area of any menu item, its alright. The label shows some description when mouse enters or mouse move on the item. But when I move mouse on other area of form. The label remain shows the description about an item. I want that when mouse leaves the area of toolstripmenu item, the label goes blank. Help me to solve this problem
I use it for toolstripmenuitem. Mouse Enter & Mouse move event works fine but the main problem in mouse Leave event. When I move mouse on "open" option it shows description in label but when my mouse leave this option or leave the visible part of this option. The label description is same. Actually mouse leave event fires when any other control is in the focus after leaving "open" option. For Example I have menustrip & in menustrip I have several options. For Example First option is "Open" & I want that When mouse enters in visible part of "open" option its shows description in status bar lable "Open Files" & when my Mouse leave the visible part of "open" option, statusbarlabel goes blank but problem is that I can't understand properly when mouse leave event fires. Thanks for replying this question.
Here is my code. Take a look & tell me whats the problem?
private void openToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
label1.Text = "Open files";
}
private void openToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
label1.Text = "";
}
Use the MouseLeave event in your menuStrip and set the label text to "".
or use the same mouse event in your form and set the label text to " "..
I mean :
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = "";
}
you can use this solution if you want to show a text when the mouse is on the form.
Use the MouseLeave Event. In the same way you did MouseOver, when you catch a MouseLeave of your menu item, change the Label's Text property to "" (i.e. blank)
Related
Just wondering if someone else can recreate this or is it just me :)
Drop a panel on the form
Put a button on the panel
Create click of button "I am Button"
Above works fine
Add onmouseleave to panel set button visible to false
Add onmouseenter to panel set button visible to true
Now if you move mouse in and out of panel the button will become visible false. Pefect.
Now move mouse back intp panel and button becomes visible. Perfect.
Clicking on button now does nothing (Lost on Click Event?)
Breakpoint in onclick never fires.
Is this a concept that cannot be done?
'Update' It wont let me post long comment.
First of all thanks for the quick responses.
I did not include code because this was in a very large project so i recreated it with a brand new app.
C# WinForm with one panel and one button on the panel.
I did not try to click on an invible button. Not only can i see the button but can see it respond to bing clicked.
#steve. Good point, The button is well inside of the panel but i see where you are going. I just performed a new test.
If i move the mouse in and out of the panel the button will show and hide perfectly.
After this, hitting enter on the button will excute onclick but the mouse will not. (I suppose this has something to do with only one button and it is default.)
What prompts the mouse to not fire the click event is beyond me.
Very Strange.
What i am trying to achieve:
An area on the screen that when the mouse enters this area a group of buttons appear so they can be used.
when the mouse leaves this area the buttons will dissapear. As my original post stated button works fine if visiblity does not change.
I suppose the your code has the following problem:
You receive the mouseenter event on the panel and you make the button
visible
You move the mouse over the button
This last action triggers the mouseleave event on the panel.
As a consequence your button becomes invisible.
But making the button invisible triggers a mouseenter over the panel and in a blink the button is again visible and causing a mouseleave on the panel.
An infinite loop between these two events starts.
So the click event is not lost, simply it will never trigger because the form engine is busy hiding and showing the button control.
I have reproduced the situation and fixed it adding an event handler for the MouseMove event. In this event I set a variable to block the infinite loop between the two events...
This is my example to adapt to your real code, try it in LinqPad
public class Form1 : Form
{
Button b1 = new Button();
Panel p1 = new Panel();
bool stillOnPanel = false;
public Form1()
{
b1.Text = "ClickMe";
b1.Click += onClick;
b1.Visible = false;
p1.MouseEnter += onEnter;
p1.MouseLeave += onLeave;
p1.MouseMove += onMove;
p1.BorderStyle = BorderStyle.FixedSingle;
p1.Size = new Size(150,100);
this.Controls.Add(p1);
p1.Controls.Add(b1);
}
void onClick(object sender, EventArgs e)
{
Console.WriteLine("Clicked");
}
void onMove(object sender, MouseEventArgs e)
{
//Check if we are still over the panel area
stillOnPanel = b1.ClientRectangle.Contains(p1.PointToClient(Cursor.Position));
}
void onEnter(object sender, EventArgs e)
{
if (!b1.Visible)
{
b1.Visible = true;
}
}
void onLeave(object sender, EventArgs e)
{
// Do not hide the button if we are over it, let it click....
if (b1.Visible && !stillOnPanel)
{
b1.Visible = false;
}
}
}
I have created a Windows Forms application, but I get strange behaviour from the Capture property of a control.
On a blank form, with a single label called "label1" and the code
public Form1()
{
InitializeComponent();
label1.MouseDown += pictureBox1_MouseDown;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Debug.WriteLine(label1.Capture);
label1.Capture = !label1.Capture;
Debug.WriteLine(label1.Capture);
}
I observed, that the first WriteLine always says "True", the second one always says "False" when the button is clicked. Even when clicked multiple times.
The label never reacts to clicks outside its border, not even when I use label1.Capture = true instead.
Am I misunderstanding the expected behaviour of the "Capture" property? I expected the initial value to be false, and the label not to react to clicks outside, after the first click I expect the value to be true, and the label to react to all mouse clicks, even outside its borders.
In a MouseDown event, the Capture for the control always set to true initially. So normally if you perform a MouseDown and then without releasing mouse button move your mouse out of the control and then release mouse button, the MouseUp event of the control will be fired.
If you set Capture to false in MouseDown, then the mouse up event of your control will only fire if your mouse is over the control and if you move the mouse out of your control and then release mouse button, the MouseUp event of your control will not raise.
Also in MouseUp the capture will be released automatically.
For more information take a look at these resources:
WmMouseDown method source code
WmMouseUp method source code
CaptureInternal proprty source code
SetCapture documentations
ReleaseCapture documentations
I'm using PromptTextBox that will show prompt text inside itself when Text is empty and IsKeyboardFocusWithin is false.
The problem is when I start new window, it automatically keyboard focus on the first TextBox so the prompt text isn't shown(but this behavior is acceptable).
If I want to un-keyboard focus I must click on another controls(e.g. click a Button, click on another TextBox), I can't click on blank space to un-keyboard focus.
I also test on normal TextBox, it's behavior is the same. so the question is:
How can I un-keyboard focus once I'd keyboard focused on TextBox by click on blank space?
In the MouseDown event handler for the 'blank space', focus on some other control, if you have any. Or else, you need an invisible/out-of-view-bounds TextBox to focus on:
<blankSpaceControl>.MouseDown += ClearFocus;
void ClearFocus(object sender, MouseEventArgs me)
{
<yourOtherFocusableControl>.Focus();
}
There is no other way. Focusing the Window itself will pass the focus on to the first focusable child control.
Just use Keyboard.ClearFocus()
https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.clearfocus.aspx
I have a ListView control on my form. I have set its display mode to LargeImageList. I need to handle the items inside this control. So I have written code for its click event. But I see now that this event is not triggered when I click in an empty area inside it.
How can I make my ListView aware of the clicks on its area regardless.
To capture mouse clicks on the "white space" around the ListView items, you will need to use the MouseDown/MouseUp events.
This will also capture clicks to the items as well.
I've used the Global Mouse Hook for similar issues. You can use it to detect Mouse Clicks anywhere on the screen, then just check the click was within the listview control bounds.
Grab the code from Global Mouse Key Hook
IKeyboardMouseEvents m_GolbalHook = Hook.GlobalEvents();
m_GolbalHook.MouseClick += m_GolbalHook_MouseClick;
private void m_GolbalHook_MouseClick(object sender, MouseEventArgs e)
{
if (listView.Bounds.Contains(e.Location)) && (e.Button == System.Windows.Forms.MouseButtons.Left))
{
//Do Stuff
}
}
While developing a simple Windows Form UI applications, I am trying to create an effect to show and close dropdown on mouse events.
Like I can open the dropdown on MouseMove event by setting comboBox.DroppedDown = true;
However, this same is not working when I set comboBox.DroppedDown = false; on MouseLeave event to close it.
No idea what exactly is needs to be done here.
The problem is on MouseLeave the dropdown does not lose focus and hence unless you select one item from list, it does not close. It waits for user to select an item from list.
If it can lose focus on MouseLeave, would work.
Any suggestions please.
First of all I must say that I am not an experienced programmer and I just started with WPF.
I know this question is two years old but I had the same issue and I found I can close the drop down list of the ComboBox using the event IsMouseDirectlyOverChanged. What was really annoying for me was that I had a ComboBox and a button, and If the drop down menu was opened without making a selection and I wanted to click the button, nothing happens at the first click because at the first click the drop down menu was closing. After this I could click on the button.
For me it's working fine: the drop down list close if I move the mouse in any direction (up, left, down, right) and a message is append to a textbox control. I don't know if this event is something new or it could be used 2 years ago too.
private void comPortList_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (comPortList.IsDropDownOpen==true)
{
txtMsgBox.AppendText("MouseDirectlyOverChanged\n");
txtMsgBox.ScrollToEnd();
comPortList.IsDropDownOpen = false;
}
}
This event triggers when your mouse pointer is over the opened ComboBox. If you don't open the drop down list, it will not trigger.
Another thing that I've seen is that this event triggers when you enter over the opened ComboBox and also when you leave it. If I append the text before checking if the IsDropDownOpen property is true, the text "MouseDirectlyOverChanged" will appear twice in my textbox when I the mouse pointer leaves the oppened ComboBox.
If i comment the line:
comPortList.IsDropDownOpen = false;
and leave the AppendText and ScrollToEnd before if, the text will append only once.
I hope this helps :)
It sounds to me like you need to be using the MouseEnter event and not MouseMove. The reason it wouldn't work on MouseLeave is because your mouse is moving, and that will just set it to true again.