Windows Develop Bookmark and Share   
 index > Windows Forms Designer > form deactivate problem
 

form deactivate problem

Hi,

i have a problem with my forms.

The program consists of 3 forms: 1. background form (form1), 2. image form (form2) and 3. list form (form3).

Now i want my forms to be invisible when the first form gets deactivated but not when it gets deactivated by the other two forms.

I call the other 2 forms like this:

form2.show(me)

form3.show(me)

and these are in the form1's load event.

Can anyone help me out?

Thanks

Attila Fogel  Sunday, April 27, 2008 10:15 AM
Attila Fogel wrote:

The program consists of 3 forms: 1. background form (form1), 2. image form (form2) and 3. list form (form3).

Now i want my forms to be invisible when the first form gets deactivated but not when it gets deactivated by the other two forms.

Hi Attila,

Please try this idea:

Code Snippet

Public Class Form1

' P/Invoke API

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer

Declare Function GetForegroundWindow Lib "user32" () As Integer

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

Form2.Show()

Form3.Show()

Timer1.Interval = 1000

Timer1.Start()

End Sub

'Do workatone secondinterval

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

' Part 1: Get current active window title through P/Invoke APIs

' Get current Active Window handle

Dim hdl As IntPtr = GetForegroundWindow()

' Create Buffer for Active Window title

Dim windowTitle As String = Space(GetWindowTextLength(hdl) + 1)

' Get Active Window title, that is strTitle

GetWindowText(hdl, windowTitle, windowTitle.Length)

' Part 2: Do expectedsomething based oncurrent active window

' If Form1 is current active window

If windowTitle = "Form1" Then

Form2.Visible = True

Form3.Visible = True

Me.BringToFront()

Else ' If none of Form1,Form2 and Form3 is current active window

If windowTitle <> "Form2" And windowTitle <> "Form3" Then

Form2.Visible = False

Form3.Visible = False

'Form2.SendToBack()

'Form3.SendToBack()

End If

End If

End Sub

End Class

Move it from VB General forum to Windows Forms Designer forum for better responses.

Best regards,

Martin

Martin Xie - MSFT  Friday, May 02, 2008 8:47 AM
Attila Fogel wrote:

The program consists of 3 forms: 1. background form (form1), 2. image form (form2) and 3. list form (form3).

Now i want my forms to be invisible when the first form gets deactivated but not when it gets deactivated by the other two forms.

Hi Attila,

Please try this idea:

Code Snippet

Public Class Form1

' P/Invoke API

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer

Declare Function GetForegroundWindow Lib "user32" () As Integer

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

Form2.Show()

Form3.Show()

Timer1.Interval = 1000

Timer1.Start()

End Sub

'Do workatone secondinterval

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

' Part 1: Get current active window title through P/Invoke APIs

' Get current Active Window handle

Dim hdl As IntPtr = GetForegroundWindow()

' Create Buffer for Active Window title

Dim windowTitle As String = Space(GetWindowTextLength(hdl) + 1)

' Get Active Window title, that is strTitle

GetWindowText(hdl, windowTitle, windowTitle.Length)

' Part 2: Do expectedsomething based oncurrent active window

' If Form1 is current active window

If windowTitle = "Form1" Then

Form2.Visible = True

Form3.Visible = True

Me.BringToFront()

Else ' If none of Form1,Form2 and Form3 is current active window

If windowTitle <> "Form2" And windowTitle <> "Form3" Then

Form2.Visible = False

Form3.Visible = False

'Form2.SendToBack()

'Form3.SendToBack()

End If

End If

End Sub

End Class

Move it from VB General forum to Windows Forms Designer forum for better responses.

Best regards,

Martin

Martin Xie - MSFT  Friday, May 02, 2008 8:47 AM

You can use google to search for other answers

Custom Search

More Threads

• Preprocessor Macros with Windows Form Designer Compiler
• Visual inheritence problem
• Control missing in runtime
• Really horrible experience
• Initialize Form in Secondary or Other Monitor than primary
• Select the Design Surface
• Incorrect Serialization of Collection property in IExtenderProvider
• DataGridView visual inheritance
• Composite control components that are not clipped?
• Notifiers (bottom left corner popups)