Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a program that will enter information based off an mp3 file. The problems is my default media player has focus and I have to click the form in order to enter the information. Is there a way to have my Windows application forms get and maintain focus?

An example of my code:

VB
Public Class introScreen

    Dim ResourceFilePathPrefix As String

    Private Sub introScreen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        If System.Diagnostics.Debugger.IsAttached() Then

            'Debugging

            ResourceFilePathPrefix = System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\resources\")

        Else

            'Published

            ResourceFilePathPrefix = Application.StartupPath & "\resources\"

        End If

        'Presume you have added calc.exe file to Resources

        Process.Start(ResourceFilePathPrefix & "msat.mp3")

    End Sub


    Private timer1 As New Timer

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        timer1.Interval = 5000
        timer1.Enabled = True
        AddHandler timer1.Tick, AddressOf Timer1_Tick
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Open login form and close initial screen
        Dim f2 As New loginForm
        f2.Show()
        timer1.Enabled = False
        Me.Hide()

    End Sub

End Class
Posted

1 solution

There is no such concept as focus of the application. Having the focus — a keyboard focus — is an attribute of Control. Only one control at a time can get focus. Use the method System.Windows.Forms.Control.Focus, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx[^].

A windows of application can be placed on top of Z-order and activated. For a form, you can use System.Windows.Forms.Form.BringToFront and System.Windows.Forms.Form.Activate, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bringtofront.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx[^].

—SA
 
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