开发者

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


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜