How to make a self replicating program in vb.net? - vb.net

I'm trying to make a little prank program where a form keeps opening until it hits a certain number like 50 or something, I've tried this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Show()
End Sub
End Class
But that doesn't work, anyone willing to help? Thanks

Ditto what litelite said. If you want to show a new instance of the form, Show() won't cut it.
In fact, once you close that form, the timer you're using will be lost. So you'll need to handle the Form.Closing event as well. Since we're just having fun here, I'd suggest that you make that work like cutting off the head of a Hydra, two more replace it.
Try something like this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim _newForm as Form2 = new Form2()
_newForm.Show()
End Sub
Private Sub Me_Closing(sender As Object, e As EventArgs) Handles Form2.Closing
Dim _newForm as Form2 = new Form2()
_newForm.Show()
Dim _newForm2 as Form2 = new Form2()
_newForm2.Show()
End Sub
End Class

Related

Change Backcolor for all inherited forms

i have a baseform and a few forms inherited from that.
Call them BaseForm and Form1, Form2
I want to select a color in Form1 and these color is set in all inherited forms.
That works, if the form is open after the color is changed.
Let me explain.
I open Form1
select the color and set a global variable(glColorVariable).
The Backgroundcolor in the Baseform.Designer.vb is set
me.backcolor=glColorVariable
The color in Form1 didn't change
If i now open form2 the color in form2 IS changed.
How can i ... repaint my Baseform in Form1 so the color is changed.
Thank you verry much!
Cornelia
You can't loop forms so that you can change a parameter on them.
I think the Assembly.GetExecutingAssembly().GetTypes() is dead end for this.
One solution, if you fill an list element with your forms.
Dim Forms As New List(Of Form)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Forms.Add(My.Forms.Form1)
Forms.Add(My.Forms.Form2)
Forms.Add(My.Forms.Form3)
End Sub
Sub SetBackColors(ByVal c As Color)
For Each f In Forms
f.BackColor = c
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SetBackColors(Color.Red)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Show()
Form3.Show()
End Sub
or what Jimi wrote, use DataBindings:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.DataBindings.Add("BackColor", Me, "BackColor", False, DataSourceUpdateMode.OnPropertyChanged)
Form3.DataBindings.Add("BackColor", Me, "BackColor", False, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.BackColor = Color.Red
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Show()
Form3.Show()
End Sub

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.

I have multiple panels on same form and I want Passing a value between 2 panel

I have multiple panels on the same form and I want Passing a value between 2 panels
I want to enter Username in the first panel and Show as Label in next panel
please not in between 2 Form but 2 Panels.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Guna.UI.Lib.GraphicsHelper.ShadowForm(Me)
End Sub
Private Sub GunaButton1_Click(sender As Object, e As EventArgs) Handles GunaButton1.Click
pnLogin.BringToFront()
GunaTransition1.Hide(pnLogin)
End Sub
Private Sub GunaButton2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GunaGradientButton2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GunaTextBox1_Click(sender As Object, e As EventArgs) Handles GunaTextBox1.Click
End Sub
End Class
If all the controls are in the same Form, then the container they are within is irrelevant.
The code would look something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub
You need to change the control names to what you have on your form for the Button, TextBox and Label.
You can simply declare the variable in form class scope and use it! can you explain more details?
Update:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Lable.text = TextBox.text
EndSub

I cant change my form

I have big problem. I have 2 forms in my VB.NET project. The names of Forms are Form1 and Form2. I have a button with this codes:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim Login As New Form2
Login.Show()
End Sub
Now when I click in this button Form2 will open, but after 3 or 4 minutes Form1 will open again.
I should say Me.close() to same happen with Me.Hide() for me.
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fff As New Form2
fff.Close()
Form3.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MsgBox("Sorry", MsgBoxStyle.MsgBoxHelp, "Error")
End Sub
I need help please
try this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Login As New Form2
Form2.Show()
Me.dispose()
End Sub
it should solve the problem

How to load a form again without exiting the application?

I have 2 form. Form1 and Form2. In form1 there's a button that will go to form2 if it is click. If I click that button if form1, form2 will load and integer a will become 1. If I click the button in form2, integer a will become 0 and it will back to form1. Since im in form1, if I click again the button in form1 it will go to form2 but form2 will not load again. Is theres a way to load again the form? Heres my example:
Form1:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
End Class
Form2:
Public Class Form2
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Me.Hide()
Form1.Show()
End Sub
End Class
Thanks in advance!
Form2's Load method only gets called once because that is the normal life cycle for a form. The load event only gets called once before the form is loaded for the first time.
From Form.Load Event:
Occurs before a form is displayed for the first time.
The solution depends on your needs. If you need to keep the state of the form even when it is hidden then you want to use the VisibleChanged event.
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
MsgBox("Visible changed")
End If
End Sub
If you don't need to keep the state then you can discard Form 2 and recreate it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim Form2 = New Form2()
Form2.Show()
End Sub
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Form1.Show()
Me.Close()
End Sub
Just set a new instance of Form2 before you load it.
Dim Form2 As New Form2
Form2 .Show()