Results 1 to 15 of 15

Thread: [RESOLVED] Form2 is opened repeatedly

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Resolved [RESOLVED] Form2 is opened repeatedly

    I have a problem with following code:

    Code:
    Dim hWnd As Integer = FindWindowNullClassName(0, "form_number2")
            If hWnd = 1 Then
            Else
                Form2.Show()
            End If
    Form2 is repeatedly opened every n-th seconds even window name is detected i.e. software is still running. What causes that behavior?
    Please dont forget to add good reputation if my advices were useful for you.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,120

    Re: Form2 is opened repeatedly

    What is the code in FindWindowNullClassName? Is this a custom method?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,929

    Re: Form2 is opened repeatedly

    1 seems like an improbable value for an hWnd, if that really is an hWnd. If the return from the method isn't exactly 1, then you'd certainly drop into the else.
    My usual boring signature: Nothing

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,884

    Re: Form2 is opened repeatedly

    I believe he tries to use an implementation of the FindWindow user32.dll to open a form on the same app.
    Something like:

    Code:
    Dim nWnd As IntPtr
        Dim zeroIntPtr As New IntPtr(0)
        Dim Wnd_name as String
    
        Wnd_name= "form_number2"
        nWnd = FindWindow(Nothing, Wnd_name)
      
        If nWnd.Equals(zeroIntPtr) Then
            'No Form
      Form2.Show()
        Else
            'Form Found
        End If
    Personally I would not use Windows API when there are similar .net core implementations or when I don't get a better result in something.

    So I would be doing something like:

    Code:
     Dim ifrmcount As Integer = 0      
            Dim forms As FormCollection = Application.OpenForms
            For Each cont As Control In forms
                If TypeOf cont Is Form Then
                    If cont.Name = "form_number2" Then
                        ifrmcount = ifrmcount + 1      
                        Exit For           
                    End If
                End If
            Next
    
            If ifrmcount <= 0 Then
                 Form2.Show()
                Exit Sub
            End If
    Last edited by sapator; Feb 13th, 2021 at 04:53 PM.
    .

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,848

    Re: Form2 is opened repeatedly

    I think hWnd will be 0 if no window found, else <> 0...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Form2 is opened repeatedly

    Yeah, I am using this:
    Code:
    Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpWindownName As String) As Integer
    @sapator
    it does not work either.
    Please dont forget to add good reputation if my advices were useful for you.

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,884

    Re: Form2 is opened repeatedly

    That is why I suggested the other way to do so.
    Anyhow, this will work fine:

    Code:
      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                          ByVal childAfter As IntPtr, _
                          ByVal lclassName As String, _
                          ByVal windowTitle As String) As IntPtr
        End Function
    
    
    ''''
      Dim i As IntPtr
            i = FindWindowEx(IntPtr.Zero, IntPtr.Zero, vbNullString, "form_number2")  'The .text of the form
    
      If i.Equals(0) Then
           ' no form
            End If
    .

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,848

    Re: Form2 is opened repeatedly

    Why use the API at all if Form2 is part of the same app? Application.OpenForms is the easier method...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,848

    Re: Form2 is opened repeatedly

    Code:
    Dim isOpen as Boolean = False 
    
    For Each f As Form In Application.OpenForms.ToArray
        If TypeOf f Is Form2 Then
            If f.Text = "your Form2 title" Then
                isOpen = True   
            End If
        End If
    Next
    
    If isOpen = False Then
         Form2.Show()
    End If

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,884

    Re: Form2 is opened repeatedly

    That's what I also suggested at post #4 but if he wants to do it with API then that's fine by me.
    .

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,848

    Re: Form2 is opened repeatedly

    Quote Originally Posted by sapator View Post
    That's what I also suggested at post #4 but if he wants to do it with API then that's fine by me.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Form2 is opened repeatedly

    Both forms are independent.
    I am interested why my prior code does not work:

    Code:
    Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpWindownName As String) As Integer
    Dim hWnd As Integer = FindWindowNullClassName(0, TextBox1.Text)
            If hWnd = 1 And Process.GetProcessesByName("ExpressionEvaluatorGUI").Length > 0 Then
            Else
                MsgBox("Process NOT found!", MsgBoxStyle.Critical)
            End If
    albeit it is rewritten from videotutorial "how to check if a process is running in visual basic net 2010" where it works fine via window name.
    textbox1.text value is "ExpressionEvaluatorGUInazov" - exactly rewritten window name from another project. Of course EEGUInazov is running all the time...
    process name is also correct.
    Please dont forget to add good reputation if my advices were useful for you.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,288

    Re: Form2 is opened repeatedly

    Quote Originally Posted by VB.NET Developer View Post
    Both forms are independent.
    I am interested why my prior code does not work:
    ....
    Post #3 and Post #5?

    What is the purpose of comparing hWnd to 1? The chances of it being 1 is probably several billion to one.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Form2 is opened repeatedly

    My prior code was incomplete - there was "counter", but I prior thought that its not neccessary since the videoguy also checked how many processess are detected. But now it works flawlessly!

    Code:
    Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpwindowName As String) As Integer
    Dim counter As Integer = 0
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim hWnd As Integer = FindWindowNullClassName(0, TextBox1.Text)
            If hWnd > 0 And Process.GetProcessesByName("ExpressionEvaluatorGUI").Length > 0 Then
                MsgBox("Process found")
            Else
                MsgBox("Process NOT FOUND!", MsgBoxStyle.Critical)
            End If
        End Sub
    Please dont forget to add good reputation if my advices were useful for you.

  15. #15
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,459

    Re: [RESOLVED] Form2 is opened repeatedly

    Yes, of course the introduction of a new variable that isn't actually used in your code fixed it...
    ...and not the fact that you also changed the logic of your If statement just like was suggested in several earlier posts...

    Keep on keeping on.

    Good luck.

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