Windows Develop Bookmark and Share   
 index > Windows Forms General > Bring form to the front
 

Bring form to the front

Hi,

I have a FormA and SplashscreenB
When i click a button on FormA, SplashscreenB opens up.
Now, if the user clicks back anywhere on the FormA, it should be on top and SplashscreenB should go into the background.

In the clickevent of FormA, i tried

FormA.bringtofront()
FormA.topmost = true --------
FormA.Activate()

SplashscreenB.sendtoback()
SplashscreenB.topmost = false

None of these worked.
Any other way?

Thanks in Advane

rowter  Monday, September 28, 2009 5:11 PM
http://social.msdn.microsoft.com/Forums/en-AU/Vsexpressvb/thread/e52a741d-a42c-44b1-b604-14b74f166435


Check that thread for some ideas on a splash screen or a log in screen.
I cannot see a need to hide the screen/form. Just instantiate a new one when you need to show it, then close it and dispose it.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, September 28, 2009 8:12 PM

Hi rowter,

Based on my understanding, you want to handle the mouse click event of FormA when SplashscreenB is shown. As general, the mouse click event only fired when the mouse is on the form. In your case, the mouse is out of SplashscreenB, the event would not fired. But we can call the SetCapture API to extend the mouse event region to the whole screen, then we are able to capture the event even the mouse is out of the form region. This is the code snippet:
FormA:

    Private SplashscreenB As SplashscreenForm
    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SplashscreenB = New SplashscreenForm()
        'Add this handler to handle the mouse clicking on FormA
        AddHandler SplashscreenB.MouseDown, AddressOf SplashscreenForm_MouseDown
    End Sub

    Private Sub SplashscreenForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Dim FormA As Form = Me
        If (FormA.RectangleToScreen(FormA.ClientRectangle).Contains(Control.MousePosition) _
           And Not SplashscreenB.RectangleToScreen(SplashscreenB.ClientRectangle).Contains(Control.MousePosition)) Then
            'If the mouse click on FormA, but not on SplashscreenB, run the code below.
            FormA.BringToFront()
            FormA.TopMost = True
            FormA.Activate()

            SplashscreenB.SendToBack()
            SplashscreenB.TopMost = False
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Show the splash screen.
        SplashscreenB.Show()
    End Sub



SplashscreenB:

Public Class SplashscreenForm
    Private Sub SplashscreenB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Extent the mouse click region to the whole screen.
        'In other words, you can handle the click event even you click a position out of the form.
        SetCapture(CType(Me.Handle, Integer))
    End Sub

    Private Sub SplashscreenB_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Reset the click region to the form rectangle.
        ReleaseCapture()
    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SetCapture(ByVal hwnd As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function ReleaseCapture() As Boolean
    End Function
End Class



This is a sample about how to create a splash screen:
http://msdn.microsoft.com/en-us/library/aa446493.aspx
This sample also shows how to create a splash screen:
http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx
This thread shows how to use a splash screen:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8af9fd3e-f679-40b4-976a-46f42319e9a5

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 4:49 AM
Why do you want to do this? It's not working because you're using the splash screen wrong. A splash screen is used to show the user that the program is loading/processing, the user should not be able to interact with the program while the splash screen is up. Tell us why you're trying to do this and we can help you find a better way, or hack it this way if that's what you really want.
ScottyDoesKnow  Monday, September 28, 2009 6:11 PM
Hi scotty,

Iwas assigned the task of displaying a mappoint object onsplasch screen. (not my decision) Everything is working good.

I do have some buttons on this splashscreen. In the button click event i have the hide functionality which works.( hides the splashscren)
If i move the splashscreen to one corner,i can work on my form.

But, my boss wants the map to go backgroundwhen the user clicks on the form.

(If this does not work, we might consider using a form in place of a splashscreen.)

Thanks
rowter  Monday, September 28, 2009 6:42 PM
Use a form instead of a splash screen. There's no reason to use a splash screen here because you're not using it as a splash screen.

[Edit] If you want you can tell me why you're using a splash screen instead of a form.
ScottyDoesKnow  Monday, September 28, 2009 7:50 PM
No special reason for using a splashscreen.
Someone suggested Splashscreen to my boss and he asked me to use splashscreen.


May be i have to go back to Form.

Thanks
rowter  Monday, September 28, 2009 8:06 PM
http://social.msdn.microsoft.com/Forums/en-AU/Vsexpressvb/thread/e52a741d-a42c-44b1-b604-14b74f166435


Check that thread for some ideas on a splash screen or a log in screen.
I cannot see a need to hide the screen/form. Just instantiate a new one when you need to show it, then close it and dispose it.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, September 28, 2009 8:12 PM

Hi rowter,

Based on my understanding, you want to handle the mouse click event of FormA when SplashscreenB is shown. As general, the mouse click event only fired when the mouse is on the form. In your case, the mouse is out of SplashscreenB, the event would not fired. But we can call the SetCapture API to extend the mouse event region to the whole screen, then we are able to capture the event even the mouse is out of the form region. This is the code snippet:
FormA:

    Private SplashscreenB As SplashscreenForm
    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SplashscreenB = New SplashscreenForm()
        'Add this handler to handle the mouse clicking on FormA
        AddHandler SplashscreenB.MouseDown, AddressOf SplashscreenForm_MouseDown
    End Sub

    Private Sub SplashscreenForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Dim FormA As Form = Me
        If (FormA.RectangleToScreen(FormA.ClientRectangle).Contains(Control.MousePosition) _
           And Not SplashscreenB.RectangleToScreen(SplashscreenB.ClientRectangle).Contains(Control.MousePosition)) Then
            'If the mouse click on FormA, but not on SplashscreenB, run the code below.
            FormA.BringToFront()
            FormA.TopMost = True
            FormA.Activate()

            SplashscreenB.SendToBack()
            SplashscreenB.TopMost = False
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Show the splash screen.
        SplashscreenB.Show()
    End Sub



SplashscreenB:

Public Class SplashscreenForm
    Private Sub SplashscreenB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Extent the mouse click region to the whole screen.
        'In other words, you can handle the click event even you click a position out of the form.
        SetCapture(CType(Me.Handle, Integer))
    End Sub

    Private Sub SplashscreenB_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Reset the click region to the form rectangle.
        ReleaseCapture()
    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SetCapture(ByVal hwnd As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function ReleaseCapture() As Boolean
    End Function
End Class



This is a sample about how to create a splash screen:
http://msdn.microsoft.com/en-us/library/aa446493.aspx
This sample also shows how to create a splash screen:
http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx
This thread shows how to use a splash screen:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8af9fd3e-f679-40b4-976a-46f42319e9a5

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 4:49 AM

You can use google to search for other answers

Custom Search

More Threads

• MDI parent and child
• Manipulating html controls in a WebBrowser control
• How to convert string to int
• Adding a picturebox on a menustrip.
• xp icons
• bold and underline simultaneously(very very urgent)
• Problem Setting Control Width in FlowLayoutPanel
• MonthCalendar Border in Vista
• One scrollbar for two texboxes
• Painting of controls