Problems loading more than 1 form - VB.net - vb.net

I searched over the Stackoverflow and Google about solution for my problem but no way! so it's the time to ask my own question.
I'm making a big project using C# and VB.net (not our topic -_-)
I opened this question to ask about VB.net problem
I'm trying to load a lot of forms but in the same time, hide the previous form!
Firstly, look here on Form1:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Label1.Text = "Setting profile.dat=Roblox.AuthAccess ..." Then
Form2.Show()
Me.Hide()
End If
End Sub
This is a timer, which detect if Label1's Text is "Setting profile.dat=Roblox.AuthAccess ..." it must show Form2 and then close Form1 (THIS IS WORKING PERFECTLY) :-)
Form2:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.Url.ToString().Contains("home") Then
Me.Hide()
Form3.Show()
End If
End Sub
This will detect if "WebBrowser1"'s URL does contain "home" then it will hide Form2 and show Form3 (AND HERE THE PROBLEM STARTED!!!)
Problem is: It shows Form3 then after 2 seconds, Form2 come back again (now both are opened once) and they're opening and closing forever (I close them by going to Task Manager and ending "vshost32.exe")
Form3:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Close()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Form4.Show()
End Sub
End Class

Actually, stopping timers is still not a stable solution and disabling them are more better.
Just replace Timer4.Stop() with Timer4.Enabled = False .

Thank you very much #jmcilhinney :)
I never thought about stopping every "Timer" after it's job had finished!
Just replaced Timer4_Tick with:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Label1.Text = "Setting profile.dat=Roblox.AuthAccess ..." Then
Me.Hide()
Form2.Show()
Timer4.Stop()
End If
End Sub
So at last, I fixed that by adding Timer4.Stop() .

Related

Command does not execute when form opens

I'm trying to execute a command when a Form was opened, but the command is not executed, can someone help me?
Code:
Private Sub MenuPrincipal_Load(sender As Object, e As EventArgs)
MessageBox.Show("You answered yes, yes and no.")
End Sub
Unless you have an AddHandler somewhere we can't see, you should declare that your Sub handles the event:
Private Sub MenuPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
^^^^^^^^^^^^^^^^^^^
...
You want to use a command when the form opens.
Then put the command in Formload , in the example i use Form2 .
Make a Private Sub and then put the name in Load. Don't know what form you are using but changing is not so hard.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MenuPrincipal()
End Sub
Private Sub MenuPrincipal()
MessageBox.Show("You answered yes, yes and no.")
End Sub

Is there a Form event like "Shown" but runs the code every time it is displayed instead of the just first time?

Just noticed in one of my programs that the form event Shown only runs the code when the form is first displayed. Every other time after that it does not run the code. Is there a form event that does what shown does, but every time it is displayed instead of just the first? Or is there a way to get around it? This is the exact same for the Load event too.
Thanks heaps, appreciate any response.
I just tested with this in Form1:
Private f2 As New Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
f2.Show()
End Sub
and this in Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Form2_Load")
End Sub
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Visible Then
MessageBox.Show("Form2_VisibleChanged")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
End Sub
and it worked exactly as expected. Every time I clicked Button1 in Form1, Form2 was displayed. The first time I saw messages for "Form2_Load"and "Form2_VisibleChanged" and on subsequent occasions on for "Form2_VisibleChanged".

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub

VB.net opening and closing forms

I have a VB program that has two forms, i have coded the form load of each forms.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 1")
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 2")
End Sub
Here is how i switch through Form1 and Form2, i made use of a button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it. I want to be able to close the previous form so that when i will open it again, the form load event will trigger again.
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it.
This is exactly what is happening. The Hide method just hides the form from the user, effectively making it invisible.
What you're looking for is the Close method, which actually closes the form. (Since you are displaying the form using the Show method, you do not need to call Dispose.)
You will, however, not be able to close a form and continue to run code in its methods. So you'll need to reverse the order of the statements in your event handler functions, displaying the other form first and then closing itself. Make them look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
That will do what you want. The Load event will be triggered each time you call the Show method, because you're creating and showing a new form.
It is worth pointing out, though, that you're relying on an unusual characteristic of VB.NET, one that it retains from the older VB languages for backwards compatibility reasons. Instead of referring to an object of your form class (like you would have to do with all other class objects), you are referring to it by the type name (the name of the class itself). You really shouldn't do that, it causes all sorts of headaches and will confuse people reading your code. It is better to just instantiate a new form object, like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form1 object
frm.Show() ' ... and display it
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form2 object
frm.Show() ' ... and display it
Me.Close()
End Sub
When you run this code, you will likely run immediately into another problem: the first time you close Form1, your entire application will quit. This is because, by default for a new project, Form1 is designated as the "Startup form" in your project's properties ("My Project" in the Solution Explorer). You will either have to:
create a third form to use as the "main" form, and set the "Startup form" to this third form, or
change the "Shutdown mode" (also in "My Project") from "When startup form closes" to "When last form closes".
I am guesing the Me.Hide() will only hide the previous form and not totally close it
Yes, it does what it says. If you want to close the form then use Me.Close() instead. The Load event will fire again when you create the new instance.
You'll have to change a setting to ensure that doesn't also close your application. Project + Properties, Application tab, change the Shutdown mode setting to "When last form closes". And put the Me.Close() call after the Show() call.
I also had a similar question. When u .Hide() you are just storing it away in memory somewhere such that when it is re-opened it doesnt have to make a new form just recalls the one from memory hence that method is not called again. You have to destroy the form. So what you can do when navigating to another form is go to that form first and then destroy the current form like so Form2.Show()Me.Close(). Look at my question and my accepted answer. If that works please dont forget to tick this as your accepted answer.
When my form is hidden and reloaded from another form it is not executing the code in the Load event
If MessageBox.Show("Are you sure to close this application?", "Close",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
frmIndex.Show() //the main form
Else
e.Cancel = True
Me.Show() // The form open
End If
The form open going closing and going back to the main/index form. hope it help :) just play with the .show, .hide and e.cancel
I think you using a silly construction, but you should;
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Form1.close()
End Sub
Use the Shown event.
And use ShowDialog()
Form1.ShowDialog()
Yes. What you are doing is closing the form before it could open up form2.
Instead Of:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
You Need To Put:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.show
Me.hide
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.show
Me.hide
End Sub
If This Helps, Please Reply.

TextBox Highlighted Text Visual Basic

I have a problem with my TextBox. I'm trying to make a small program like memo. When you write something in the textbox it saves it for next time for you to open it again. But the problem is when you open the program once again, the old text is highlighted. So how I remove that?
Public Class Form1
Private Sub
Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.memo = TextBox1.Text My.Settings.Save()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.memo + Environment.NewLine
End Sub
End Class
See the TextBox.SelectionStart property