To determine the content of my context menu I need to detect when the user clicks into the blank area of a treeview.
But, neither the Click nor the MouseClick events fire.
Is it possible to achieve the desired functionality by deriving from TreeView?
If not, what can I do?
You can use the MouseDown event, that fires even in the empty area.
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
// MessageBox.Show("treeView1_MouseClick");
}
private void treeView1_Click(object sender, EventArgs e)
{
// MessageBox.Show("treeView1_Click");
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("treeView1_MouseDown");
}
Related
Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
I am working in mouse_down event.
private void treeView_MouseDown(object sender, MouseEventArgs e)
Now, In mouse_down event How to check user clicked expand/collpase icon not node?
Note : Before_Expand and Before_Check events will not work bcoz mouse_down event fire first.
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
var Test = treeView1.HitTest(e.Location);
if (Test.Location == TreeViewHitTestLocations.PlusMinus)
{
//You can check here
}
}
I have many controls in my form. for example 120 labels in one panel. and i want when user clicked on each label just call same function with same parameter.
Now i used like this :
private void label67_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label66_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label65_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label64_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
now can i make easy way to call ChangeToTextbox function when user clicked in any label?
Add the same OnClick handler for all labels on the panel:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.OfType<Label>().ToList().ForEach(l => l.Click += label_Click);
}
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Find all your controls then just add the handler via code;
List<Control> controls = GetAllMyControls();
foreach(Control control in controls)
{
control.OnClick += (o, e) => { ChangeToTextBox(o); }
}
The syntax should be very similar for both web and winform solutions.
It can be easily achieved by using following approach: Pls give a try,
1) Go to Windows Forms Designer and click the first Label control to select it. Then hold down the CTRL key while you click each of the other labels to select them. Be sure that every label is selected.
2) Then go to the Events page in the Properties window. Scroll down to the Click event, and type label_Click in the box
3) Press ENTER. The IDE adds a Click event handler called label_Click() to the code, and hooks it to each of the labels.
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Reference: http://msdn.microsoft.com/en-us/library/dd553231.aspx