removing focus from buttons

Shaifali jain 420 Reputation points
2023-09-29T08:21:45.1033333+00:00

Hello ,

User's image

this is the what the main exit form looks when it loads . i found that when it loads the yes button has focus on it by default . Although i had hard coded that when the user presses enter here , the application should gets closed but what i am trying is to find a way by which neither of buttons has this focus on them . the project is at https://github.com/Shaifalijain31/copyprojectshaifalijain

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-09-29T08:53:26.0566667+00:00

    Its by design of Windows to have a default focus so to achieve what you want is to add another button and set its left property off screen than set its tab index to be the first button, yes button next and the no button next.

    Screenshot, button1, left property -120

    form

    Note this also is a per-say hack as what you ask for must be a hack.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-09-29T08:38:37.82+00:00

    which neither of buttons has this focus on them

    One control has always the focus, no-focus is not possible.

    How to set the tab order on Windows Forms (Windows Forms .NET)

    Add an imgae control with a size of 1px * 1px with same color as Windows background and set the focus on that for user not really visible control.


  2. Castorix31 90,681 Reputation points
    2023-09-29T10:11:24.3+00:00

    Remove TabStop :

            private void MainExitForm_Load(object sender, EventArgs e)
            {
                button1.TabStop = false;
                button2.TabStop = false;
            }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.