how to call back to original form - vb.net

I have 2 forms. Form1 and Form2.
When Form2 is closing, how do I make Form2 to inform Form1 that Form2 is closed.
Do I need to use Delegate, if yes, how?
thanks.

You will need to first reference Form2 from Form1, then you can use one of the close events on the other form. Either the Form.OnClosed or Form.OnClosing events.
' On form1
Private Sub Form2_Closing(ByVal sender As Object, _
ByVal e As EventArgs) Handles Form2.OnClosing
' Form2 informed us that it is closing. Do stuff!
End Sub

You can just create a custom event in Form2 and then make Form1 subscribe to it. Here's a nice and easy tutorial article for this:
http://www.codeproject.com/KB/vb/StepByStepEventsInVBNET.aspx
Edit: If you just need the event when it's closing, I'd go with Oded's solution since that's easier.

Related

Visual Basic:Closing and Opening Forms?

I am making a program in which you can press a button and it makes a new form but I have a little bit of a problem.
Because I have Form1 and Form2. When I press the button on Form1 it shows Form2 and it should just close Form1.
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Form2.Show()
Me.Close()
What actually happens is that it closes Form1 and Form2 even if I said Me.Close().
Is there a fix to it or did I just do it wrong somehow?
I bet Form1 is your startup form. If not sure sure check that in Project properties.
The moment you close Form1, your application terminates altogether. So in simple words you can't close Form1 and show another form, at least not with 2 lines of code.
What you can do is hide Form1 and Show Form2.
Form2.Show()
Me.Hide()
Now when you close Form2, make sure you either unhide Form1 (so that usercan manually close it) or automatically close Form1 from Form2's FormClosing event, else your process will be alive in the background, a ghost :)
So in your Form2, add the FormClosing event handler and then inside that close Form1
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub

Accessing text box on Form1 from Form2

I am new to vb.net so forgive me if this is an easy question.
I have a created a class library project that houses two windows forms, Form1 and Form2. The main class library has the event to open Form1. A button on Form1 launches Form2. The bulk of the code is in Form1, which I don't want to change if I can help it.
What I am trying to do, is access a sub that is on Form1 from Form2. That sub is changing the value of a text box on Form 1. I don't get any errors when I compile the project, however, nothing happens.
Here is an example
Form1:
Public Sub test()
Me.Panel1.Controls("Textbox1").Text = "Test"
End Sub
Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainForm As New Form1
Me.Close()
MainForm.test()
End Sub
Don't get too caught up on how I built it out, I have tried about 20 different things and this is where I am at now.
I have tried defining Form1 in the sub test(). I have tried setting sub test() to shared. I have tried closing Form2 and activating Form1. I have tried changing the modifiers property on the text box to public. I have tried making Form1 the parent and Form2 a child (I honestly don't understand MDI very much). All these results end up in a project that will compile but wont give me any results. My code accesses the sub just fine, it wont access the text box's text property.
Any suggestions will help. I am trying to access the text boxes in a way that I can loop through all of them. For example: Me.Panel.Controls("Textbox" & i).Text = "Something". Also I would like to keep the sub in the class for Form1 if I can.
Any suggestions would be great!
You are creating a brand new Form1 in Form2, that is the problem.
Just use :
Call Form1.test()
By the way, I think this code in the sub is an easier way to set the text:
Panel1.TextBox1.Text = "Test"

ShowDialog() in vb.net not working properly

I'm not sure if this question has been already posted or not but since I couldn't find the answer I'm looking for so I'll ask anyways.
When I use ShowDialog() to call another form from the current form it just displays the name of the controls the first time it opens, if I close it and open it again then it displays at it should have. For example if I write a simple code as follows
Form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Display("Message")
End Sub
End Class
Form2
Public Class Form2
Public Sub Display(ByVal msg As String)
Me.ShowDialog(Parent)
Label1.Text = msg
End Sub
End Class
When I click the button the first time all I get is "Label1" instead of "Message" displayed on the called form but when I click on the button a second time I get the desired output.
I tried the same thing with Show() and didn't have any such problem. But I need the called form to be modal (like a MessageBox), so is there any other way to do it or can someone tell me the right way to use ShowDialog() ?
I'm new to vb so please keep it simple.
Thanks
ShowDialog is a blocking call. You need to set everything before you call it.

How can I make a custom .Net Windows Form that acts like a MessageBox?

How can I make a form that behaves like a message box?
For example, I have 2 forms named form1 and form2.
form1 contains my basic information (Name, gender, address and 1 button (educational background)).
form2 contains my educational background (school, school_add, button(OK) and button (Cancel)).
If I open form1 and I click the educational background button, then form2 will appear. What I want is that if the user tries to click form1 while form2 is still open, then form2 will blink and the user cannot manipulate form1. I want to require the user to click the Cancel or the OK button on form2 before returning to form1, just like how a message box prevents you from using the form behind it.
How can I do this?
What you are looking for is called a Modal form.
It's very easy to do: just call .ShowDialog(Me) instead of .Show().
' Form1 button handler
Private Sub buttonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.Click
Dim f As New Form2()
f.ShowDialog(Me)
End Sub
See also http://msdn.microsoft.com/en-us/library/39wcs2dh(v=vs.100).aspx
Load your form using .ShowDialog instead of .Show
Form2.ShowDialog

Next button in VB.net

how do i close my form after opening the next form (VB.net Windows form) like the Next button
I tried Form2.show() it shows form2 but does not close the form1 and if i type me.close(), the entire project stops
If you just want Form2 to be Visible you can hide Form1 when you show Form2, then show it Form1 again when you close Form2. What is happening is that once you close Form1 your program will exit see below edit.
Something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
AddHandler frm2.FormClosed, AddressOf Form2Closing
frm2.Show()
Me.Hide()
End Sub
Private Sub Form2Closing(sender As Object, e As FormClosedEventArgs)
Me.Show()
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf Form2Closing
End Sub
If you just are wanting to Close Form1 and not go back to it once Form2 is open, you can change your project settings from When startup form closes(which is the default) to When last form closes then you can close your first form without closing your application.
You have to specify what to close:
Form1.Close()
You better not close your form after opening another one from it, unless you want that other one also closed. Otherwise it will cause ownership and visibility side effects, which you really don't want to deal with in the long run.
This is exactly why me.close() on your main form stops your project. You just have consider paradigms Microsoft put into a Winforms application. If you don't, you are guaranteed to get in trouble.
Instead, a wizard control is what you are probably looking for.