vb.net bug when trying to showdialog - vb.net

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.

Related

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

"Specified cast is not valid" when creating form in 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

how to run a function/sub after loading the form window in VB?

I have a function that gets User ID from USB badge reader, used to log in an application.
when I run the app, the log in window does not appear until I swipe the tag.
I need to know if it`s possible to load the windows, then to start running the function that gets the data from the USB.
Thanks :)
Private Sub SerialPort1_DataReceived()
'Threading.Thread.SpinWait(1000)
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
'read entire string until .Newline
readBuffer = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
WWIDTextBox.AppendText(readBuffer)
End If
End Sub
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
SerialPort1_DataReceived()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SerialPort1_DataReceived()
End Sub
The problem is that you are calling the ReadLine method, which is a blocking (synchronous) method. In other words, when you call it, the method does not return the value until it has the value to return. Because of that, it stops execution on the current thread until a complete line is read (when the badge is swiped). Since you are on the UI thread when you call it, it will lock up the UI until the badge is swiped.
Instead of calling your SerialPort1_DataReceived method from the UI thread, you can do the work from a different thread. The easiest way to do that is to drag a BackgroundWorker component onto your form in the designer. Then you can add code like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
Dim readBuffer As String = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
e.Result = readBuffer
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
WWIDTextBox.AppendText(CStr(e.Result))
End Sub
Working on VS2013, I came across the same issue, I needed to to a datagridview refresh (colors in the gridrows). This worked for me.
Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
'do action...
End If
End Sub
Try Form Activated Event
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
'Call your function here
End Sub
It call the function After the Form Loads...
Private Sub loadCombo()
Dim sqlconn As New OleDb.OleDbConnection
Dim connString As String
connString = ""
Dim access As String
access = "select slno from atable"
Dim DataTab As New DataTable
Dim DataAdap As New OleDbDataAdapter(access, connString)
DataAdap.Fill(DataTab)
ComboBox1.DataSource = DataTab
ComboBox1.DisplayMember = "slno"
End Sub