Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Since I dident get an answer about this on my previous question can anyone tell me how to set a buttons Focus to false in Visual studio using C#.
Thanks
Posted
Comments
dan!sh 12-Jan-11 11:09am    
Can you please put this as an update to your original question? Also, did you checked my reply?
WurmInfinity 12-Jan-11 11:13am    
done and done :)
Nuri Ismail 12-Jan-11 12:39pm    
It appears that is easier to restore back the focused texbox, the caret position in it and the selected text length in it than preventing buttons to get focus (at least in WinForms). Please see my updated answer to your original question.

Set the TabStop property to false. Or do you mean to make a button completely unavailable but visible? Set Enabled to false. Both of these can be set in code.
 
Share this answer
 
Comments
WurmInfinity 12-Jan-11 11:27am    
No I want it so that the focus dosent go away from the textbox. TapStop still pulls focus when the button is clicked. Any other ideas?
R. Giskard Reventlov 12-Jan-11 12:37pm    
Why not set focus back to the textbox in the onclick event (either javascript or code behind as appropriate)?
Sergey Alexandrovich Kryukov 12-Jan-11 15:04pm    
This is not what Inquirer asked about (at least formally), the problem was a misconception -- see my answer.
R. Giskard Reventlov 13-Jan-11 3:22am    
You're trying to be too clever and, thereby, entirely missing the point. He doesn't care about the semantics or the whys or wherefores or that it is an 'unspecified action', he just wants to get it done.
I think it's going to be final answer:

The question is not correct, because it makes no sense.
Being in a focus is not a control's exclusive property, this is essentially a property if a set of controls.

First of all, essentially, the focus is the target of dispatching of the keyboard event; in what control the events go. Additionally, there should be a visual clue for that state (implementation of this visual clue in a control can easily be failed, in case of sloppy programming).

Generic de-focusing of the control is not possible or impossible, it is unspecified action. It is not possible to "remove" focus, it is only possible to move focus somewhere else. Also, a request to remove a focus from a particular control make no practical sense.

When you want your eyes to remove focus from an object you look at, you normally realize it like focusing on something else or else you can close your eyes.

Now, here is more on background: there are also windows or forms. They are not focused, they are activated or deactivated. Among windows, "activity" status plays the role of the focus within a window. When a windows is activated, one of its controls gets focus. When it is deactivated, nothing on it is in focus, but on next activation, the same very control will get in focus. Within the window, the focus can be moved from control to control. So we can talk about "logical" and "physical" focus of a control (on WPF, whole hierarchy of logical focus is supported, on WinForms we can talk only about one control). "Logical" focus does not depend on the activation status of the form, but "physical" does -- a control is in focus (can physically accept keyboard events) only when it is in a "logical" focus and the window is activated.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jan-11 0:36am    
Ha-ha! Voters! A sweet pair. Your teacher didn't say "good job sweetheart" as before?
R. Giskard Reventlov 13-Jan-11 3:18am    
Complete twaddle.
Sergey Alexandrovich Kryukov 14-Jan-11 15:32pm    
Somebody wanted to proof it's not a pair. This is so funny.
Tarun.K.S 16-Jan-11 11:48am    
You are so wrong!
Sergey Alexandrovich Kryukov 16-Jan-11 15:18pm    
Perhaps you do not understand. Please explain it.
C#
class ButtonNoFocus : Button
{
    protected override void WndProc(ref Message m)
    {
        const int WM_MOUSEACTIVATE = 0x0021;
        const int MA_NOACTIVATEANDEAT = 4;

        if (m.Msg == WM_MOUSEACTIVATE)
        {
            m.Result = new IntPtr(MA_NOACTIVATEANDEAT);
            DoMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0));
        }
        else
            base.WndProc(ref m);
    }

    public new event MouseEventHandler MouseDown;

    private void DoMouseDown(MouseEventArgs mouseEventArgs)
    {
        var e = MouseDown;
        if (e != null)
            MouseDown(this, mouseEventArgs);
    }
}


This will prevent focus and raise a fake MouseDown event when clicked. Note: Click event will not fire as the control must take focus for the Click to happen; however, if want to capture the mouse and manually track this should work.

UPD: It is better to return MA_NOACTIVATE (3) as it will not bring focus to the control, but will still raise the usual MouseDown event.
 
Share this answer
 
v2
This is not going to be easy. At least I am not aware of an easy way. I think you will have to extend th eexisting button control for that.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-11 15:05pm    
There is no way, but not because it's just impossible, because the notion itself makes no sense, the problem was a misconception -- see my answer.
You can do this :

Button1.Focusable=False
 
Share this answer
 
Comments
Ashish Tyagi 40 12-Jan-11 12:48pm    
Focusable is a read only property friend...
Tarun.K.S 12-Jan-11 13:07pm    
Public Property Focusable() As Boolean
Member of System.Windows.UIElement
Summary:
Gets or sets a value that indicates whether the element can receive focus. This is a dependency property.

Return Values:
true if the element is focusable; otherwise false. The default is false, but see Remarks.

Well this is what Visual Studio says!
Sergey Alexandrovich Kryukov 12-Jan-11 15:03pm    
This is not what Inquirer asked about (at least formally), the problem was a misconception -- see my answer.
Tarun.K.S 13-Jan-11 0:21am    
Well the Inquirer asked to set a button's focus to false and that's what i did.
From your answer, i don't agree with this: "It is not possible to "remove" focus, it is only possible to move focus somewhere else". It is possible, i have tried it, you won't be able to click and neither have keyboard focus once you set the focusable to false.
Nish Nishant 14-Jan-11 11:16am    
Wow, marked as answer and yet voted 1. Bizarre!
Try ActiveControl property of form

C#
this.activecontrol = textbox1;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900