Windows Forms

Windows Forms is a feature of the .NET framework that provides a set of classes for building GUI applications. Windows Forms can be used by any .NET language.
A Windows Forms application consists of one or more windows called forms. These may be top-level windows, child windows or dialog boxes and an application may support many different forms.


Modal vs Modeless - be able to copy text to and from dialogs
Disable all UI commands based on permissons


System::Windows::Forms


Overloading Windows Forms Constructor
frmMyForm(string sname) : this()



SizeGrip

Unable to resize on the sizes only from the corner ??


If you are a Visual Basic or C# programmer you can use the designers to help create the user interface. These designers generate the code for you.
In .NET you do not create a form, you extend the functionality of a generic windows form.
Each form has its own co-ordinate system. The coordinate systems starting point or origin is the upper left corner of a form. The default coordinate system is made up of pixels.
You can change the size of the grid displayed on yoru form by choosing (Tools > Options)(Windows Forms Designer Folder Grid size).
Forms are always constructed at run-time by executing code and the designers simply take out what you do on the screen and generate the code underneath.
There is currently no designer for Microsoft Visual C++.NET included in the first release of Visual Studio.NET
When you create a Windows Form you will find that a container is created for you
It is important that the component does not rely on constructor parameters; instead your component should be initialised though properties.
You can draw on forms using the classes in System::Drawing namespace


Setting the focus to a particular control

Use the Form_Activate event.
No controls are visible when the form is loaded so you cannot use the Form_Load event.




Cancel / Close Button

The Escape key doesn't work if this button is not visible
If you have a form and don't want to show the Cancel button but keep the escape functionality change the Top property off the button so it is off the form when the form is displayed.




Example - C#

Private void Form1_Load(object sender, system.EventArgs e) 
{
   System.Data.SqlClient SqlConnection cn = new System.Data.SqlClient.SqlConnection();

   SqlDataAdapter da = new SqlDataAdapter()
   Dataset ds = new DataSet();

   cn.Open();



   da.SelectCommand = new SqlCommand(sQuery, cn);

   da.Fill(
}

Form overrides dispose to clean up

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 

End Sub


The Form class provides a number of properties and methods to help in the development of MDI applications


Example - C#

Public Class MDIMainForm : System.Windows.Forms.Form 
{
   Public MDIMainForm()
   {
      this.Text = "Caption title";
      this.IsMdiContainer = True;
   }

   Public static void Main(String{ ] args)
   {
      Application.Run(new MDIMainform() );
   }
}

Form a = new Form(); 
a.MDIParent = this;
a.show();

Form b = new Form();
b.MDIParent = this
b.show();



AutoScaleBaseSize - this property sets the base size used at display time to compute the sclaing factor for the form.
ClientSize - this property sets the size of the forms clients area which is the size of the form excluding borders and title bar.
Both of these are automatically defined for you in the designer.



Calling Events Directly

Call Me.chbCheckBox.CheckedChanged(Me, New System.EventArgs) 


Additional constants

The following constants should be added to the AppConstants module.

'used for a reference to the main form
Public gfrmMAINFORM As frmMain

'used as a way to temporarily stop the form events from firing
Public gbfrmMainForm_FireEvents As Boolean

'used to define the default positon on the form
Public Const giDEFAULT_FORMWIDTH As Integer = 800
Public Const giDEFAULT_FORMHEIGHT As Integer = 776

Private Sub frmMain_Load(ByVal sender As System.Object, _ 
                         ByVal e As System.EventArgs) _
                         Handles MyBase.Load

   gsVersion = clsAssembly.GetVersion(System.Reflection.Assembly.GetExecutingAssembly)
   gbfrmMainForm_FireEvents = True
   gfrmMERGEFIELDS = Me
   Me.Text = gsFORMS_TITLE

'etc etc

End Sub

Private Sub frmMain_Close() 

   Try
      If clsError.ErrorFlag() = True Then Exit Sub

      If clsRegistry.App_FormSaveLocationRead = True Then
         Call clsAssembly.FormLocationSave(Me.DesktopBounds)
      End If

End Sub

This is always called before the form is dismissed.


Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 
'this can also be used to make sure there is only ever once instance of a modeless form
   gfrmMERGEFIELDS = Nothing

End Sub

#Region " Main Menu Commands " 
'************************************************************************************
'FILE menu
'************************************************************************************
Private Sub menFileExit_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles menFileExit.Click

   If gbfrmMainForm_FireEvents = False Then Exit Sub

   Application.Exit()
'will call frmMain_Close before it exits

End Sub

'************************************************************************************
#End Region

Template Code


Private Const m_sFORMNAME As String = "frmName" 
Private Const m_sFORMTITLE As String = "Descriptive Name"
Private m_iFormLocation_x As Integer
Private m_iFormLocation_y As Integer
Private m_sCurrentValues As String
 
'**************************************************************************************
Public Sub New(ByVal iApplicationWindow_Center_x As Integer, _
                         ByVal iApplicationWindow_Center_y As Integer)
   InitializeComponent()
 
  m_iFormLocation_x = CType(iApplicationWindow_Center_x - (Me.Width / 2), Integer)
  m_iFormLocation_y = CType(iApplicationWindow_Center_y - (Me.Height / 2), Integer)
 
   Me.MinimumSize = Me.Size
 
'used for the progress dialog
   gWindowsForm = Me
 
   Me.m_sCurrentValues = ""
 
End Sub
'**************************************************************************************
Public Sub frmName_Load() Handles MyBase.Load
   garDisplayedWindowsForms.Add(m_sFORMNAME)
 
   If (m_iFormLocation_x > 0) And (m_iFormLocation_y > 0) Then
      Me.Location = New System.Drawing.Point(m_iFormLocation_x, m_iFormLocation_y)
   End If
   Me.Text = My.Settings.APP_WINFORMS_PREFIX & m_sFORMTITLE & _
             " [" & m_objCurrentDocument.Name & "]"
 
   Call Me.Form_Load()
 
End Sub
'**************************************************************************************
Private Function Validation_BeforeLoading() As Boolean
   If (condition) Then
      modSpecificMessages.Message_
      Return False
      Exit Function
   End If
 
   Return True
End Function
'**************************************************************************************
Private Sub Form_Load()
   If (Me.Validation_BeforeLoading() = False) Then
      Call Me.Close
      Exit Sub
   End If
 
'call this after all the data has been returned
   m_sCurrentValues = Me.GetValues_Current
End Sub
'**************************************************************************************
Private Sub frmName_FormClosing() Handles Me.FormClosing
   Dim objresult As System.Windows.Forms.DialogResult
   Dim sLatestValues As String
 
   sLatestValues = Me.GetValues_Latest
   If (m_sCurrentValues = sLatestValues) Then
 
      Call Me.Form_Close()
      Me.Dispose()
      Exit Sub
   Else
      objresult = modMessages.WindowsForm_ClosingQuestionSaveYourChanges(m_sFORMTITLE)
      If (objresult = Windows.Forms.DialogResult.Cancel) Then
         e.Cancel = True
         Exit Sub
      End If
      If (objresult = Windows.Forms.DialogResult.Yes) Then
         If (Me.Form_Save() = False) Then
            e.Cancel = True
            Exit Sub
         End If
         Call Me.Form_Close()
         Me.Dispose()
         Exit Sub
      End If
      If (objresult = Windows.Forms.DialogResult.No) Then
         Call Me.Form_Close()
         Me.Dispose()
         Exit Sub
      End If
   End If
End Sub
'**************************************************************************************
Private Sub Form_Close()
   My.Settings.USER_ERROR_OCCURRED = False

   My.Settings.USER_OPTIONS_TITLEAUTHOR_LASTTABINDEX = Me.TabControl1.SelectedIndex
   My.Settings.Save()
 
   If Not (m_BackgroundWorker_1 Is Nothing) Then
      If (m_BackgroundWorker_1.IsBusy = True) Then
         m_BackgroundWorker_1.CancelAsync()
      End If
   End If
 
   If Not (gFormNativeWindow Is Nothing) Then
      gFormNativeWindow.ReleaseHandle()
   End If
 
   Me.m_objWebServiceReference = Nothing
 
   garDisplayedWindowsForms.Remove(m_sFORMNAME)
End Sub
'**************************************************************************************
Private Function Form_Save() As Boolean
   My.Settings.USER_ERROR_OCCURRED = False

   If (Me.Validation_BeforeSaving() = False) Then
      Return False
      Exit Function
   End If
   
'update the current values to match the latest values
   Me.m_sCurrentValues = Me.GetValues_Current
 
   Return True
 
End Function
'**************************************************************************************
Private Function Validation_BeforeSaving() As Boolean
   If (condition) Then
      modSpecificMessages.Message_
      Return False
      Exit Function
   End If
 
   Return True
End Function
'**************************************************************************************
Private Sub btnSave_Click() Handles btnSave.Click
   Call Me.Form_Save()
End Sub
'**************************************************************************************
Private Sub btnSaveClose_Click() Handles btnSaveClose.Click
 
   Me.btnSaveClose.Enabled = False
   If (Me.Form_Save() = True) Then
      Me.Close
   Else
      Me.btnSaveClose.Enabled = True
   End If
End Sub
'**************************************************************************************
Private Sub btnCancel_Click() Handles btnCancel.Click
   Me.Close()
End Sub
'**************************************************************************************
Public Function GetValues_Current() As String
   Dim sconcat As String = ""
 
'typically from document variables or within the file
 
   Return sconcat
End Function
'**************************************************************************************
Public Function GetValues_Latest() As String
   Dim sconcat As String = ""
   Dim icount As Integer
 
   For icount = 0 To (Me.dgrFullList.Rows.Count - 1)
      sconcat &= Me.dgrFullList.Rows(icount).Cells(5).Value.ToString
   Next icount
 
'if objects are not available or services have not returned data yet then refer back to document variables for values
 
   Return sconcat
End Function
'**************************************************************************************



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext