Library tutorials & articles

Windows Forms and Controls

The Complete Form Class (ctd.)

Finally we have our event handlers and we close the class.

Private Sub cbAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAccept.Click
       'MsgBox("Changes Applied!", MsgBoxStyle.OKOnly And MsgBoxStyle.Exclamation)
       Dim newForm As frmMain = New frmMain()
       Dim newTxt As System.Windows.Forms.TextBox = New System.Windows.Forms.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()
   End Sub

Private Sub cbCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCancel.Click
       Me.Close()
   End Sub
End Class

It’s well worth examining the code carefully because you’ll garner a very good understanding of how VB.Net works. One thing to note when compiling is that most declarations in the code I compiled contain full reference to the namespace i.e. Dim newTxt As System.Windows.Forms.TextBox = New System.Windows.Forms.TextBox() - You don’t have to do this as long as you have the references at the start of the file i.e. the Imports System.Windows.Form statement means I could leave the above code snippet as Dim newTxt as TextBox = New TextBox if I desired. You just have to make sure you reference all namespaces you use. The 3 used in this example are the basic references you will always need for a Windows Form, the Drawing and Forms as well as the basic System namespace.

Comments

  1. 01 Aug 2008 at 19:22

     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

  2. 22 Oct 2003 at 16:26

    It may not work with mdi children.

  3. 11 Jul 2003 at 03:26

    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,

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Windows Forms and Controls.

Leave a comment

Sign in or Join us (it's free).

Brian O'Connell Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web applicat...
AddThis

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

Want to stay in touch with what's going on? Follow us on twitter!