Why do I get an InvalidOperationsException error? - vb.net

Here's the deal. I tried using classes instead of the usual modules(in an attempt to try a different approach[aside from what I know that is] to OOP). So I used classes and in a simple showing and hiding of forms, I got an InvalidOperationsException error. Weirded out, I removed the OOP parts and just tried calling the other form directly on the form itself and still got the same error.
Here's the error I get:
An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'
Here's the code:
Private Sub btnNewSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click
'This ought to have opened the new form via a method in the class
'order.NewOrder()
frmNewOrder.Show()
Me.Hide()
End Sub
either way, I get the same error.
Tried using modules instead. Here's the code:
Public Sub ShowForm(ByVal frmName As String)
If frmName = "Order" Then
frmOrders.Show()
ElseIf frmName = "AddOrder" Then
frmAddOrder.Show()
End If
End Sub
Now so far (in all my programming experience) this ought to work just fine, but it still returns the same error..
Update!
Tried removing all OOP aspects in form calling and left a module to simply show or hide some controls in one form.
Here's the code in the module:
Public Sub DesignSelect(ByVal design As String)
If design = "Basic" Then
frmAddOrder.lblD3.Hide()
frmAddOrder.cmbD3Color.Hide()
frmAddOrder.cmbD3Type.Hide()
frmAddOrder.lblD4.Hide()
frmAddOrder.cmbD4Color.Hide()
frmAddOrder.cmbD4Type.Hide()
Else
End If
End Sub
Now correct me if I'm wrong, but I do believe nothing is wrong with it right?
Now here's the code of the form where the module was used:
Dim selectedDesign As String = ""
Private Sub frmSalesTrans_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub frmSalesTrans_FormClosing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
'ShowForm("Order")
frmOrders.Show()
End Sub
Private Sub rdbBasic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbBasic.CheckedChanged
selectedDesign = "Basic"
DesignSelect(selectedDesign)
End Sub
And here's the code of the form that calls the form above:
Private Sub frmSales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnNewSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click
Me.Hide()
frmAddOrder.Show()
End Sub
Now it just boggles me why I get this error.. If I removed all OOP (including the subprocedure DesignSelect), it works fine. Kindly enlighten me on this...

Related

form starting but not displaying vb.net

So I was coding a program that opens 20 web browsers on one page, for a youtube or view bot any URL really, but when I launched the program It displayed the text box I put in to see if it was actually starting and then nothing else
So my question is, Whats wrong with my code?
startup form (where you can launch the main code with button 1)
Public Class Startup
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Settings.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
views.Show()
End Sub
End Class
main form
Public Class views
Private Sub views_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show("Form Initalised")
WebBrowser1.Navigate(My.Settings.url)
WebBrowser2.Navigate(My.Settings.url)
WebBrowser3.Navigate(My.Settings.url)
WebBrowser4.Navigate(My.Settings.url)
WebBrowser5.Navigate(My.Settings.url)
WebBrowser6.Navigate(My.Settings.url)
WebBrowser7.Navigate(My.Settings.url)
WebBrowser8.Navigate(My.Settings.url)
WebBrowser9.Navigate(My.Settings.url)
WebBrowser10.Navigate(My.Settings.url)
WebBrowser11.Navigate(My.Settings.url)
WebBrowser12.Navigate(My.Settings.url)
WebBrowser13.Navigate(My.Settings.url)
WebBrowser14.Navigate(My.Settings.url)
WebBrowser15.Navigate(My.Settings.url)
WebBrowser16.Navigate(My.Settings.url)
WebBrowser17.Navigate(My.Settings.url)
WebBrowser18.Navigate(My.Settings.url)
WebBrowser19.Navigate(My.Settings.url)
WebBrowser20.Navigate(My.Settings.url)
Threading.Thread.Sleep(My.Settings.Time)
reload.Show()
Me.Close()
End Sub
End Class
and form reload mentioned in the main form.
Public Class reload
Private Sub reload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Close()
views.Show()
End Sub
End Class
Try to use the Form.Shown event instead of the Form.Load event. In Form.Load the controls are often not fully initialized and if something goes wrong there the initialization of the whole form sometimes goes awry. It's better to use the Form.Shown for such lengthy procedures you have there.

WP7 - How to refer to the MainPage.xaml.vb from App.xaml.vb? (Passing data?)

so basically I have my MainPage.xaml.vb and App.xaml.vb and in the app.xmal.vb i added :
Private Sub Add_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Save_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub EditAppBar_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub DeleteAppBar_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
This is because I added more than 1 ApplicationBars from looking at the MSDN Sample. The problem is that how do i refer to mainpage to make the code work? e.g. the "Add" will contain "pvt.SelectedIndex = 2" and when it's clicked it's meant to navigate the the 3'rd page but i can't seem to do it in here. If i take this code and add it to the "MainPage" it will work fine. The error i get is "'pvt' is not declared. It may be inaccessible due to its protection level." How do i fix this?
Any ideas?"
I got it solved by using: Dim mp = TryCast(DirectCast(Application.Current, App).RootFrame.Content, MainPage) mp.pvt.SelectedIndex = 2

vb2010 pass data from one form to another

I am trying to pass a value of a textbox on frmMain to a textbox on frmDepartment. I have tried the following code which I thought would work but it dosen't. I ma a new user to VB and come from a php background where it would have been a simple task of setting a seesion. Can anyone help with this? If you need to see more code, please ask. many thanks
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
In frmMain, I am getting value like this:
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.Show()
End Sub
Which is showing in frmMain ok.
In frmDepartment I have this code
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
'Dim customer As String
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
instead of putting the code within the txtDeptCustomer.TextChanged sub, try putting it within the frmDepartment_load:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
or you could set the frmDepartment text box text on the frmMain button click:
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.txtDeptCustomer.Text = txtCustomerActive.Text
frmDepartment.Show()
End Sub

Need Help In WebBrowser Control in VB.net

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
WebBrowser.Show()
WebBrowser.WebBrowser1.Navigate("www.carsonmap.com/hidalgo/login.cfm")
WebBrowser.WebBrowser1.Document.GetElementById("UserName").SetAttribute("value", "lrgvdc")
WebBrowser.WebBrowser1.Document.GetElementById("PW").SetAttribute("value", WebBrowser.TextBox2.Text)
End Sub
I need help with this code I keep getting an error saying Null Reference.
I got the idea from this Youtube Video check out to see what I am trying to accomplish.
https://www.youtube.com/watch?v=9EJXzWasTq4&list=PL42055376AE25291E&index=41
They used two buttons to enter to the website I am trying to enter to a website By using one button any ideas why doesnt it work.
When you use WebBrowser1.Navigate - it just begins to load document, the document is not available right away.
You need to use WebBrowser.DocumentCompleted Event to place your code that works on document's elements. E.g. something like
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
WebBrowser.Show()
WebBrowser.WebBrowser1.Navigate("www.carsonmap.com/hidalgo/login.cfm")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser.WebBrowser1.Document.GetElementById("UserName").SetAttribute("value", "lrgvdc")
WebBrowser.WebBrowser1.Document.GetElementById("PW").SetAttribute("value", WebBrowser.TextBox2.Text)
End Sub

Second form not showing value stored in first form when called

Hey all i am trying to figure out why my 2nd form is not displaying the value i recived in my first form.
The code for the first form is:
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
Call frm1110.clickButton(responceBack)
End Sub
The second form code is this:
Public Sub clickButton(ByRef theResponse As String)
txtNumber.Text = theResponse
'Call cmdNextFinish_Click(Nothing, Nothing)
End Sub
However, when i debug it to make sure there is something stored for theResponse, there is but for some reason it does not put it into the textbox. It's blank.
Any help would be great!
David
UPDATE
Ok so Form1:
Dim tmpForm3020 As New frm3020
Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
tmpForm3020.Show()
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
tmpForm3020.txtNumber.Text = responceBack
End Sub
If thats correct then i get an error on line:
xForm.txtNumber.Text = responceBack
Saying:
Cross-thread operation not valid: Control 'txtNumber' accessed from a thread other than the thread it was created on.
Are you explicitly creating an instance of your second form, or relying on the default instance? I.e. is "frm1110" the second form's class name, or an instance that you have new'd up? Make sure in either case that it is the same instance that is actually being displayed.
Dim tmpForm3020 As New frm3020
Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
tmpForm3020.Show()
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
TestData(responceBack)
End Sub
Private Sub TestData(ByVal xVal As String)
If InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf TestData))
' change Me to tmpForm3020 (if it does not work)
' tmpForm3020.Invoke(New MethodInvoker(AddressOf TestData))
Else
tmpForm3020.txtNumber.Text = xVal
End If
End Sub