Results 1 to 15 of 15

Thread: [RESOLVED] Form2 is opened repeatedly

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.
    How? Under this post there is "RATE THIS POST" button. Click on it.

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

    Re: Form2 is opened repeatedly

    What is the code in FindWindowNullClassName? Is this a custom method?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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

    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
    6,597

    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
    25,464

    Re: Form2 is opened repeatedly

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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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
    25,464

    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
    25,464

    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
    6,597

    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
    25,464

    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
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.
    How? Under this post there is "RATE THIS POST" button. Click on it.

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

    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
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  15. #15
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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