form starting but not displaying vb.net - 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.

Related

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
I appreciate any help you can offer!
From my comments above:
When you create your instance of your second form called "formNew",
use the AddHandler command to wire up its FormClosed event to a method
in the main form. Then in the second form you can just call Me.Close()
and the method in the main form will fire where you can call
Me.Show()..
In main menu:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

Reset My.Settings to values last saved

I'm currently using a "Settings" form to set various settings for my application. What I'm trying to do is to revert settings back to before any changes the user has made before opening and changing a field. I have a text box with a data binding to a setting and when I make a change and click okay it gets saved the next time I open it. When I hit cancel it also gets saved. Not quite sure if I'm approaching this correctly.
Public Class frmSettings
Private _mysettings As Configuration.SettingsBase
Private Sub frmSettings_Load(...) Handles Me.Load
_mysettings = My.Settings
End Sub
Private Sub btnCancel_Click(...) Handles btnCancel.Click
For Each p As Configuration.SettingsPropertyValue In _mysettings.PropertyValues
My.Settings(p.Name) = p.PropertyValue
Next
Me.Close()
End Sub
Private Sub btnOkay_Click(...) Handles btnOkay.Click
My.Settings.Save()
Me.Close()
End Sub
End Class
Rather than using databound control you can simply load the value of the setting when the settings form loads. It is simple doing it that way, and it works. Otherwise you would have to make a clone of My.Settings: doing _mysettings = My.Settings is only creating a pointer to My.Settings, not a copy of it.
For example, I have a Form named ChangeConnectionString which has OK/Cancel buttons and a TextBox control named connString:
Public Class ChangeConnectionString
Private Sub bnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnOK.Click
My.Settings.connectionString = connString.Text
My.Settings.Save()
Me.Close()
End Sub
Private Sub bnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCancel.Click
Me.Close()
End Sub
Private Sub ChangeConnectionString_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connString.Text = My.Settings.connectionString
End Sub
End Class

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

Why do I get an InvalidOperationsException error?

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...