看看我的是不是最简单:
for i=1 to n*n
me.Controls("label" + Trim(str(i))).Caption=str(i)
next i
这样便可以将所有的框上赋予1到n*n的值,然后你要指定哪一个框的值,即可以用
me.Controls("label" + Trim(str(i))).Caption=str(i)。
Dim EditRow As Integer, EditCol As Integer
Private Sub Command1_Click()
Dim I As Integer
Grid1.Rows = Val(Text1.Text)
Grid1.Cols = Val(Text1.Text)
Grid1.RowHeight(-1) = 600
Grid1.ColWidth(-1) = 600
For I = 0 To Grid1.Cols - 1
Grid1.ColAlignment(I) = 4 ' Center align.
Next I
Text2.Width = 600 - 10
Text2.Height = 600 - 10
End Sub
Private Sub Command2_Click()
Dim NAnswer As Double
Dim Sum1 As Double
Dim I As Integer, J As Integer, K As Integer
Text3.Text = ""
NAnswer = 0
Sum1 = 1
For I = 0 To Grid1.Cols - 1
K = I
For J = 0 To Grid1.Rows - 1
If K >= Grid1.Cols Then
K = 0
End If
Sum1 = Sum1 * Val(Grid1.TextMatrix(J, K))
K = K + 1
Next J
NAnswer = NAnswer + Sum1
Text3 = Text3 & "+" & Sum1
Sum1 = 1
Next I
Sum1 = 1
For I = 0 To Grid1.Cols - 1
K = I
For J = 0 To Grid1.Rows - 1
If K < 0 Then
K = Grid1.Cols - 1
End If
Sum1 = Sum1 * Val(Grid1.TextMatrix(J, K))
K = K - 1
Next J
NAnswer = NAnswer - Sum1
Text3 = Text3 & "-" & Sum1
Sum1 = 1
Next I
Text3 = Text3 & "=" & NAnswer
End Sub
Private Sub Grid1_DblClick()
EditRow = Grid1.Row
EditCol = Grid1.Col
Text2.Left = Grid1.Left + Grid1.CellLeft - 10
Text2.Top = Grid1.Top + Grid1.CellTop - 10
Text2.Visible = True
Text2.SetFocus
End Sub
Private Sub Text2_LostFocus()
Grid1.TextMatrix(EditRow, EditCol) = Text2.Text
Text2.Visible = False
End Sub
在窗体上动态显示N×N个输入数据用的文本框
Option Explicit
Public WithEvents TextBoxObj As TextBox
Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
Dim mNN As Integer
mNN = 4 '在Form中显示4x4TextBox
Dim TempTop As Integer
Dim TempLeft As Integer
TempLeft = 800
For j = 1 To mNN
TempTop = 300
TempLeft = TempLeft + 1070
For i = 1 To mNN
Set TextBoxObj = Controls.Add("VB.TextBox", "Text" & j & i)
With TextBoxObj
.Visible = True
.Height = 400
.Width = 900
.Text = "霸王丸" & j & i
TempTop = TempTop + 600
.Top = TempTop
.Left = TempLeft
End With
Next
Next
End Sub