Library tutorials & articles
Windows Forms and Controls
Inheriting a Form
Using code we can create a second instance of the original form and display
it so without further ado lets do so. Replace the MsgBox code in the cbAccept
button’s click event with the following: Dim newForm As frmMain = New frmMain()
Dim newTxt As TextBox = New TextBox()
newTxt.Location = New System.Drawing.Point(80, 80)
newTxt.Width = 100
newTxt.Text = "New TextBox"
newForm.Controls.Add(newTxt)
newForm.BackColor = System.Drawing.Color.Brown
newForm.cbAccept.Visible = False
newForm.lblEnterText.Left -= 15
newForm.lblEnterText.Width -= 30
newForm.txtEnteredText.Left -= 30
newForm.txtEnteredText.Text = "New Instance of original
form"
newForm.txtEnteredText.Width = "New Instance of original
form".Length * 6
newForm.Show()
O.k. I’ve created a new instance of frmMain, created a brand new control and
added it to our new instance and then changed a few control properties. Note
the Point which is part of the System.Drawing namespace. This namespace is used
to draw almost everything on the interface. Also pay close attention to the method
used to add a control to a form. In this case I created a reference to a TextBox,
set some properties and used the Controls.Add method to add it to the form. Then
I changed some properties of the new instance of the form, backcolor, made the
Accept Button invisible and changed the position and width of the label and existing
textbox. This, I did using the new -= capabilities of VB.Net which saves me having
to write the property out twice. Finally I used the .Length property of a string
to set the width of the existing textbox. The Len function can still be used
as far as I know but since everything in .Net is now an object, including variables
and in this case an undeclared string, all now have methods and properties.
This is but a brief demonstration of the power of inheritance with forms. You
can go further, create a class that inherits your base form class, add new controls
and add new events and so on. For now though I’m going to finish with an examination
of the code for everything we have done.
Related articles
Related discussion
-
Error in VB code
by glib162002 (0 replies)
-
i have struck with my project in vb.net
by gangireddysaritha (1 replies)
-
Very slow inserts using SqlCommand.ExecuteNonQuery()
by porchelvi (1 replies)
-
Datagridview Setting datasource property of datagridviewcomboboxcell at run time
by sairfan1 (2 replies)
-
vb.net mp3 +g player
by novavb6 (1 replies)
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum)
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
Only 5 years after the last post eh :) Anyway because things may have changed I recommend you look at the microsoft docs on this - http://msdn.microsoft.com/en-us/library/ms229597(VS.80).aspx
It may not work with mdi children.
I use VB.Net (framework 1.0.3705) on Windows 2000, and I noticed the 'MinimumSize' property doesn't seem to have any effect on my forms (MDIChildren). Does that property really prevent users from resizing a form to a smaller size than the MinimumSize? Any other experiences?
Thank you,
This thread is for discussions of Windows Forms and Controls.