VB.NET - Check if webbrowser's url has changed - vb.net

So basically I am asking how to check if the URL of the webbrowser has changed (is diferrent from the previous one).
Thank you.

You can check ...
Sub webbrowser1_Complete(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
//Webbrowser1.url property to get valu of url
End Sub

Ok from what I can understand from your question this is what I would do. First create a structurethis will allow you to store data that you may want to use again. Next create a Function in this case with a Boolean return that checks to see if WebBrowser1's current url is the same as the one we have stored within our structure.And once you have done that, I would create a new WebBrowser1 Event in this case WebBrowser1_DocumentCompleted to trigger the Function to compare both the WebBrowser1 url textbox and the structures stored string when a web page as completely loaded.
Public Class Form1
Dim urlSettings As urlSetting
Structure urlSetting
Public url As String
End Structure
Private Function checkURL(url As String) As Boolean
Dim changed As Boolean = True
If Not urlSettings.url = url Then
changed = False
End If
Return changed
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate(New Uri(TextBox1.Text))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Not checkURL(TextBox1.Text) Then
urlSettings.url = TextBox1.Text
MessageBox.Show("The URL has changed")
End If
End Sub
End Class
Of course you can modify so suit your needs, however this should get you on your way. :) MSDN Information Structure: https://msdn.microsoft.com/en-us/library/4ft0z102.aspx WebBrowser Control : https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(v=vs.110).aspx Return Statesments eg Function: https://msdn.microsoft.com/en-us/library/2e34641s.aspx

Related

Visual Basic web application call function from multiple objects

I have a Visual Basic web application where I will have a 5x5 grid displayed on the web page. Each grid box will contain a button. I want to be able to access the button attributes when any button is clicked. I would assume I would create a private function to handle each button click (I would create 25 functions), but I want each private function to call a public function which will receive the object and the grid coordinates.
My buttons on the web pages are called btn_column1_row1, btn_column2_row1, btn_column3_row1, btn_column4_row1, btn_column5_row1, btn_column1_row2, etc.
I want to code the private functions like this:
Private Sub btn_column1_row1_Click(sender As Object, e As EventArgs) Handles btn_column1_row1.Click
inspectButton(e,100,1,1)
End Sub
Private Sub btn_column2_row1_Click(sender as Object, e As EventArgs) Handles btn_column2_row1.Click
inspectButton(e,100,2,1)
End Sub
Private Sub btn_column5_row5_Click(sender as Object, e As EventArgs) Handles btn_column5_row5.Click
inspectButton(e,500,5,5)
End Sub
Then, the function inspectButton should look something like this:
Function inspectButton(ByVal e as EventArgs, textvalue, column, row)
Dim Question as string = Session("question")
Dim Answer as string = Session("answer")
if e.text = textvalue then
e.text = Question(column, row)
else
e.text = Answer(column, row)
end if
return
End Function
Is this the correct way to pass the button object to the inspectButton function?
Private Sub btn_Click(sender as Object, e As EventArgs) Handles btn_column1_row1.Click, btn_column1_row2.Click, btn_column3_row5.Click, btn_column5_row5.Click, btn_column5_row1.Click 'etc
inspectButton(sender)
End Sub
Function inspectButton(ByVal btn as Object)
Dim Question as string = Session("question")
Dim Answer as string = Session("answer")
if btn.Text = btn.Text then
btn.Text = Question(btn.column, btn.row)
else
btn.Text = Answer(btn.column, btn.row)
end if
return
End Function
Maybe this can be a solution... i can't understand the textvalue,column and row parameters passed...
You can handle multiple events with a single sub.

How to update a form based on a checkbox status

I'm currently trying to code some macros for a software (Revit), and I ran into an issue I do not know how to solve.
So I have a windows form with two checkboxes and a list of elements, and I would like that list of elements to be updated depending on what the status of the checkboxes is.
Here's my checkbox status' code:
If StructcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.EngineeringPlan
vpList.Add(tmpVP)
End Select
End If
If LegcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.Legend
vpList.Add(tmpVP)
End Select
End If
Now the problem with that code is that it only checks the initial status of the checkboxes, but do not update the list when the checkboxes are checked/unchecked.
How to make it so that the list VpList is updated everytime a checkbox status is changed?
Thanks!
The key here is to add a sub that does the checkboxes checking. You will need that sub because it will called multiple times depending on the user's actions.
Because you are not providing any insights about tmpView and vpList I will stick with your code, however you should note that depending on what exactly are you trying to do your code can be simplified or rewritten to be a bit more efficient. For example you don't specify if you want the tmpVP value to be unique in the list or to be more than one times, I assume that you want to be unique, so here is the sub's code (please read the comments in the code):
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Now that you have the sub you can call it whenever you want. "Whenever you want" means on various user's actions, like checking or un-checking a checkbox and in various other places like form's initialization (loading) these are events. According to your question you need to call it from 3 different events.
1.When the form is loading in order to get the initial status:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
2.When the user changes the StructcheckBox status:
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
3.When the user changes the LegcheckBox status:
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Here is the full form's code:
Public Class Form1
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
End Class
Hope this helps.

How to pass ListBox selected item into file?

What I have:
Private Sub ChooseProgram_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseProgram.SelectedIndexChanged
Dim curItem As String = ChooseProgram.SelectedItem.ToString()
End Sub
Private Sub Install_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Install.Click
Dim jhin As System.IO.StreamWriter
jhin = My.Computer.FileSystem.OpenTextFileWriter("C:\Temp\jhin.bat", True)
jhin.WriteLine("$program = " & curitem.string")
jhin.Close()
End Sub
I just want to write the string into the file.
How is that possible?
Thx for ur help!
Hannir
UPDATE:
' Ensure an item is selected
If ChooseProgram.SelectedItem Is Not Nothing Then
curItem = ChooseProgram.SelectedItem.ToString()
End If
I get an error here.
"The Is operator does not accept operands of type " integer " . The operands must be reference types, or permit types , NULL values ​​."
Really thx for ur quick help! #Pro Grammer
If you just click Install, and nothing is selected, it ends in an error. So is it possible to say "You need to select an item before" or the Install button is just clickable when selected an item?
For starters you have to move the declaration of the curItem variable out of the SelectedIndexChanged method, to what's called Class level.
As it stands your variable is accessible within your SelectedIndexChanged method only, whereas if you move it to class level it will be accessible by everything within that class (the class in this case is your Form). You then just modify the variable from your SelectedIndexChanged method.
Dim curItem As String = "" 'We'll start with an empty string.
Private Sub ChooseProgram_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseProgram.SelectedIndexChanged
curItem = ChooseProgram.SelectedItem.ToString()
End Sub
Now you will be able to access the variable from your button and write it to a file.
The last thing you have to do is to close the StreamWriter that you create so that it will release the lock on the file. The easiest and best way to do so is wrapping it in an Using/End Using block.
Private Sub Install_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Install.Click
Using jhin As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("C:\Temp\jhin.bat", True)
jhin.WriteLine("$program = " & curItem)
End Using
End Sub
EDIT:
To make your button clickable only when an item is selected, first set the button's Enabled property to False via the Property Window in the designer, then use this code in the SelectedIndexChanged event:
Install.Enabled = ChooseProgram.SelectedIndex >= 0
If ChooseProgram.SelectedIndex < 0 Then Return
curItem = ChooseProgram.SelectedItem.ToString()
This is one option:
Private Sub Install_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Install.Click
Dim curItem as String = ""
' Ensure an item is selected
If ChooseProgram.SelectedItem IsNot Nothing
curItem = ChooseProgram.SelectedItem.ToString()
End If
' Double checking valid input
If String.IsNullOrWhiteSpace(curItem) Then
' Handle empty input - Display message, etc
' Exit Sub (unless bat handles empty)
End If
Dim jhin As System.IO.StreamWriter
jhin = My.Computer.FileSystem.OpenTextFileWriter("C:\Temp\jhin.bat", True)
jhin.WriteLine("$program = " & curItem)
jhin.Close()
End Sub
Instead of using the ChooseProgram_SelectedIndexChanged event, you can deal with it from here, since you aren't performing any other actions in that method. If you wanted to still use that event, you would assign the string value into a field which could be accessed across the class. Check out Visual Vincent's answer for this example, and also of a Using block, which removes the need to call jhin.Close() manually and also provides a much clearer format

Is there a way to get the input from a Load event handler (entered via inputBox) to be used Globally in the program?

I am creating a form that will prompt the user to enter a file name upon loading the file. The issue I encounter is that my variable I use to store the input is not recognized in another one of my procedures. The code here shows my current set up.
Imports System.IO
Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
Dim strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEmail.Clear()
txtExtension.Clear()
txtFirst.Clear()
txtLast.Clear()
txtMiddle.Clear()
txtNumber.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim inputFile As New StreamWriter(strFileName)
If File.Exists(strFileName) = True Then
inputFile = File.CreateText(strFileName)
inputFile.Write(txtEmail.Text)
inputFile.Write(txtExtension.Text)
inputFile.Write(txtFirst.Text)
inputFile.Write(txtLast.Text)
inputFile.Write(txtMiddle.Text)
inputFile.Write(txtNumber.Text)
inputFile.Write(txtPhone.Text)
inputFile.Write(cmbDepart.Text)
Else
MessageBox.Show("" & strFileName & "Cannot be created or found.")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The load event handler is where I want the user to input the name of the file.
Change the scope of your variable to outside of your load method...
Public Class frmEmployee
Private strFileName As String
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
You could use My.Settings and load the file string there and save it. All forms can now access that. When you close the the app set it to String.Empty if you want it to be.
You also could be taking advantage of a class object for the fields you are capturing then using either BinaryFormatter or XmlSerializer to store the data. Better databinding, creation and reconstruction using an object.
My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()
The problem with the approach you have there is that your variabl strFileName is scoped to the class frmEmployee.
You need to set up a genuinely global variable (at the application startup) an then use that. So you will need a Sub Main as an entry point to your application See here, and just before that create a public variable to hole the file name.
So you might have something like this for your application startup:
Public fileNametoUse as string
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub
Then in you load sub for frmEmployee you would have:
fileNametoUse = InputBox("Please name the file you would like to save the data to: ")

How is it possible to parse the URL of the desired popup to the popup-form AND show hints/tooltips in the WebKit-Component?

I'm trying to use the WebKit-component (http://www.webkit.org/) in VB with the help of Visual Studio 2008.
This is running without problems, except for two following two issues:
1. Hints/Tooltips are not shown (e.g. as there usually will appear one if you stay with the mouse over the Google-logo)
2. If there's a popup-window, I don't know how to get the new desired URL.
I'm already working a few days on this matter and couldn't find any solution yet :(
Maybe you know a solution to this problem.
Cheers
Markus G.
P.S.: If you need more than the following Source Code to analyze the problem, then let me know ...
Source Code Form1
Imports System.IO
Imports WebKit
Public Class frmMain
Private _url As String
Private _mode As String
Private _popupUrl As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim bLogging As Boolean
setWindowAndBrowserSettings()
_url = "http://www.google.com"
browserComp.Navigate(_url)
End Sub
Private Sub setWindowAndBrowserSettings()
Me.Text = "Test - Browser"
Me.WindowState = FormWindowState.Maximized
browserComp.Dock = DockStyle.Fill
browserComp.Visible = True
End Sub
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
'frmPopup.WindowState = FormWindowState.Maximized
frmPopup.Text = "ixserv - POPUP"
frmPopup.popup.Navigate(_popupUrl)
frmPopup.Show()
End Sub
Private Sub browserComp_NewWindowRequest(ByVal sender As Object, ByVal e As WebKit.NewWindowRequestEventArgs) Handles browserComp.NewWindowRequest
e.Cancel = False
_popupUrl = browserComp.Url.ToString ' WHERE can I get the clicked URL? This is the old one of the remaining window
End Sub
End Class
Code Form2
Public Class frmPopup
End Class
Following popup/new-Window-Create-function works for me:
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
frmPopup.Text = "POPUP"
Dim popupBrowser As WebKit.WebKitBrowser
popupBrowser = e.WebKitBrowser
frmPopup.Controls.Add(popupBrowser)
frmPopup.Show()
End Sub
whereas frmPopup is a new form.
Before I tried this I already added the Webkit-component to the new form, which might had been the problem. I assume, the trick is, to create a new WebKitBrower-element that is directly connected to the argument e.WebkitBrowser instead of overloading an existing webkitbrowser-component in the form. Don't ask me for reasons for this now (I really don't know) :P
Oh, I should add that I used the Webkit.NET component. The same trick works also for the OpenWebkitSharp-wrapper
The hint-problem still remains ...