Results 1 to 8 of 8

Thread: Form focus

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    6

    Form focus

    hi all,

    i have a Windows Form with a bunch of texboxes and combobox in it .
    When i'm running the app and filling out the form , I want to click on the form itself so no control has the focus . I thought by clicking on the form itself will make all controls lose the focus , but somehow that is not the case and the last control used still has the focus .

    is this a bug or am i missing something here , do i need to set a property in the form ?

    is there a way to make the form gain the focus so that no controls has the focus ?

    thanks,
    hang

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form focus

    If a form contains any controls that can receive focus then one of them always will have focus. What you ask for is not possible, at least using the .NET Framework anyway. Perhaps it's possible using the Windows API but not that I'm aware of.

    Why do you want this anyway. My guess is that you're trying to accomplish something that really doesn't need to be done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form focus

    Forget everything I just said. I just tried setting the form's ActiveControl property to Nothing and "hey presto"! Note that this must be done from inside the form itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    6

    Re: Form focus

    Quote Originally Posted by jmcilhinney
    Forget everything I just said. I just tried setting the form's ActiveControl property to Nothing and "hey presto"! Note that this must be done from inside the form itself.
    i will try it tomorrow but i think you answer my question ...
    do i need to put that one line code in the Form_load event or the Form_focus event ??

    here is an explanation as to my question, I have a bunch of combobox in a Windows form and my users like to use the mouse wheel to scroll and i'm afraid if the focus is on the combobox, they will accidentally change the answer in the combobox, so i want my users to click on the form and then scroll using the mousewheel.
    Because my users are used to doing that in web pages, where when you click
    on the webpage itself, all webpage's controls lose focus and they can scroll the web page using the mousewheel .

    thanks,
    hang

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form focus

    If you want something to happen when the form is clicked then you need to do it in the form's Click event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    6

    Re: Form focus

    Quote Originally Posted by jmcilhinney
    If you want something to happen when the form is clicked then you need to do it in the form's Click event handler.
    *bang my head on the wall*

    on Form_click
    form1.activecontrol = nothing


    thanks,
    hang

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form focus

    VB Code:
    1. Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    2.     Me.ActiveControl = Nothing
    3. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    6

    Re: Form focus

    Quote Originally Posted by jmcilhinney
    VB Code:
    1. Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    2.     Me.ActiveControl = Nothing
    3. End Sub

    Thanks for the answer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width