Where is the bug?
-
Hi I have formulated a function that checks the titles of the IE pages open and if required page is found, takes the appropriate action (in this code snippet, just tells by message box) The problem is function does not do the required job. Where is the bug? Any help would be appriciated. Thanks (vb.net version 2003) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strNames() As String = {"Yahoo", "Gmail", "Hotmail"} Dim bln As Boolean = FindWindow(strNames) MsgBox(bln.ToString) End Sub Private Function FindWindow(ByVal Names() As String) As Boolean Dim pr As Process() = Process.GetProcessesByName("iexplore") If pr.Length > 0 Then For i As Integer = 0 To UBound(pr) For j As Integer = 0 To UBound(Names) If pr(i).MainWindowTitle.IndexOf(Names(j)) <> -1 Then Return True Exit Function End If Next j Next i End If End Function reman
-
Hi I have formulated a function that checks the titles of the IE pages open and if required page is found, takes the appropriate action (in this code snippet, just tells by message box) The problem is function does not do the required job. Where is the bug? Any help would be appriciated. Thanks (vb.net version 2003) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strNames() As String = {"Yahoo", "Gmail", "Hotmail"} Dim bln As Boolean = FindWindow(strNames) MsgBox(bln.ToString) End Sub Private Function FindWindow(ByVal Names() As String) As Boolean Dim pr As Process() = Process.GetProcessesByName("iexplore") If pr.Length > 0 Then For i As Integer = 0 To UBound(pr) For j As Integer = 0 To UBound(Names) If pr(i).MainWindowTitle.IndexOf(Names(j)) <> -1 Then Return True Exit Function End If Next j Next i End If End Function reman
Amer Rehman wrote:
The problem is function does not do the required job.
OK, so what does it do? My guess is that you are finding that Process.GetProcessesByName("iexplore") returns one process. If you look at the process tab of task manager you will see that only one instance of internet explorer runs irrespective of the number of windows it has open. Contrast this to Notepad which starts one instance per window. The title you are retrieving via pr.MainWindowTitle is the current iexplore window. By the way I got all of this information from four lines of code and an inquisitive mind. How are your debugging skills today? Alan.
-
Hi I have formulated a function that checks the titles of the IE pages open and if required page is found, takes the appropriate action (in this code snippet, just tells by message box) The problem is function does not do the required job. Where is the bug? Any help would be appriciated. Thanks (vb.net version 2003) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strNames() As String = {"Yahoo", "Gmail", "Hotmail"} Dim bln As Boolean = FindWindow(strNames) MsgBox(bln.ToString) End Sub Private Function FindWindow(ByVal Names() As String) As Boolean Dim pr As Process() = Process.GetProcessesByName("iexplore") If pr.Length > 0 Then For i As Integer = 0 To UBound(pr) For j As Integer = 0 To UBound(Names) If pr(i).MainWindowTitle.IndexOf(Names(j)) <> -1 Then Return True Exit Function End If Next j Next i End If End Function reman
Not sure but this would be better:
Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strNames() As String = {"Yahoo", "Gmail", "Hotmail"} Dim bln As Boolean if FindWindow(strNames) then ' true - found MsgBox(bln.ToString) else ' False - Could not find end if End Sub Private Function FindWindow(ByVal Names() As String) As Boolean Dim lbReturn as Boolean = False Dim pr As Process() = Process.GetProcessesByName("iexplore") If pr.Length > 0 Then For i As Integer = 0 To UBound(pr) For j As Integer = 0 To UBound(Names) If pr(i).MainWindowTitle.IndexOf(Names(j)) <> -1 Then ' You do not need to return here or even exit function as ' functions should fall through to the end and then return ' its just better programming lbReturn = True End If Next j Next i End If Return lbReturn End Function
Regards Julian Mummery
Please Visit my FREE Bug / Fault Logging Website at FaultLogger.com**
**
-
Amer Rehman wrote:
The problem is function does not do the required job.
OK, so what does it do? My guess is that you are finding that Process.GetProcessesByName("iexplore") returns one process. If you look at the process tab of task manager you will see that only one instance of internet explorer runs irrespective of the number of windows it has open. Contrast this to Notepad which starts one instance per window. The title you are retrieving via pr.MainWindowTitle is the current iexplore window. By the way I got all of this information from four lines of code and an inquisitive mind. How are your debugging skills today? Alan.
Thanks for reply, but I am sorry to say that you are wrong. First, what do I want? I want to prevent user from downloading other than the mail pages(Yahoo, Gmail,Hotmail). Process.GetProcessesByName("iexplore") returns an array of processes whose Title we can retrieve in this way pr(i).GetMainWindowTitle. If we change the argument "i" we can retrieve all the open windows of IE though the process type is one ("iexplore"). What does the application do? It gives wrong answers.i.e. if the mail page is open first, it says "True", but if it is opened after opening another IE page, it says "False" despite the fact that it is open.
reman
-
Thanks for reply, but I am sorry to say that you are wrong. First, what do I want? I want to prevent user from downloading other than the mail pages(Yahoo, Gmail,Hotmail). Process.GetProcessesByName("iexplore") returns an array of processes whose Title we can retrieve in this way pr(i).GetMainWindowTitle. If we change the argument "i" we can retrieve all the open windows of IE though the process type is one ("iexplore"). What does the application do? It gives wrong answers.i.e. if the mail page is open first, it says "True", but if it is opened after opening another IE page, it says "False" despite the fact that it is open.
reman
-
Thanks for reply, but I am sorry to say that you are wrong. First, what do I want? I want to prevent user from downloading other than the mail pages(Yahoo, Gmail,Hotmail). Process.GetProcessesByName("iexplore") returns an array of processes whose Title we can retrieve in this way pr(i).GetMainWindowTitle. If we change the argument "i" we can retrieve all the open windows of IE though the process type is one ("iexplore"). What does the application do? It gives wrong answers.i.e. if the mail page is open first, it says "True", but if it is opened after opening another IE page, it says "False" despite the fact that it is open.
reman
You're right I am wrong, or maybe we're both right! When I want an extra window I have always used IE's menu File..New..Window which doesn't start a new process and then there is only one title available, that of the current window. I can get another iexplore process from the start menu and then as you say there is a title for each window. Alan.
-
You're right I am wrong, or maybe we're both right! When I want an extra window I have always used IE's menu File..New..Window which doesn't start a new process and then there is only one title available, that of the current window. I can get another iexplore process from the start menu and then as you say there is a title for each window. Alan.
Dear Mr. Alan After replying your first mail, I checked the code and found that I was maistaken. I did'nt check the Vb Documentation before writing code.Perhaps the solution to my problem is by using some API call. There is no managed way in VB 2003 to get all the open window. Or if there is, I'm not aware of it. And I'm using IE version 6. Thanks Amer
reman