Odds and Ends Component Tray Menu and contextmenu Splash Screen
Examples in this show Mainmenu Context menu Errorprovider Splashscreen Cellular automata
The component tray contains: Timer Errorhandlers Print capabilities DialogBoxes Menus Context menus
Adding a main menu Most apps nowadays have menus – MS powerpoint and the VB IDE both do.
Add Mainmenu to component tray from toolbox
Selecting the mainmenu control allows you to begin editing the menu
and editing…and editing
add submenus to a menu item by filling in the little box that appears below or next to it.
As with other controls, it is a good idea to rename menu items from MenuItem1,…to mnuFile, …
shortcut keys You can add shortcut keys to a menu item by selecting shortcut in the properties for this item. Select the shortcut you wish to associate with the menu item.
Ctrl-s for save
More about menus Menu items display their submenus automatically if they have them. You add itemclick subs to add functionality to menu items that have no submenu.
Save clicked event handler Private Sub mnufilesave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnufilesave.Click MessageBox.Show("save selected", "Menu", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Sub
Clicking save in debug
Context menu A context menu is added (to the form component tray) from the toolbox. When you right-click a control information can be displayed.
Context menu
Add context menu to a control in properties
Add to context menu as you would to a regular menu
Add sub code to context
Context menu popup code Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup MessageBox.Show("context", "context", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Sub
Menu example exe In p:\vs\menu
Handling errors Handling errors with an errorprovider It is added to the component tray
Handling errors
Error provider An errorprovider is added to the component tray In this project the “causes validation” property of the two textboxes are both set to true Just the validation code was written – no other processing
Build error provider when form is loaded Private Sub frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load errorhandler = New ErrorProvider() End Sub
Corrected one error, now have another
Another ss entry error
Finally: Corrected ss num error
Two event handlers provided – the validation for the textboxes Private Sub txtint_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtint.Validating Dim int As Integer errorhandler.SetError(txtint, "")’ clear any old errors Try int = Integer.Parse(txtint.Text) Catch ex As Exception errorhandler.SetError(txtint, "field must be int") e.Cancel = True ‘cancel event End Try End Sub
Part of ss validation Private Sub txtss_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtss.Validating ' Me.Refresh() errorhandler.SetError(txtss, "") Dim ss As String ss = txtss.Text If ss = "" Then errorhandler.SetError(txtss, "field required") e.Cancel = True ElseIf ss.Length <> 11 Then e.Cancel = True errorhandler.SetError(txtss, "field must contain 11 chars") ElseIf ss.Substring(3, 1) <> "-" Or ss.Substring(6, 1) <> "-" Then ‘’’’’there’s more End If End sub
Errorprovider example In p:\vsp\errorprovider\bin\... There is more to do with this example… like enable navigation and exit without correcting the error.
Splash screen: comes up briefly with some info when a form is loaded
Splash screens: Create a main form with just a label and exit button Add the exit code (me.close()) Add a second form to the project called frmSplash
Splash screens: On frmsplash put: Information about the application, copyright, programmer, date, pictures, whatever.
Splash screens: Add a timer with interval 5000 to the frmspash. In the timertick sub put –Me.Close() ‘close form after 5 sec Be sure to start the timer in the frmsplash load sub.
Splash screens: set topmost property to true
set frmsplash formborderstyle to fixedtool (so it can’t be resized)
Back in frmmain Add formload code to create an instance of the frmsplash. Recall this code is: Dim x as frmsplash x.show() When frmsplash loads it will start its timer. Exe in p:\vs\splash\splash.exe
Cellular automata: in p drive vb folder
Cellular automata There are many examples of uses of cellular automata, from biological modeling to designing fabric patterns. I cover only a little bit about them here. Discussion of 2-d automata Discussion of 1-d automata
One dimensional automata The original values are stored in an array. These might be generated randomly, or come from measurements. Display (in VB) might be in a panel. The “first generation” consists of a single set of rectangular colors. Each color represents an integer in the array. The next generation is computed from the first (usually) according to some formula for example: Newvalue(i)=(old(i-1)+old(i)+old(i+1)) mod maxcolors You’ll need two one dimensional arrays, and you’ll have to keep copying the new values back into the old array, and displaying.
Two dimensional automata Two d automata are displayed similarly to one-dimensional ones: each value represents a color. But each new generation is computed in two dimensions, often using some rule about how many neighbors a given cell has in the current generation.
Two dimensional automata A sketch of the code goes like this: 1.Assign initial values to a fairly large (like 100X100) array. 2.Loop a bunch of times or do on buttonclick: 3.Compute next generation values into a new array 4.Display the current generation 5.Copy next generation into current 6.endloop
Example..in p:\vb\twodcellularautomata.exe