使用动态数组控件出了问题了,程序算法不知道那里出错

Rubi 2004-10-19 08:40:28
我的目的是实现一排4个command1,一共四排的16个command1按钮,其中显示情况如下
1 2 3 +
4 5 6 -
7 8 9 *
0 . = /
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 16
Load Command1(i)
Command1(i).Left = Command1(0).Left + 500 * i
If i > 4 And i < 9 Then
Command1(i).Top = Command1(0).Top + 500
Command1(i).Left = Command1(0).Left + 500 * (i - 5)
ElseIf i >= 9 And i <= 12 Then
Command1(i).Top = Command1(0).Top + 1000
Command1(i).Left = Command1(0).Left + 500 * (i - 9)
ElseIf i >= 13 And i <= 16 Then
Command1(i).Top = Command1(0).Top + 1500
Command1(i).Left = Command1(0).Left + 500 * (i - 13)
End If
Next i

Command1(i).Visible = True
Select Case i
Case 4
Command1(4).Caption = "+"
Case 8
Command1(8).Caption = "-"
Case 12
Command1(12).Caption = "*"
Case 14
Command1(14).Caption = "."
Case 15
Command1(15).Caption = "="
Case 16
Command1(16).Caption = "/"
Case Else
Command1(i).Caption = Command1(0).Caption + i
End Select
End Sub
不知道这里那里有问题了阿
...全文
208 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
51365133 2004-10-19
  • 打赏
  • 举报
回复
northwolves 2004-10-19
  • 打赏
  • 举报
回复
Private Sub Form_click()
Dim i As Integer
On Error Resume Next
For i = 0 To 15
Load Command1(i)
Command1(i).Caption = Mid("123+456-789*0.=/", i + 1, 1)
Command1(i).Visible = True
Command1(i).Move (i Mod 4) * Me.Width \ 5, (i \ 4) * Me.Height \ 5, Me.Width \ 5, Me.Height \ 5
Next
End Sub
route2 2004-10-19
  • 打赏
  • 举报
回复
Private Sub Form_Load()

Dim i As Integer '循环变量
Dim nRow As Integer '计算按钮所处的行数
Dim sBtnCap As String '按钮的文本
sBtnCap = "123+456-789*0.=/"

For i = 1 To 16
Load Command1(i)
nRow = Int((i - 1) / 4)
Command1(i).Caption = Mid(sBtnCap, i, 1)
Command1(i).Left = Command1(0).Left + (i - nRow * 4) * Command1(0).Width
Command1(i).Top = Command1(0).Top + nRow * Command1(0).Height
Command1(i).Visible = True
Next i

End Sub

这样子就可以了捏
有些数值处理,自己推敲一下就明白了的捏
Rubi 2004-10-19
  • 打赏
  • 举报
回复
en,我知道我的错误了,是数组标为搞错了,还有自己的表达式上也有点问题,谢谢了
a达哥a 2004-10-19
  • 打赏
  • 举报
回复
Command1(0) Command1(1) Command1(2) Command1(3)
1 2 3 +
Command1(4) Command1(5) Command1(6) Command1(7)
4 5 6 -
Command1(8) Command1(9) Command1(10) Command1(11)
7 8 9 *
Command1(12) Command1(13) Command1(14) Command1(15)
0 . = /

看到了吗? Command1(8) =7 ! 不是一一对应的!
a达哥a 2004-10-19
  • 打赏
  • 举报
回复
首先, VB的默认下标是0, 你的循环加载了16个Command,所以你看到的是17个按钮
其次, 你的Command的Index和Caption不是一一对应的, 因为有"+ - * /"等字符穿插在里边, 举个例子吧:
Command1(5) 的 Caption不是 5 !
a达哥a 2004-10-19
  • 打赏
  • 举报
回复
正解:Option Explicit
Private Sub Form_Load()
Dim i As Integer
Command1(0).Caption = "1"
For i = 1 To 15
Load Command1(i) '加载按钮
Command1(i).Move Command1(0).Left + 500 * (i Mod 4), Command1(0).Top + 500 * (i \ 4)
Command1(i).Visible = True '显示按钮
Select Case i '同过选择判断来显示按钮上面的字符
Case 3
Command1(i).Caption = "+"
Case 7
Command1(i).Caption = "-"
Case 11
Command1(i).Caption = "*"
Case 13
Command1(i).Caption = "."
Case 14
Command1(i).Caption = "="
Case 15
Command1(i).Caption = "/"
Case Else '出了以上,其他的显示数字按照以下公司
Command1(i).Caption = 1 + i - (i \ 4)
End Select
Next i '这里进行i的循环
Command1(12).Caption = "0"
End Sub


Rubi 2004-10-19
  • 打赏
  • 举报
回复
为什么这样说呢,5以上的为什么就不是了阿
a达哥a 2004-10-19
  • 打赏
  • 举报
回复
问题就在你的:
Case Else'出了以上,其他的显示数字按照以下公司
Command1(i).Caption = Command1(0).Caption + i

这个公式是错的, 因为5以上的Command1(i)的Caption不是i

Rubi 2004-10-19
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 16
Load Command1(i) '加载按钮
Command1(i).Left = Command1(0).Left + 500 * i'动态数组控件排序,每行4个,一共显示4行上下对齐
If i > 4 And i < 9 Then
Command1(i).Top = Command1(0).Top + 500
Command1(i).Left = Command1(0).Left + 500 * (i - 5)
ElseIf i >= 9 And i <= 12 Then
Command1(i).Top = Command1(0).Top + 1000
Command1(i).Left = Command1(0).Left + 500 * (i - 9)
ElseIf i >= 13 And i <= 16 Then
Command1(i).Top = Command1(0).Top + 1500
Command1(i).Left = Command1(0).Left + 500 * (i - 13)
End If


Command1(i).Visible = True '显示按钮
Select Case i'同过选择判断来显示按钮上面的字符
Case 4
Command1(4).Caption = "+"
Case 8
Command1(8).Caption = "-"
Case 12
Command1(12).Caption = "*"
Case 14
Command1(14).Caption = "."
Case 15
Command1(15).Caption = "="
Case 16
Command1(16).Caption = "/"
Case Else'出了以上,其他的显示数字按照以下公司
Command1(i).Caption = Command1(0).Caption + i
End Select
Next i'这里进行i的循环
End Sub
没有错误提示,但是显示出了问题,显示的实际情况是这样的
1 2 3 4 +
6 7 8 -
10 11 12 *
14 . - /
Rubi 2004-10-19
  • 打赏
  • 举报
回复
我就是设计了一个窗体,然后一个文本框,然后设计了一个command1命令按钮,其他的按钮在窗体启动的时候就是form_load的时候自动加载其他的按钮阿
starsoulxp 2004-10-19
  • 打赏
  • 举报
回复
//Command1(0).Caption ?

Command1(0)是哪个?

starsoulxp 2004-10-19
  • 打赏
  • 举报
回复
窗体设计过程是怎么样的?
starsoulxp 2004-10-19
  • 打赏
  • 举报
回复
哪一句提示出错了 ?

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧