jvcoach23
jvcoach23

Reputation: 3037

How to add a click event to a textbox created in code

I'm using silverlight 3 and i'd like to create a handler and event wired up to a mouse click in a text box that was created in code behind. Can someone point me in the right direction.

I need to make it so that some things fire off when that textbox is clicked into.

if you have an example in vb.net that would be even better. thanks shannon

Upvotes: 2

Views: 6072

Answers (3)

Malcolm
Malcolm

Reputation: 1807

I will just add something to ChrisF answer, and let me know if thats what you want..

 TextBox textBox1;

    public SilverlightControl1()
    {
        InitializeComponent();

        textBox1 = new TextBox();
        textBox1.MouseLeftButtonDown += new MouseButtonEventHandler(textBox1_MouseLeftButtonDown);
    }

    void textBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        InvokeEvent(sender, null);
    }


        public event EventHandler FireEvent;

        public void InvokeEvent(object sender, EventArgs e)
        {
            EventHandler handler = FireEvent;
            if (handler != null) handler(sender, e);
        }

/////Here its in vb.net code snippet please try the below code :

Public Partial Class SilverlightControl1
    Inherits UserControl
    Private textBox1 As TextBox

    Public Sub New()
        InitializeComponent()

        textBox1 = New TextBox()
        AddHandler textBox1.MouseLeftButtonDown, AddressOf textBox1_MouseLeftButtonDown
    End Sub

    Private Sub textBox1_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        InvokeEvent(sender, Nothing)
    End Sub


    Public Event FireEvent As EventHandler

    Public Sub InvokeEvent(ByVal sender As Object, ByVal e As EventArgs)
        Dim handler As EventHandler = FireEvent
        RaiseEvent handler(sender, e)
    End Sub
End Class

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189457

The TextBox whilst having mouse events by virtue of inheriting from UIElement only fires the MouseDown event when its border is clicked. You do not get mouse events when clicking into the text editing area of the TextBox.

The closest you can get to this is the GotFocus event.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137128

The following code will simulate a mouse click in a text box created in the code behind.

    TextBox textBox1;
    bool mouseDown;

    public SilverlightControl1()
    {
        InitializeComponent();

        textBox1 = new TextBox();
        textBox1.MouseLeftButtonDown += textBox1_MouseLeftButtonDown;
        textBox1.MouseLeftButtonUp += textBox1_MouseLeftButtonUp;
        mouseDown = false;
    }

    void textBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (mouseDown)
        {
            // Do the mouse click here
        }
        mouseDown = false;
    }

    void textBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        mouseDown = true;
    }

You'll probably want to add an addition check that the time between the mouse down and mouse up is less than 500 milliseconds (say) and that the mouse hasn't moved more than a pixel or two between the events.

Upvotes: 1

Related Questions