tags:

views:

34

answers:

3

Hi, I'm making simple C# Winform application. There is a Form having a textbox. I want to change the location of textbox by arrow key but textbox has the input focus so form's KeyDown event is not called. How can I remove that input focus?

Typing on the textbox should still be possible. I try to make a dummy label and give the focus, but It doesn't work. If I press any key, the cursor go back to the textbox. please help me. How can I solve this problem?

+1  A: 

Handle the TextBox.KeyDown event. And set e.Handled = true; in your handler after you move the TextBox, but before you return. And, yeah, only handle the arrow keys.

jeffamaphone
+1 and filter for arrow keys? Good idea.
LeonixSolutions
Thank you for answer. It is good idea. but I also need to make editing possible. This way will make 'moving textbox' ok. but 'moving cursor' is not. I'm sorry for my poor explanation.
Shongky
Oh, From other answers, Maybe I can put code for select that handler or not.For example If(IsMoveMode) { if(e.keyCode == Keys.Left) bula bula~~~
Shongky
A: 

Hmm, not sure if I understand. If the user can type into the edit box, then it can have focus. If he clicks outside of it, on a blank are of the form, then it loses focus.

If you want to be able to 1) type into the edit box and 2) move the edit box, then you need a separate mechanism to enter "move mode".

I would suggest either a "click here to move selected control" button, or a right-click context menu on the control with a "move control option".

You would also have to conisder how the user indicates that moving has ended.

Hope this helps.

LeonixSolutions
Thank you for idea! Separate to two mode will be work fine. This is good for me. thank you.
Shongky
A: 

NOTE: I just realized this is not even an in-browser C# app. I guess disregard all of this. Serves me right for not reading carefully enough.


Use Javascript, in particular, I'd personally recommend jQuery.

They have pretty nicely documented their library: http://docs.jquery.com/Main_Page

For this particular task, you are going to want to bind some sort of key event (ie. keypress) and make sure to stop event propagation (so that you prevent the default response which is to be sent to be simply handled by the textbox element's default listener).

So, to give you an idea, if you want to change the location of the textbox using keypresses (maybe arrow keys), do something like this:

/* link the jQuery source to the HTML page with script tag */
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">

/* short hand for $(document).ready(function() { ... */
$(function() {

    $("#textbox_id").keypress(function(e) {
        var $this = $(this); // store the #textbox_id element in $this
        e.preventDefault();
        switch (e.keyCode) {
            // find the actual integer code for the up arrow
            case UP_ARROW: 
                $this.animate({
                    top : '-=10px'
                }, 100); // time in milliseconds to complete the animation
            /* fill in the cases */
        }
   }

});

Okay, I hope you get the picture. Find out more about animate() and other jQuery functions at in the documentation at the link I provided above. Hope that helps!

NOTE: Obviously, preventing the default handling of events is a terrible idea in this case for accessibility reasons. Use your best judgement when selecting keypresses to trigger these moving events -- whatever you do, don't disallow users from moving around within the text they have in input fields.

tjko
Thank you for answer. hmm. I also need the Default handlng :) Maybe I can put the switch to select what handler default or myversion to move box. thank you.
Shongky
Yeah, sorry I misinterpreted your question. Hopefully you took something out of this response... Good luck!
tjko