Short solution for short problems

On leave and enter hit events in UITextField (iOS)

If you want to catch the event when a user leaves a text box and has changed something, or when he presses enter in the text box itself, you will need to do 2 things:

- For the leave it's very easy, Apple already prepared this event for you. Use the Editing Did End Action and connect it to your controller's implementation file:


But now you also want to capture the event that occurs when the user presses enter in the textfield.

For this we start by adding the UITextFieldDelegate protocol in the controller.h:

@interface PhotoTakerViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate, UITextFieldDelegate >

next we implement it like this:

- (BOOL) textFieldShouldReturn:(UITextField *)textField


    ...Do something

    return YES;




Create a custom event handler for an ASP.net user control with custom event arguments.

Just a quick tutorial on how to pass events from a user controls to the page.

Imagine you have a user control that has some drop down lists filled. When the dropdown list selection changes, you want to pass this event to the page that is implementing the user control.

First lets create our own general event handler using our own event arguments (This class can be in a general class, so you can reuse it):

/// <summary>

/// Summary description for Project Drop Down

/// </summary>

public class PDDCommandEventArgs


    private string _id;

    public string ID


        get { return _id; }



    private string _description;

    public string Description


        get { return _description; }


    public PDDCommandEventArgs(string guid, string description)


        _id = guid;

        _description = description;




public delegate void PDDCommandEventHandler(object sender, PDDCommandEventArgs e);


Next , we declare the event handler on our user control:

public event PDDCommandEventHandler ValueChanged;


Then we fire the event when the dropdown is selected on the user control:

private void OnValueChanged(object sender, PDDCommandEventArgs e)


    if (ValueChanged != null) ValueChanged(this, e);



protected void drpSteps_SelectedIndexChanged(object sender, EventArgs e)


    OnValueChanged(this, new PDDCommandEventArgs(this.drpSteps.SelectedValue, this.drpSteps.SelectedItem.Text));



Once a page that uses this control has declared the event handler, it will be fired when the selection changes:

projectDropDown1.StepChanged += new ZinqProjectsWeb.Controls.PDDCommandEventHandler(projectDropDown1_StepChanged);

void projectDropDown1_StepChanged(object sender, Controls.PDDCommandEventArgs e)





Fire and event from a sub thread to the main thread in WPF

This small example shows you how to simply fire an event from a sub thread, capture it In the main thread and handle it in WPF.

The demo application (Attached to this post) simply starts a sub thread. In this thread we generate some random numbers and sent them back to the main thread. The main thread will show these numbers in a list.

First let’s investigate the main thread.

I’m using 2 private objects in my main thread:

private Dispatcher _uiDispatcher;

Thread _workerThread;


The _uiDispatcher will be used to store the main thread dispatcher in. The workerThread is the thread that will be used to handle the subroutine.

When I initiate the mainform, I will store the dispatcher of the main thread in the _uiDispatcher.

// Call from the main thread  

public void UseThisThreadForEvents()


_uiDispatcher = Dispatcher.CurrentDispatcher;



public MainWindow()






So when the program starts, we have the dispatcher of the main thread that will be responsible to update the UI stored in a variable.

On the main form there is a button, when clicked it will start the sub thread.

private void button1_Click(object sender, RoutedEventArgs e)


    ThreadStart start;

    Worker worker = new Worker();

    worker.OnValueSent += new Worker.SentValueHandler(worker_OnValueSent);

    start = delegate()





    // Create the thread and kick it started!

    _workerThread = new Thread(start);

    _workerThread.Priority = ThreadPriority.Lowest;





In my worker class I declared an event that will be fired every 2 seconds. This event will be captured in the main thread and handled by the worker_OnValueSent method.

So here is where we will have to check in which thread we are and if needed invoke the dispatcher of the main thread so we can update the UI without invoking every control separately.

public delegate void OnValueSentDelegate(List<int> list);


void worker_OnValueSent(List<int> list)


    if (Dispatcher.CurrentDispatcher != _uiDispatcher)


        _uiDispatcher.BeginInvoke(new OnValueSentDelegate(worker_OnValueSent), list);











Simple logic written here. When the event is fired, we check if the dispatcher equals the _uiDispatcher (Dispatcher of the main thread). If not, we invoke the main thread dispatcher and use a delegate method to call the OnValueSent method.

So that’s how we do it.

For the people who need it, here the simple code of the worker class:

public class Worker


    public delegate void SentValueHandler(List<int> list);


    public event SentValueHandler OnValueSent;


    public void SentValuesEvery2Seconds()


        int test = 0;

        while (test < 5)



            if (OnValueSent != null)


                Random rd = new Random();


                OnValueSent(new List<int>() { rd.Next(0, 1000), rd.Next(0, 1000), rd.Next(0, 1000) });







And here is a screenshot to give you an idea of what it does and how it looks.



WpfApplication1.zip (50.11 kb)