Just out of curiosity, how do you stop the DataGridView.Click event from stealing the "thunder" from the RowHeaderMouseClick event?
When I click the ROW HEADER, the DataGridView.Click event is fired.
When I DISABLE the DataGridView.Click event, the RowHeaderMouseClick event is fired, as desired.
Apparently, the ROW HEADER is part of the DataGridView--therefore, I guess, technically, it is performing as it should.
Instead of the Click event, you may use the MouseClick event, which will allow you to easily perform a HitTest() to get information about where the click has occurred.
Here's an example:
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Console.WriteLine("RowHeaderMouseClick");
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.RowHeader) return;
// Row header clicks won't trigger the following line.
Console.WriteLine("MouseClick");
}
Technically, you could do the same thing with the Click event:
private void dataGridView1_Click(object sender, EventArgs e)
{
var cursorPos = dataGridView1.PointToClient(Cursor.Position);
var hitTestInfo = dataGridView1.HitTest(cursorPos.X, cursorPos.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.RowHeader) return;
Console.WriteLine("Click");
}
But there's really no reason to since MouseClick already gives you the location of the cursor out of the box.
Related
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 am new to WinForms events and I am getting a strange error.Well I write when I start my control:
this.MouseUp += MouseUpMethod;
But the problem is, when I release the mouse button out of my control, the program recognize as I release the mouse over the Control. I am not able to understand this error.
Did ever someone got this error?
It's because, by default, your control captures mouse. Just set Control.Capture to false somewhere in your MouseDown event handler, for example:
void MouseDown(object sender, MouseEventArgs e) {
this.Capture = false;
}
As alternative just check in MouseUp that mouse is still inside your control:
void MouseUp(object sender, MouseEventArgs e) {
if (ClientRectangle.Contains(PointToClient(Cursor.Position))) {
// Your code here
}
}
see, you need to associate event with event handler just after your InitializeComponent()
public Form1()
{
InitializeComponent();
this.button2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button2_MouseUp);
}
then your event handler should be
private void button2_MouseUp(object sender, MouseEventArgs e)
{
//Do stuff here
}
Hii I am making a windows Form application
I added a user control in that application
Is there a way to disable the right click event.
because if I use
Button.click += SomeMethod
It is going to someMethod in case of both (left and right) clicks.
I want to do this action in case of left click only.
Can't you handle which button has been clicked inside of the event?
private void uc_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//more logic here
}
}
You can detect which button of mouse was clicked inside event handler.
Check MouseEventArgs e argument of handler
You can check the button and call the native onClick for cases of left-button click.
[pseudo code]
SomeMethod(e){
if (e.Button == left){
control.onClick(e);
}
}
you could do number of ways;
1) if you want to use your button.click event then do the code below:
Button.Click += SomeMethod;
private void SomeMethod(object sender, EventArgs e)
{
System.Windows.Forms.Button bt = sender as System.Windows.Forms.Button;
if (bt != null)
{
if (bt.Equals(System.Windows.Forms.MouseButtons.Left))
{
// do something;
}
}
}
2) Or use the mouse clicked event in windows form "button.MouseClick".
Button.MouseClick += SomeMethod;
private void SomeMethod(object sender, MouseEventArgs e)
{
if (!e.Button.Equals(System.Windows.Forms.MouseButtons.Right))
{
// do work
}
}
I was hoping that we could use the e.Handled = true, but in this case neither of the event argument has the Handled property, therefore you have to manually check them.
I have to perform a cellcontent click event from a button (called Next button) which will invoke cellcontent_click event for the next row.That next button is not inside the grid view.
How can i do it. Please help.Thanks
There's no way to raise the event, but you can always call the event handler explicitly. This is, however, seen as bad coding practice, so you should put the common code in its own method:
void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) {
CommonClickBehaviour(e.RowIndex);
}
void btnNext_Click(object sender, EventArgs e) {
if ((dataGridView.CurrentRow.Index + 1) < dataGridView.Rows.Count)
CommonClickBehaviour(dataGridView.CurrentRow.Index + 1);
}
void CommonClickBehaviour(int rowIndex) {
// respond to the event or psuedo-event here.
}