views:

139

answers:

3

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

A: 

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.

ChrisF
thanks for the response.. question abut what you've posted above.. i don't see where you are adding the event handlers for the mouseleftbuttongup/down. from what it looks like above the code is there to handle an event.. but not fire one. perhaps can continue to help me out... thanks
jvcoach23
@jvcoach23 - I used the short form - missing out the `new MouseButtonEventHandler` for the sake of keeping the code narrow enough to fit without scrolling. I must admit I didn't test the code when I posted it - but I have now. It does work, but only when you click on the border as per @AnthonyWJones's answer.
ChrisF
ChrisF.. more questions... first.. if the textbox is created and put as a child to a border, when you click in the textbox.. does it pick up as you have clicked in the border as well. The code i have looks something like this TextBox1 = New MyTextBox() TextBox1.AcceptsReturn = True TextBox1.TextWrapping = TextWrapping.Wrap TextBox1.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden Me.brTyping.Child = TextBox1course i still haven't gotten the convert c# code to work that you gave to ask any good questions.. but 1 thing at a time i guess
jvcoach23
A: 

Hi jvcoach , 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
Malcolm
sorry for the need to be spoon fed... i've converted what you provided to vb.net. I get an error in the InvokeEvent saying Public Event FireEvent(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. So i didn't do something right on the Public Event FireEvent as EventHandler. Thanks
jvcoach23
HEY jvcoach23, just check the updated answer..
Malcolm
A: 

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.

AnthonyWJones
@AnthonyWJones you are right
Malcolm