How create a sub-forms in vb.net - vb.net

I will like to create 2 windows forms using vb.net
The 1st window forms is main and the second window forms is for sub-window forms and they appear at the same time when run the program
When i close the sub-windows forms it will not close my program, if i close the main window forms it will close my program.
How can i do that ?

When you open the second form, set the owner to the parent form and when you close the parent, it will force the child to close as well. I believe (at least it seems it did this at one time in VB history) that if you hide the parent, it will hide the child too.
If you are using SHOW to display you 'Sub-Form', pass the parent form to it like this:
Form2.show(Form1)

If you are trying to close the owner form when the child form is closed and (if the owner is the starting form of the entire program) end the program, then you will first want to do what Steve has answered to assign form1 as the owner of form2.
Then, to make the child form close the parent, use the following code in form2's FormClosed event:
Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Me.Owner.Dispose()
End Sub

Related

Open 2 MDI Parent after opening the main menu form

I have 3 forms
MDI parent for TCP client
MDI parent for serial
The main menu form
I want to set the main menu form as the startupform then I want the 2 MDI parent to be loaded on the background. I don't want to show them, but I want to load them so I can trigger the on_load event of those 2 mdi parent which is to create serial/client child and connect automatically.
I've tried, but it didn't work.
Private Sub frmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
frmTcpMDI.show
frmSerialMDI.show
End Sub
I even tried adding a timer then starting it upon the show event of my startup form, the tick event is to open my frmTcpMDI and frmSerialMDI, still, it is not working.
Okay basically, if I understand what you want, you need a way to load your forms but not display them.
You want to run some code when the Load event occurs. However, as stated here by Microsoft :
Occurs before the control becomes visible for the first time.
Which basically screws you up...
So there is a few workaround for that.
Option 1 : You decide to run your code in the Sub New()
If you put all the code you want to run in the Sub New() of your child forms, you don't need to call Form.Show(), you just need to create the forms, and your code is running. When you actually need to display the form you call the Show() method and it's done.
Option 2 : You can't run your code in the Sub New()
If, for some reason, you can't run your piece of code in the Sub New() method, you still can do it with the load event. Just hide the form after showing it... Looks silly but will work.
First notice, still from Microsoft :
The Load event occurs when the handle for the UserControl is created. In some circumstances, this can cause the Load event to occur more than one time. For example, the Load event occurs when the UserControl is loaded, and again if the handle is recreated.
Which mean you have to be carefull to run your code only once...
Then, from your parent :
Dim myNewForm = new frmTCPMdi()
myNewForm.Show()'will call the Load event
myNewForm.Hide()'will hide the form, so it is loaded but invisible...
Honnestly, I think Option 1 looks better, but sometimes and for some reason you can't always go the easy way...

Sharing Controls from multiple forms/classes

I'm currently stuck in how to share controls between two forms.
This is what i want to do:
- I have several forms that have their own function.
- Now i want to bring them all to gather on one mainform.
Normally i do this by creating the following code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f As New Form2
GroupBox1.Controls.Add(f.Get_Groupbox_controls)
End Sub
End Class
This add all the control containing in Form2.Groupbox1 and adds it to Form1.Groupbox1.
Normally this works just fine for me.
Where "Get_Groupbox_controls" is a function that returns Form2.Groupbox1
Or if this doesn't works i display the entire form2 over form1 with a transparancykey. (this isn't a need solution, you have to do a lot of location calculating, but it works)
However now i got a form that has a lot going on. For the mainform i only want to display one Container(groupbox) but when the user clicks "More setting.." then it has to show the entire form with all the control on it.
The problem is when i use the container.controls.add() function i get an exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created "
The Error is logical because the entire form isn't made yet. But in order for the form to work properly it has to update the "hidden" UI controls (labels/buttons/etc)
So the main question is, how can i "port" a selection of controls from Form1 to form2 and keep all the handlers etc.. on Form1
If you have two forms where you want to place identical looking/functioning GroupBox sections, you may want to consider creating a custom user control that contains the shared functionality and can be placed in each form. User controls can even be added to a form through the form designer in Visual Studio.

VB using me.close before opening new form

Hi Guys i'm new to Visual Basic Coding, and i can't seem to get where's my mistake on my coding, i'm trying to create a button that opens a new form while closing the current form.
i have two forms, form 1 is MainForm, form 2 is SearchForm
Whenever i use this code:
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
MainForm.Close()
SearchForm.Show()
End Sub
End Class
it will generate an error and says i need to replace MainForm.Close() into Me.Close()
When i Use this
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
Me.Close()
SearchForm.Show()
End Sub
End Class
It closes both Forms and it doesn't leave any Form Open. Kindly direct me to the proper path, thanks in advance.
You need to Hide the form rather than closing it. Since it's your main form, when it closes, the application exits.
Standard UI guidelines are to leave the main form open, and open search form on top of that. If you need to block the main form, while search criteria are selected, use .ShowDialog, instead of just .Show.
.NET WinForms programming pattern kind of implies that you never close your main form. If you deviate from this approach, you are guaranteed to encounter all sorts of layout and display issues. So please don't. You can .Hide the main form, if it needs to go to system tray or run in background.

Blocking interaction between child and main parent form vb

At the moment, I have a project I'm working on with a menu that opens a new window. Annoyingly, when the new window is opened, it just does everything the main parent window does that the new window is opened up from. I want to be able to have the new window be able to do it's own thing and not just share what ever the parent is doing. Is there anything I can do at a button's click to give this form some kind of attribute that disallows this?
You can use seperate form. the working of each form will depend on what you specified in that particular form.. You can use the MDI form. and call all child forms under the mdi form. the child form will open in seperate window under MDI form.
you can simply call the child form as
Private Sub MembersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MembersToolStripMenuItem.Click
Dim xfrm As New childForm
xfrm.MdiParent = Me
xfrm.Show()
End Sub

Form maximize on another form

I have an MDI application which contains many child forms
My problem is, on clicking a particular menu, I am opening a form with maximize window in the MDI form. This works fine.
Now if I open another form above the first one, and if i want the second form to be of normal size, i am unable to do it.
Second form also opens with maximized window similar to first one. I want the second form to be of normal small size.
I want to show second form normally and first form maximized.
How can i do that?
Private Sub TESTToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TESTToolStripMenuItem.Click
Dim f As New newCalendar2("UGHARANI")
f.Show()
f.MdiParent = Me
f.WindowState = FormWindowState.Maximized
End Sub
Okay, I think I understand what you're trying to accomplish: you want the first (data) form to be a kind of background to your MDI application and have the other forms display on top of it, right?
Well one way to do it might be to remove borders from the background form –FormBorderStyle = None– and fill-dock it in the MDI parent form. Although it would end up coming to the fore and hiding all your other forms if a user clicked anywhere on it. But if it doesn't require any user interaction you could always use its Activate event to send it back to the background, using Me.SendToBack().