Back to Gambas

Introduction edit

A grid has rows and columns. They have width and height. You can fill a grid with text, numbers or even a picture.

Example 1 edit

You need a new form to get the program going. On this form you place a gridview , which you can find in your toolbox. There should be a picture x.png in your directory. Otherwise it wont be shown. There wont be an error message, when it is not found.

 STATIC PUBLIC SUB Main()
  hForm AS Fmain
  hForm = NEW Fmain
  hForm.show
 END

 PUBLIC SUB _new()
  GridView1.Columns.Count = 4
  GridView1.Rows.Count = 3
  GridView1.Columns.Width = 52
  GridView1.Rows[1].Height = 52
  GridView1[0,0].Text = "0,0"
  GridView1[0,0].Alignment = 4
  GridView1[1,1].Text = "1,1"
  GridView1[0,1].Text = "0,1"
  GridView1[1,0].Picture = Picture["x.png"]
 END

Create a Grid without the Toolbox edit

The grid ist creatable. How it is done shows the little example.

 g AS Gridview 

 PUBLIC SUB _New()
   g = NEW Gridview(ME) AS "Gridview1"
   g.show
   g.Columns.Count = 4
   g.Rows.Count = 3
   g.Columns.Width = 52
   g.Rows[1].Height = 52
 END

You just need a empty form to get the program going.

Properties of the grid

BackColor  Background  Border  ClientH  ClientHeight  ClientW  ClientWidth  Column  Columns  Current  Cursor 
Design  Drop  Enabled  Expand  Font  ForeColor  Foreground  Grid  H  Handle  Height  Id  Left  Mouse  Parent  Row 
Rows  ScreenX  ScreenY  ScrollBar  Tag  ToolTip  Top  Visible  W  Width  Window  X  Y  

  Methods

Clear  Delete  Drag  Grab  Hide  Lower  Move  Raise  Refresh  Resize  SetFocus  Show  

  Events  

Activate  Click  DblClick  Drag  DragMove  Drop  Enter  GotFocus  KeyPress  KeyRelease  Leave  LostFocus  
Menu  MouseDown  MouseMove  MouseUp  MouseWheel  Scroll 

Fill the grid with some values edit

You have a list of values and you want to have them in a grid. The example shows you a solution. Still not very nice and with some German words in it.

You need the following controls:

  • 1 Textarea
  • 1 Gridview
  • 2 Commandbuttons

The code

 PUBLIC SUB Button1_Click()
   textarea1.Text = "114"
   textarea1.Text = textarea1.Text & Chr(10) & "135"
   textarea1.Text = textarea1.Text & Chr(10) & "104"
   textarea1.Text = textarea1.Text & Chr(10) & "118"
   textarea1.Text = textarea1.Text & Chr(10) & "125"
   textarea1.Text = textarea1.Text & Chr(10) & "121"
   textarea1.Text = textarea1.Text & Chr(10) & "122"
   textarea1.Text = textarea1.Text & Chr(10) & "96"
   textarea1.Text = textarea1.Text & Chr(10) & "118"
   textarea1.Text = textarea1.Text & Chr(10) & "120"
   textarea1.Text = textarea1.Text & Chr(10) & "112"
   textarea1.Text = textarea1.Text & Chr(10) & "127"
   textarea1.Text = textarea1.Text & Chr(10) & "122"
   textarea1.Text = textarea1.Text & Chr(10) & "128"
   textarea1.Text = textarea1.Text & Chr(10) & "120"
 END

 PUBLIC SUB _new()
   GridView1.Columns.Count = 2
   GridView1.Rows.Count = 15
   GridView1.Columns.Width = 72
 END

 PUBLIC SUB Button2_Click()
   DIM text AS String
   DIM Liste AS String[]
   DIM Einzelwert AS String
   DIM x AS Integer

   x = 0
   text = textarea1.Text
   Liste = Split(text,Chr(10))

   FOR EACH Einzelwert IN Liste
      GridView1[x,1].Text = Einzelwert
      GridView1[x,0].Text = x
      x = x + 1
   NEXT

   PRINT liste.Length
 END

When you push button1, then the textarea is filled with values. When you push button2, they will be placed in the grid.

You can improve this program if you want. It looks a little newbee like.