DataGridView and the CellEndEdit Event - c#

I have a DataGridView, and would like to hook into the CellEndEdit event. I've been able to successfully hook into the CellContentClick event, but am having issues with CellEndEdit.
I added the following code to my Form1.cs file:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1[0, 0].Value = "Changed";
}
With that code, nothing happens when I am done editing a cell. Is there anything else that I need to do to successfully hook into this event? I see that CellContentClick has a
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
line of code in the Form1.Designer.cs file, but I tried to mimic this for CellEndEdit, and received a compile error
(No overload for 'dataGridView1_CellEndEdit' matches delegate
'System.Windows.Forms.DataGridViewCellEventHandler')

You could implement this yourself.
In your constructor you could have a HookEvents() method which wires up such events.
Or, within the form designer, click the gridview to select it, go to the properties window and click the yellow thunderbolt to find a list of events. Then, scroll down and find the CellEndEdit event and double click it - this will wire up the event for you.
To wire it up yourself, it may look like:
class A : Form
{
public A()
{
Initialize();
HookEvents();
}
private void HookEvents()
{
dataGridView1.CellEndEdit += dataGridView1_CellEndEdit;
}
}

I doubt very much that your solution would work.
It's not a matter of where you place the subscription, is how you do it.
Brandon, you are declaring an EventHandler, that is the function responsible of doing what you want to do in case of that event "dataGridView1_CellEndEdit" but you are not subscribing to the event. Also in your function you are passing the wrong parameters.
The easy solution is either subscribe from the designer window or by code doing this:
write "dataGridView1.CellEndEdit +=" and then press the TAB buton twice. That shoud create the code for subscription to the event and the correct delegate to handle it.

Related

Button Click event is getting called for holding event

I have used command binding for the click event of the button. Now i also have a holding event on the button. So whenever i do holding on the button, click is also getting called along with holding event handler. I have tried setting
e.handled = true;
but that doesn't work. Any suggestions on why both the events are getting detected. If i use Tapped event instead of command binding, everything work fine. But my requirement is to use command binding for click event.
Edit: Below are some code behind
Code:
xaml :
Button Command={Binding ButtonClicked} Holding="Button_Holding"
xaml.cs
private void Button_Holding(object sender, HoldingRoutedEventArgs e)
{
}
ViewModel has the ButtonClicked Command
I am not an Expert in this stuff but i guess that Holding is a bubbling event and ButtonClick is a direct event(If somebody knows that for sure i would love to read a comment)
So your e.handled = true; doesn't apply to the ButtonClicked event.
I would say that you have to let your button clicked command know that the hold event was fired first and then ignore that event via the CanExecute method, or whatever you named it, or at least do something like
public void YourButtonClickedMethod()
{
if(SomeObject.IsHoldAllreadyExecuted)
{
SomeObject.IsHoldAllreadyExecuted = false; //set it to false for the next run
}
else
{
//DO the stuff you want to
}
}

Registering MouseDown and MouseMove on Form

How and where can I register a mouse event on a form. When I doubleclick on the form it'll generate the Form_Load event for me and I can add code into there. However when I add something like
private void Form1_MouseDown(object sender, MouseEventArgs e{
Console.WriteLine("mouse down")
}
However when I do a mousedown event on the form I don't get anything on the console. I know something is missing where I register the event to the form or something of the sort. Any ideas?
Thanks,
In the designer view, select the form and then in the properties window, click the little lightning bolt (events).
Here you're able to select which delegate method is called for which event. If you haven't created the method already, just double click the empty space next to an event and it will generate the code for you.
If you are using VS.net then you should find all the events in the property panel. Just pick the ones you want.
If you want to grammatically register an event then the code would looks like:
Form1.Click += new MouseEventHandler(Form1_MouseDown);
in order to unregister it's
Form1.Click -= new MouseEventHandler(Form1_MouseDown);
The event needs to be "wired up" either from the designer or from code. You can wire up an event from Visual Studio by double-clicking the event in the properties window:
Which generates code like the following in the auto-generated .designer file:
theForm.MouseDown += new MouseEventHandler(Form1_MouseDown);
You can also use the code like the above to manually wire up events in your Form_Load method.
With WinForms, you'll want to add it via the design view in Visual Studio.
While in the design view, select your form. Then, click on the 'Events' button in the Properties panel (looks like a lightning bolt) and type in the function name under the appropriate event. You can also click on an event here to automatically generate a new function in the code-behind.
As you noticed, double-clicking the form will automatically generate a certain function in the code-behind. For forms, it is Load but for other things it may be MouseDown or some other event.
By your explanation it seems like you have registered the event properly through the designer... if you still don't see the string on the console try System.Diagnostics.Debug.WriteLine (Maybe you are looking on the wrong window)

C# TreeView SetFocus firing leave event twice

I have this simple code, where when the user leaves the TextBox control, TreeView gets focused:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add("A");
this.treeView1.Nodes[0].Nodes.Add("A.A");
this.treeView1.Nodes.Add("B");
this.treeView1.Nodes[0].Nodes.Add("B.A");
}
private void textBox1_Leave(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Leave..");
this.treeView1.Focus();
}
}
If we execute this code the Leave event is fired twice:
Leave..
Leave..
But if we set focus to other control, only one Leave event is fired.
Is that a problem of the TreeView? Do you know any workaround? Should we report this to Microsoft?
Thanks,
RG
this.treeView1.Focus();
Do not use the Focus() method in an event handler that's called because of a focusing event, like Leave. If you need to prevent a focus change then use the Validating event instead. Setting e.Cancel = true stops it.
But do note that this isn't very logical to do so for a TreeView, there isn't anything the user can do to alter the state of the control. You'll trap the user. Maybe that was the intention, do make sure the user can still close the window. If not then you might need the FormClosing event to force e.Cancel back to false.
Given that there is no code there to wire up the event I'm guessing you did it from the designer which means a line of code such as
textBox1.Leave += new EventHandler(textBox1_Leave);
will have been added to the Form1.designer.cs, check this file to ensure the line doesn't exist more than once as for each time this line is run you will get an event trigger, so if you run the line 3 times the Leave event will fire 3 times when you leave the textbox!
HTH
OneShot

Adding Events To WinForms?

I have a TextBox on a WinForm and I want to execute some code every time someone presses a key inside of that TextBox. I'm looking at the events properties menu, and see the KeyDown event, but don't know how to add code to it.
You need to add an event handler for that event. So in the properties menu, double-click on the field beside the KeyDown event and Visual Studio will create an event handler for you. It'll look something like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// enter your code here
}
You can also subscribe to events yourself without using the Properties window. For example, in the form's constructor:
textBox1.KeyDown += HandleTextBoxKeyDownEvent;
And then implement the event handler:
private void HandleTextBoxKeyDownEvent(object sender, KeyEventArgs e)
{
// enter your code here
}
These answers will have visual studio generate the event and bind it behind the scenes in the Designer.cs file.
If you want to know how to bind events yourself, it looks like this.
MyTextBox.KeyDown += new KeyEventHandler(MyKeyDownFunction)
private function MyKeyDownFunction(object sender, KeyEventArgs e) {
// your code
}
If done this way, the new KeyEventHandler() part is optional. You can also use lambdas to avoid boilerplate code.
MyTextBox.KeyDown += (s, e) => {
// s is the sender object, e is the args
}
Doubleclick the textfield next to it.
I assume you are in Visual Studio. One way would be to double click on the empty textbox on the right of the KeyDown event: VS will generate the code for you.
You need to add a handler to the event.
Double-click the KeyPress event in the textbox's Properties window to make Visual Studio generate an event handler in the code file.
You can then put any code you want to inside the event handler function. You can check which key was pressed by writing e.KeyCode.

Routing Key events to other control

Is there any standard way to route all Key events from the control A to other control B? I wish that the keyboard focus will still be on A however the event handler of A would trigger the all event handlers of B for the key events.
edit: Clarification: calling a specific event handler I wrote for B is not enough. I need to mimic the actual event. So for example I want that if a key is sent to a TextBox, it would be written to the TextBox. The solution given below does not do that (not to mention the fact that if new event handlers are added to B it completely fails).
I'm aware that WPF differentiates between logical focus and keyboard focus, but I need both focuses to remain on control A, but in a certain cases route its incoming event to other controls.
Couldn't you do something like this?
private void button1_Click(object sender, RoutedEventArgs e)
{
// Check if the event needs to be passed to button2's handler
if (conditionIsMet)
{
// Send the event to button2
button2.RaiseEvent(e);
}
else
{
// button1's "Click" code
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
// button2's "Click" code
}
Edit: Modified code to use the RaiseEvent() method to programmatically raise a specific event, rather than just calling the event handler for button2.

Categories