c# - Trackbar keeps stealing my focus -


it has been asked few times couldn't use of answers. problem everytime want change trackbars value keeps focused when i'm clicking on other parts of window. , when want use keys work in trackbarbox.

what did try?:

-i tried set causesvalidation/tabstop/topmost false/true

-i tried use mouseleave/focusenter events set focus on form this.focus()

-i tried put

protected override bool isinputkey(keys keydata) {    return true; } 

and/or

protected override bool showwithoutactivation {    { return true; } } 

into maincode

here screenshot of programm understand problem: it's german doesn't matter. want press enter while i'm drawing line trackbar keeps focused , blocks it

the usual way to override onkeydown event after setting keypreview = true:

    protected override void onkeydown(keyeventargs e)     {         base.onkeydown(e);         // code here..         text = "testing: keycode" + e.keycode;     } 

but can use previewkeydown event. make sure set form's keypreview property true , add common event controls might steal/receive focus!

since previewkeydown event of controls usees different argument need route event form's keydown event:

    private void commonpreviewkeydown(object sender, previewkeydowneventargs e)     {         form1_keydown(this, new keyeventargs(e.keycode));     }        private void form1_keydown(object sender, keyeventargs e)     {         // code here..         text = "testing: keycode" + e.keycode;     } 

you may want hook handles in code:

    void routekeys(control container)     {        foreach (control ctl in container.controls)             if (ctl.canfocus) ctl.previewkeydown += commonpreviewkeydown;      } 

call this:

    public form1()     {         initializecomponent();         routekeys(this);     } 

of course may want add filters prevent routing of keys form not going process..

a difference between 2 techniques when override form.onkeydown receive keyevents anywhere; include e.g. textboxes both character , editing keys routed form.

if don't want need add filter event:

if (tb_notes.focused) return; if (tb_morenotes.focused) return; if (rtb_edit.focused) return; 

the second way let's decide controls shall included or excluded in routing..:

if (ctl.canfocus && !(ctl textbox || ctl richtextbox))                 ctl.previewkeydown += commonpreviewkeydown; 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -