Click to See Complete Forum and Search --> : [RESOLVED] Having a hard time unserstanding this.


admdev
May 7th, 2009, 11:03 AM
Hi all,

I have spent some time reading about events and delegates and being new to this concept, I find it difficult to understand. I would like to call the keypress event of a textbox from another object, say a button, using this concept. I would really appreciate if an expert walks me through the process of achieving this.

I know that I need to create a delegate and an event as follows.

public delegate void KeyHandler(object source, KeyEventArgs e);
public event KeyHandler KeyPress;

That you subscribe to it using the += and unsubscribe using the -+, but how to connect them together and link them to the textbox keypress event.

Your help would be greatly appreciated.

Thanks.

KennethRJones
May 7th, 2009, 02:07 PM
First, I need to know what you are trying to do here. There really isn't going to be a way to directly map a button clicked event to a key pressed event because the signatures of the two events are different. Since both events exist within the same class it is possible to simply call the KeyPress event manually, however, the KeyPress event is normally used to handle key presses and because no key was acutually pressed, any operation done within the KeyPress event would most likely be useless. Are trying to register a default key press depending on what button was clicked?

The two events are pretty much incompatible as far as anything logical is concerned. Can you please post your program code so that I know what you are trying to do? I can help you modify it to use a delegate but unless you are registering a default key press its pretty much pointless. In order for a delegate to be usefull the signatures of all functions involved must match.

Delegates are used (normally) to allow one object watch for events that fire in another object by subscribing to those events, and then doing something usefull after that event has been fired. These events are normally not contained within the same class hince the need for delegates in the first place.

admdev
May 7th, 2009, 03:54 PM
KennethRJones,

Thanks for your reply.


private void rtxtFind_KeyPress(object sender, KeyPressEventArgs e)
{

//Do not allow characters other than numbers, period, and backspace.
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back)
{
e.Handled = true;
}
if (e.KeyChar == 13)
{
//Retrieve the record and populate the form with detail
mgetrecords();
}

}


When the user does not remember the record number, I have them click a find button. Clicking the find button opens another form with a listview and a list of records. When they click the record on the listview, I want to call the keypress event of the rtxtFind and refresh the parent form.

Thanks.

nelo
May 8th, 2009, 02:47 AM
When the user does not remember the record number, I have them click a find button. Clicking the find button opens another form with a listview and a list of records. When they click the record on the listview, I want to call the keypress event of the rtxtFind and refresh the parent form.
Thanks for making it clearer. Here are my thoughts...
1. In the 'other' form with the listview declare a public event. It has to be public so that calling form can subscribe to it.

public class OtherForm
{
...
public EventHandler RecordSelectedEvent
...
}

2. In the listview form raise the RecordSelectedEvent. You should do this inside the handler for the click on the list view (I assume you already have this).

if (RecordSelectedEvent != null)
{
RecordSelectedEvent(this, new EventArgs());
}

Note that you have to check for null just in case nobody has subscribed to the event (in which case you don't need to do anything).
3. In the main form subscribe to the event. You can do this anytime after you've created an instance of the listview form. You must of course supply the method that will handle the event (i.e. RecordSelectedEventHandler in this case).

OtherForm listViewForm = new OtherForm();
listViewForm.RecordSelectedEvent += RecordSelectedEventHandler;

private void RecordSelectedEventHandler(object sender, EventArgs args)
{
// Do what you need to do here to refresh the form or whatever.
// You do not need to simulate the key press. You are not interested in the key press. You are actually interested in what happens because of the key press. e.g. mgetrecords();

}


As I stated in the comments with this framework you don't need to simulate or artificially generate the key press. You want to call your mgetrecords() method or something like that.

Now this example assumes that you already have a way of getting what the user selected in the list view form.

admdev
May 12th, 2009, 09:21 AM
nelo,

Thank you so much for your help. This is exactly what I was looking for. So this is event handling?

I was having some trouble understanding the concept of event handling, but you made it perfectly clear.

It's working.

Thanks.