logo  VB.Net Forms
This Chapter
Forms
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
The functions on this page manipulate Form properties to perform programming tasks. It is assumed that the functions are contained within the code for a form. When a functions is used to change the properties of a form which does not contain the function, you will need to replace every occurrence of "Me." to the required formName.
Fix Size Of Form
Do not allow the user to change the size of a form

Private Sub PreventResize()
 Me.MinimumSize = Me.Size ' prevent form from getting smaller
 Me.MaximumSize = Me.Size ' prevent form from getting smaller
 Me.MinimizeBox = False ' Remove the minimise icon from the form header
 Me.MaximizeBox = False ' Remove the maximise icon form the form header
 Me.FormBorderStyle = FormBorderStyle.FixedSingle ' change form border to indiacte it is not resizeable
End Sub
  
Show a "Help" button in Title Bar of the Form
The help button defaults to a question mark ? Vb.Net will not display minimize/maximize and help buttons simultaniously! You must remove the minimize and maximise butttons when trying to display the help button.

Private Sub ShowHelpButton()
 Me.MaximizeBox = False
 Me.MinimizeBox = False
 Me.HelpButton = True
End Sub
  
Change the Icon Displayed in Form Title Bar
The icon can be any suitably sized Icon file.

Private Sub ChangeFormIcon(icon1 As Icon)
 Me.Icon = icon1
End Sub
  

Private Sub DisplayFormIconInTitleBar(tf As Boolean)
 Me.ShowIcon = tf
End Sub
  
Remove the X Form-close Button from TitleBar
Just because you can do this, it does not mean you should do it! The user should always be allowed to close a program! If you have essential code that must finish before closing, you should accept the X click, then use the FormClosing event to trigger any essential form-closing code.

Private Sub ShowFormCloseButton(tf As Boolean)
 Me.ControlBox = tf
End Sub
  
Make Entire Form Parially Transparent
Transparancy must be a number between 0 and 1. Transparency = 0 makes the form totally transparent. (Form invisible because you can see everything that should be hidden behind it. Transparancy = 1 makes the form totally opaque (you cannot see anything behind the form)

Private Sub FormTransparancy(Transparancy As Double)
 Me.Opacity = Transparancy
End Sub
  
Make Part of a Form Transparent
You will need to "identify" which areas of a form should become transparent before this function can work. Select a colour that is not used on the existing form. Create a background image on the form and edit the image to ensure teh selected colour covers all sections which should be transparent.

Private Sub MakePartOfFormTransparent(TransparentColour As Color)
 Me.TransparencyKey = TransparentColor
End Sub
  
Remove Header and Footer from a Form
Removing the title bar will conceal the minimize, maximize and form-close(X) button. You will need to provide the user with an alternative method to close the form. (e.g. create a buttton on the main part of form which calls Me.Close()

Private Sub RemoveTitleBar()
 Me.FormBorderStyle = FormBorderStyle.None
End Sub
  
You can reinstate the title bar by using a differnt FormBorderStyle e.g.
   Me.FormBorderStyle = FormBorderStyle.FixedSingle
  
or
   Me.FormBorderStyle = FormBorderStyle.Sizable
  
Make Form Fill Entire Screen
This function allows a form to completly fill the screen, covering the windows bar at bottom of screen etc. It could be used for screen-saver or apps that need the extra space.

Private Sub FormFullScreen()
 Me.TopMost = True
 Me.FormBorderStyle = FormBorderStyle.None
 Me.WindowState = FormWindowState.Maximized
End Sub
  
Show Form in Windows Task Bar

Private Sub ShowInTaskBar(tf As Boolean)
 Me.ShowInTaskbar = tf
End Sub
  
Is Program Already Running?
Whilst Windows can handle mutilple copies of the same program running simutaneously, there are circumstances when this would be undesireable. This function returns TRUE when it detects that a program "with the same name" is already running. It is possible for two different apps to have the same name but the use of an unusual program name could avoid the issue.

Private Function IsAlreadyRunning() As Boolean
 Dim ModNam As String = Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName
 Dim ProNam As String = System.IO.Path.GetFileNameWithoutExtension(ModNam)
 Return (System.Diagnostics.Process.GetProcessesByName(ProNam).Length > 1)
End Function
  
If you add the following code to the start of the ".Load" event of your main form, it will close duplicate occurences.

If IsAlreadyRunning() Then
 Environment.Exit(0)
End If
  
You sometimes see
   Application.Exit
  
used in code examples, however, Environment.Exit(0) forces an immediate exit. (Under certain circumstances, a program can continue running lines of code after encountering the Application.Exit instruction.)

DigitalDan.co.uk