"Specified cast is not valid" when creating form in vb.net - vb.net

I am new to programming and I am trying to create a program which will return the href links of a website. The links will then be displayed in a text box
I have created a backgroundworker for the code to run so that the program dosent freezes when extracting data, but however I got this error instead:
"An exception of type 'System.InvalidCastException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: Specified cast is not valid."
Would appreciate any help I can get. Thanks!
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.BackgroundWorker2.RunWorkerAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim pageHTML As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("a")
For Each pageHREF As HtmlElement In pageHTML
Dim picurl As String = pageHREF.GetAttribute("href").ToString
TextBox2.Text = TextBox2.Text & picurl & Environment.NewLine
Next
End Sub
End Class

Related

Error System.NotSupportedException was unhandled by user code

I try to get automatic click on a button on a page on my website
I have try multiple things and i can not get out of this exception
This is My code
Well wen i press the button1 its come an error
I describe below code
Public Class Form5
Dim CheckButton, skip_ad_button As HtmlElement
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
WebBrowser1.ObjectForScripting = True
WebBrowser1.ScriptErrorsSuppressed = True
End Sub
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
skip_ad_button = WebBrowser1.Document.GetElementById("skip_ad_button")
CheckButton = WebBrowser1.Document.GetElementById("skip_ad_button")
If Not skip_ad_button Is Nothing Then
skip_ad_button.InnerText = "skip_ad_button" 'Replace testID by the ID you want
End If
If Not CheckButton Is Nothing Then
'some code here
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http:\\mediaads.eu/proxy")
End Sub
End Class
This is the error
System.NotSupportedException was unhandled by user code
HResult=-2146233067
Message=Property is not supported on this type of HtmlElement.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.HtmlElement.set_InnerText(String value)
at WindowsApplication1.Form5.WebBrowser1_DocumentCompleted(Object sender,
WebBrowserDocumentCompletedEventArgs e) in \Documents\Visual Studio
2012\Projects\WindowsApplication1\WindowsApplication1\Form5.vb:line 23

Visual Basic Beginner ..SubStrings

I'm new to programming and I'm trying to figure out this simple question! The language is Visual Basic! The Question is below:
"Users of a computer program often like to enter numbers with commas inserted in the middle, such as "1,234,000,688". Most computer languages consider this format to be non numeric. Write a program that inputs a number containing no more than three commas, and produces a string containing the same number without the commas"
When I enter this number: 1,234,000,688 and hit Display in Visual Basic I get this error message --> Argument is out of range Exception was unhanded
I'm not exactly sure why this is happening because I'm within my strUserInput length.
My Code:
Public Class Form1
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Variable declarations
Dim strUserInput = txtUserInput.Text
Dim strOutputNumber1 As String
Dim strOutputNumber2 As String
Dim strOutputNumber3 As String
Dim strOutputNumber4 As String
' 1,234,000,688
strOutputNumber1 = strUserInput.Substring(0, 1)
strOutputNumber2 = strUserInput.Substring(2, 4)
strOutputNumber3 = strUserInput.Substring(5, 8)
strOutputNumber4 = strUserInput.Substring(9, 12)
lblDisplayNumber.Text = strOutputNumber1 & strOutputNumber2 & strOutputNumber3 & strOutputNumber4
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lblDisplayNumber.Text = String.Empty
txtUserInput.Text = String.Empty
End Sub
End Class
You're getting that exception because of the way you're using substring, the last one there starts at index 9 and extends 12 characters...that would be a 22 character number, and the text you have as input is probably much shorter. You don't need to make things overly complicated for yourself using substrings, all of the above code could be greatly condensed and cleaned up by simply using String.Replace like this:
Public Class Form1
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
lblDisplayNumber.Text = txtUserInput.Text.Replace(",", "")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lblDisplayNumber.Text = String.Empty
txtUserInput.Text = String.Empty
End Sub
End Class

JS error Vb.net embedded browser

I have a program with an embedded browser. When I try to load a site, (Motdgd.com) it throws the error
the value of the property 'atob' is null or undefined not a function object
Line 101
Char 5
I have searched extensively, but cannot find an answer.
Public Class Form1
Dim Count As Integer
Dim AdLink As String
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Count = 1
UserLink = TextBox1.Text
Label1.Text = UserLink
WebBrowser1.Url = New Uri(UserLink)
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
End Class
SOLVED
InitializeComponent();
RegistryKey Key = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
Key.SetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe", 11999, RegistryValueKind.DWord);
Key.Close();
Will force IE11 to run, via the registry

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

vb.net bug when trying to showdialog

when i try to show dialog of one of my forms it displays this error all other forms work perfectly i have tried to copy the code into another form happened the same
here is the error:
A first chance exception of type 'System.InvalidOperationException'
occurred in hyper market system.exe
Additional information: An error occurred creating the form. See
Exception.InnerException for details. The error is: Object reference
not set to an instance of an object.
If there is a handler for this exception, the program may be safely continued.
here is all my code
Public Class farm
Dim inifile As New IniFile(myfiles & "\system.ini")
Dim myfiles As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\HMsystem"
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Dim Count As Integer = 0
Dim total As Long = 0
Dim productnum As String = TextBox1.Text
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim productnum As String = TextBox1.Text
Dim num As String = TextBox2.Text
Dim itemname As String = inifile.GetString("productname", productnum, "غير موجود")
Dim price As String = inifile.GetString("productprice", productnum, "غير موجود")
ListView1.Items.Add(productnum)
ListView1.Items(Count).SubItems.Add(itemname)
ListView1.Items(Count).SubItems.Add(num)
ListView1.Items(Count).SubItems.Add(price)
total += price
Count += 1
Dim a As String = inifile.GetString("productquan", productnum, "0")
Dim itemquannow As String = inifile.GetString("productquan", productnum, "0")
If itemquannow <= 5 Then
Else
MsgBox("لم يبق الا 5 من هذا المنتج")
End If
inifile.WriteInteger("productquan", productnum, a - num)
MsgBox("تم الاضافة")
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label4.Text = total & " السعر النهائي"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListView1.Clear()
TextBox1.Clear()
TextBox2.Clear()
Count = 0
total = 0
MsgBox("تم الشراء بنجاح")
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
End Class
When you get an error message like that, i.e. "An error occurred creating the form", it almost always means the same basic issue: you have an event handler that is being raised because of a property value set in the designer and that event handler assumes that the user has made that change after the form has been displayed. For instance, if you set the Text property of a TextBox in the designer then that's going to raise the TextChanged event. If you have handled that event then your event handler is going to be executed during the initialisation of the form, before it has been displayed to the user. If you assume that, for instance, an item is selected in a ComboBox then you're in trouble because there will be no such selection.
As the error message states, look at the InnerException, which will tell you exactly where the original exception was thrown. That will tell you which event handler is the issue and you can then look at the code in that method and determine what would cause an issue if the form had not yet been displayed. If in doubt, update your question with the code of that event handler and tell us where the exception was thrown, which the stack trace will tell you.