Diff for "VbDotNetExample" - Methods
location: Diff for "VbDotNetExample"
Differences between revisions 15 and 16
Revision 15 as of 2007-09-12 11:52:18
Size: 6190
Editor: pc0040
Revision 16 as of 2007-09-12 11:54:46
Size: 6166
Editor: pc0040
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
===== ExpSettings ===== === ExpSettings ===
Line 18: Line 18:
===== ExpDisp ===== === ExpDisp ===
Line 38: Line 38:
===== ExpTimingAndReponses ===== === ExpTimingAndReponses ===
Line 43: Line 43:
===== ExpTrial & AttentionTrial ===== === ExpTrial & AttentionTrial ===
Line 46: Line 46:
===== ExpTrialList & AttentionTrialList ===== === ExpTrialList & AttentionTrialList ===
Line 69: Line 69:
===== frmStart ===== === frmStart ===

A sample experiment in VB.net


The example is here attachment:ExperimentTemplate.zip

This is an attention monitoring experiment. It may be used in the scanner, or out of the scanner, with a simple switch of a parameter. It contains a colleciton of modules (starting Exp...) that contain functions that should be generally useful for many experiments.

This program is provided as an illustration only. You should always check your code carefully.

Program components


General experimental settings. Most useful is probably

Public FullScreen As Boolean == True

By default, the experiment acquires the whole screen, to give accurate timing, and prevent other programs from being displayed. However, this can be irritating when debugging, so set this to False when programming.


Contains various subroutines to help you use DirectX for display.


Sets up screen, in full screen or windowed mode. Make sure the number of colours (third argument in

             DXDevice.SetDisplayMode(ES.ScreenWidth, ES.ScreenHeight, 32, 0, False)

is set to the same as the colour depth that windows was started in, if you use .FROMARGB anywhere to convert RGB values to colours.


Clears the screen


Draws a fixation cross


To ensure that graphics are displayed tidily, drawing is done on an "off screen surface" and then flipped to the display. This command flips the display. If in fullscreen mode, a DirectX flip command is used, which will be executed by the graphics card, and give high performance. In the testing windowed mode, a copy command is used to emulate the action of the flip.


Various routines for getting accurate timing, synchronising with the scanner (if used), checking button boxes and the keyboard.

Set USESCANNER and USEBUTTONBOX to True to use the software on the scanner or mimic machines.

ExpTrial & AttentionTrial

The ExpTrial class that represents a single trial, storing the parameters of the trial. This class would usually be used as a base for your own class, which more fully implements the particular experiment in question. Here, this is done in AttentionTrial, which inherits ExpTrial, but extends the "Run" method to display an attention monitoring trial.

ExpTrialList & AttentionTrialList

ExpTrialList stores a list of trials, and knows how to randomize it (or individual parameters), run all of the trials, and dump various things to the output file. AttentionTrialList is a specific implementation for this experiment, that also prepares the grating stimuli. Nevertheless, the example code of AttentionTrialList contains several snippets useful for various experiments:

Useful code for loading bitmaps is comprised of code necessary to find the files on disk:

    Function getstimdir()
        Return (Application.StartupPath & "\stim")
    End Function

    Function getfn(ByVal k, ByVal j) As String
        Return (getstimdir() & "\grating_" & k & "_" & j & ".bmp")
    End Function

These two functions specify the path to the various images necessary for the experiment. Using variables (here 'k' and 'j') to define the specific file (e.g. 'grating_1_2.bmp') allows concise description of each image. Additionally, initial loading of all the necessary images before the experiment can aid performance:

    For k As Integer = 0 To 1
        For j As Integer = 0 To numbitmap
            b(k, j) = New Bitmap(getfn(k, j))

Here, 'b(k, j)' becomes a stored image that can be recalled in the experimental trial.


This is the main form. The code behind the "Start" button actually runs the experiment. This code contains the definitions of the trials:

Dim ATTENDLIST() = {0, 3, 12, 15}

Modify these definitions to describe the design of your experiment. You may choose to represent each condition by a number of definitions (e.g. a trial may be defined by CONGRUENCY, SOA, DELAY and PRIMEPRESENCE).

Then a list of trials is created:

TL = New AttentionTrialList(NUMCONDS * NUMSUBBLOCKS, txtOutputFilename.Text, txtSubjectCode.Text)

Here, you have three arguments separated by commas: (NewNumTrials, NewOutputFilename, NewSubjectCode); dictate the value of the first argument in relation to the definitions described above.

Finally, the list is populated by the different parameters relevant to each experimental trial, again dependent on the definitions above:

For i = 0 To NUMSUBBLOCKS - 1
            For j = 0 To NUMCONDS - 1
                TL.TrialList(ind).AddParameter("Subblock", i)
                TL.TrialList(ind).AddParameter("MonitorDur", ((MAXBLOCKDUR - MINBLOCKDUR) * i / (NUMSUBBLOCKS - 1.0)) + MINBLOCKDUR)
                TL.TrialList(ind).AddParameter("AttendTo", ATTENDLIST(j))
                ind = ind + 1

This nested function ensures all trials are created different. Expand/modify this depending on the structuring of your trials.

Then the trials are randomized with either or both of two commands:


The first one randomizes a single parameter of the given name independently of the others (change as appropriate) and the second randomises the order of all trials either preserving (argument set to 'True') or not ('False') the Subblocks (see above). Lastly, before running the experiment we need to initialise the devices necessary for our experiment, such as our keyboard:


Finally, we run our experiment


and terminate the display afterwards.


Author & Acknowledgements

Page and example program created by Rhodri Cusack. Thanks to Daniel Mitchell & Alejandro Vicente-Grabovetsky who contributed parts to this code.

None: VbDotNetExample (last edited 2013-03-08 10:28:25 by localhost)