DataGridViewButtonColumn Handler Never Called - c#

I am creating a simple Windows Form with a DataGridView that contains a total of 6 columns. Only one of these columns (the very first) is a button column. I am having trouble with the handler for when the button, or rather, cell is clicked.
For reference (if it matters), the name of my DataGridView is infoGrid.
The only operation that I have the handler perform is a simple popup window, just so I can know the handler is being correctly called. I also have not added conditions yet to ensure it was the first column that was clicked, but I do plan on implementing conditions when I can get the handler to properly work. This is what I have:
private void infoGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("It's working.", "Test", MessageBoxButtons.OK);
}
After spending a couple of hours scouring the internet for an answer, I noticed that just about all examples on this website and others use dataGridView1_CellClick for their method name. I wasn't sure if that was just a generic answer, but I tried it myself, and still got nothing.
I added breakpoints at the beginning of this method as well, and found that no matter how many times I click ANY cell, the handler is never even called.
Does anybody know what I may be doing wrong? All help is appreciated!

CDove actually answered this for me in his comment to the original question, but I just wanted to make it more visible for anyone else who may have the same problem.
After creating the handler method, you have to go to the designer and click the lightning bolt "Events" button under the "Properties" side bar. Then find the event you want to handle, and add the handling method that you created to it.
Once again, credit for this answer goes to CDove.

Related

Why is my DataPager causing multiple MouseButtonUp events to be throw?

I have a DataGrid and a DataPager bound to a DomainDataSource. That part works fine. The problem is, I have a function to handle double clicking on a DataGridRow
private void dataGrid_list_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.AddHandler(Control.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Row_MouseLeftButtonDown), true);
}
The actual logic of Row_MouseLeftButtonDown isn't important. It works fine, except for once I've clicked to another page using the DataPager. Now I'm showing that I've got two Mouse Clicks firing at the same time (which is triggering my double click when it shouldn't). If I click through four pages, I register FIVE clicks when I click on a row (one for the row click, and I guess one for each time I clicked to change pages with the DataPager)
Any idea why this is happening and how I can fix it? I tried putting a random button on the page that doesn't do anything, and clicking that doesn't cause this problem, so it doesn't seem to be just any click.
The event is being registered each time dataGrid_list_LoadingRow is called which must be happening whenever you click to another page.
private void dataGrid_list_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.RemoveHandler(Control.MouseLeftButtonDownEvent, Row_MouseLeftButtonDown);
e.Row.AddHandler(Control.MouseLeftButtonDownEvent, Row_MouseLeftButtonDown, true);
}
On the first time this is hit, RemoveHandler will not cause any problems as it will simply have no effect, according to the documentation. But on the second time this function is hit, it will remove the initially registered handler and simply add it again. This way, you won't just keep building up the list of registered handlers.
Also, new MouseButtonEventHandler(Row_MouseLeftButtonDown) is not necessary, you can just use Row_MouseLeftButtonDown (see Add/Remove handler to textbox).

TreeView EndUpdate() call isn't working as I thought it would

So I have a tree view that I extended so the user could select multiple items, but I've run into a little bug where it randomly stop painting itself.
After some digging around I actually found that it was my MouseDown and MouseUp events that were causing the problem:
protected override void OnMouseDown(MouseEventArgs e)
{
BeginUpdate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
EndUpdate();
base.OnMouseUp(e);
}
So if the user clicks in the TreeView, but releases outside of it, the MouseUp event doesn't get called, and the update won't end. I understand that much, but what's strange is after that, no matter how many times they click in the box it won't start drawing again, even though the EndUpdate() is now being called. This can be resolved by adding a button with EndUpdate() in it, but obviously I don't want that.
The reason I even need this is that I manually manage the coloring of the nodes as well as the selected node so all of the normal selection events are actually canceled. But without the Begin and End update I still get a flicker of a selection box before it reaches the cancel. With these I don't get the flicker but apparently it comes at a cost.
Here's a dropbox link to an empty project that demonstrates the issue.
If you break on the end update, you can see that it is getting called, but it just doesn't seem to change anything. But if you click the button that calls the same code, it resolves it.
Can anyone explain why this is happening? Am I missing something terribly obvious? Or is this just a bad way to go about it in general?
Regards.
Answer:
As no answer has been submitted, here's the solution brought to my attention by Hans Passant. Didn't want to post and mark as answer myself as I didn't come up with this.
Added this to my extended TreeView class and all issues seem to be resolved. (Albeit it's probably going to make some bad calls, but besides being wasteful it doesn't appear to hurt anything).
protected override void OnMouseCaptureChanged(EventArgs e)
{
EndUpdate();
base.OnMouseCaptureChanged(e);
}

How to restrict user from double clicking any command button

There is an issue that I am facing, is to restrict user from double clicking on a command button. What it does is execute twice the code written inside it's click event. I've read many solutions for it, which I find are irrelevant. The command button, they say, should be disable after a click is performed so that user won't be able to perform another click. This will create an issue when an error occur and the code that is written to enable the button didn't execute.
Is there any other way to do it? Please suggest if any one have better option than this.
You could disable it, and put all your code within a try, catch, finally clause, and put the enabling code in finally.
Give it a read.
That way it should always run, exception or not.
I assume that you are referring to WPF, as Windows Forms has a specific double click event which can be disregarded.
Unfortunately WPF does not make a distinction between double clicking a button and clicking the button twice. This means that you have to perform the check to see if the second click occurred too soon to the first click to be regarded.
This could be done by storing the DateTime.Now in a member variable in the button click event handler and if the click occurs within a too short amount of time of the previous click, simply indicate that the button click event was handled and return without doing anything.

Setting a "click" method to execute right away

I have a working method
something_Click(object sender, EventArgs e)
{
code
}
...
It of course executes after someone clicks the element. What I need to do is, to execute this method immediately after the element appears on the screen (it is StripStatusLabel). I have tried just to add a call of the method to beginning of the code, but it did nothing.
You can call Button.PerformClick on your button in a Form.Load event handler.
You might also want to consider moving that logic into its own method, and call that method from both the button handler and the Load event, as this will be more clear. (It's obviously code you want triggered on more than just a "button click").
Depending on what framework you are targeting (WPF vs Winforms) you might be able to handle the Load event instead. It triggers when the element appears on screen.
It's not best practice to fire a form event which would normally be originated from teh user. In the sense, you are trying to "fake" the click. Think of the future debugging, or of the colleague who might inherit your project. When inspecting code you would expect something_Click to only be fired when there is a click on "something".
A better solution is to put the "code" part in your snip into a method whose name reflects what it really does.
They you may fire this method in different areas. Fire it at the click, at the load, anywhere.
something_Click(object sender, EventArgs e)
{
DoStuff();
}
OnAppearToScreen()
{
DoStuff();
}
DoStuff()
{
//code that actually does stuff
}
Later on, when you want to check when "the stuff" was done to your object, you can easily tell by code inspection.

How to force control to fire leave event in winforms using c#?

I am pasting text in textbox1.text and I need textbox1 should fire its leave event by itself.
For now I am using this following code. but i will appreciate if anyone can suggest me an elegant or better way:-
private void event()
{
textbox1.Text = SearchedText;
textbox1.Focus();
textbox2.Focus();
}
First I am pasting text, then setting up Focus on the control, then set up focus again on second control. It is firing leave event of textbox1, but any thing better?
Just call the code directly, no need to wait for an event:
private void textBox1_Leave(object sender, EventArgs e) {
mumble();
}
private void someEvent() {
textBox1.Text = SearchedText;
mumble();
}
void mumble() {
// etc...
}
Just calling textBox1_Leave(this, EventArgs.Empty) works fine too.
You should handle the TextChanged or Validated events instead of the Leave event.
To FORCE Leave, Validating and so on Events, no matter what, I've found ONE working solution.
First i tried:
ProcessTabStop(true);
ProcessTabStop(false);
instead of:
textbox1.Focus();
textbox2.Focus();
Problem with the TextBox 1 and 2 Focus() is that its only Active Component that needs Leave, Validating and so on fired, not other Controls, and besides, what if the form is dynamic, you as a programmer not necessarily have any idea what Control you are trying to Leave, that's why i changed Control.Focus() method to ProcessTabStop method above. The problem is then, if only ONE Control has TabStop true, there is no control to go to and back from. So Events are NOT Fired.
Next problem is that i not necessarily Close the Form with the mouse so Focus doesn't change, I use a Key (Ctrl+Enter) to Accept the Form, and then Leave, validating and so on are NOT fired, when i Send Form Close, as Form Close registers weather there are changes or not. But Values are set in Leave on TextBoxes, so I had to find a solution that worked no matter what i did to it. I almost gave up, actually i had a problem report all filled out, when I thought, what if i set ActiveControl to Null and then to the Control it came from. It worked, but had som "Flicker" due to color change on Parent Panel depending on Active or Inactive.
The "Workaround" that works in all cases is:
Control Old = ActiveControl;
// ActiveControl.SuspendLayout();
// ActiveControl.FindForm().SuspendLayout();
ActiveControl = null;
ActiveControl = Old;
// ActiveControl.FindForm().ResumeLayout();
// ActiveControl.ResumeLayout();
That seems to fire Leave, Validating and so on Events, no matter number of Form Controls and TabStopped Controls. You MAY need to SuspendLayout on either ActiveControl, or Form. My Control (Parent Panel) changes color when Active/Inactive, if I do not Suspend Layout on Form, parent panel gets an unwanted "flicker" effect.
Looking at the solution, it is very obvious, now I've found it, but took me half a day to try different things that solved one or another problem, but not all.
I know this a VERY old thread, but one of very few articles I've found on the subject of Forcing Leave Event to be Fired.

Categories

Resources