跳轉到內容

Gambas/Grid

來自華夏公益教科書

返回 Gambas

網格有行和列。它們有寬度和高度。您可以用文字、數字甚至圖片填充網格。

您需要一個新窗體來執行程式。在這個窗體上,您放置一個網格檢視,您可以在工具箱中找到它。您的目錄中應該有一個名為 x.png 的圖片。否則,它將不會顯示。如果沒有找到,不會出現錯誤訊息。

 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

在沒有工具箱的情況下建立網格

[編輯 | 編輯原始碼]

網格是可以建立的。一個小例子展示了它是如何完成的。

 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

您只需要一個空窗體來執行程式。

網格的屬性

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  

  方法

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

  事件  

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

用一些值填充網格

[編輯 | 編輯原始碼]

您有一系列值,並且希望將它們放在網格中。該示例向您展示了一種解決方案。仍然不太好,並且包含一些德語單詞。

您需要以下控制元件

  • 1 個文字區域
  • 1 個網格檢視
  • 2 個命令按鈕

程式碼

 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

當您按下按鈕 1 時,文字區域將填充值。當您按下按鈕 2 時,它們將被放置在網格中。

如果您願意,可以改進此程式。它看起來有點像新手。

華夏公益教科書