• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

我该处理,特别是使用数组记录在文本框输入的每个值的按钮?

用户头像
it1352
帮助1

问题说明

我在学校实习时遇到这样的问题:

使用动态数组编写一个程序,以记录最近7天的高温,并显示所有这些平均值和高温平均值.使用屏幕上的三个按钮,一个输入温度,第二个显示结果,第三个退出程序.


我该如何解决,特别是使用数组的按钮来记录在文本框中输入的每个值?

谢谢

Frank

I have such a question for my practice at school:

Write a program using dynamic arrays to record the high temperatures of last seven days and to display all of them with the average of high temperatures. Use three buttons on the screen, one to enter the temperature, second to display the result and third to exit from program.


How can I go about it esp the button that uses arrays to record each value entered in a text box?

Thank

Frank

正确答案

#1
在这里您在VBA中获得了一个示例(Visual Basic for Application-Office):

步骤:
1)打开MS Excel(已创建新工作簿)
2)使用ALT F11转到MS Visual Basic for App COde编辑器
3)使用菜单:Insert-> UserForm
4)将控件添加到UserForm:
-1个ListBox(将属性名称"更改为LstTemp
-3个CommandButton(将名称"属性更改为:CmdAddTemp,CmdDisplay,CmdExit)
-1个TextBox(将名称"属性更改为:TxtAvgTemp)
5)复制下面的代码
6)将其粘贴到UserForm模块中
7)玩得开心;)

Here you got an example in VBA (Visual Basic for Application - Office):

Steps to do:
1) Open MS Excel (new workbook is created)
2) Use ALT F11 to go to MS Visual Basic for App COde Editor
3) Use Menu: Insert->UserForm
4) Add controls to UserForm:
- 1x ListBox (change property "Name" to LstTemp
- 3x CommandButtons (change property "Name" to: CmdAddTemp, CmdDisplay, CmdExit)
- 1x TextBox (change property "Name" to: TxtAvgTemp)
5) Copy code below
6) Paste it into UserForm module
7) Have a fun ;)

'declare variables
Option Explicit
'set arrays low bound = 0
Option Base 0

'array to store temp
Dim aTemp() As Single
'
Dim iArr As Integer

Private Sub UserForm_Initialize()
'coz array start from 0 (zero)
iArr = 0
End Sub


Private Sub CmdAddTemp_Click()
Dim sTemp As String

'on error goto error handler
On Error GoTo Err_CmdAddTemp_Click

'display inputbox
sTemp = InputBox("Enter temperature", "Enter value...", "0")

'when Cancel button was pressed
If sTemp = "" Then GoTo Exit_CmdAddTemp_Click

'change array size
ReDim Preserve aTemp(iArr   1)
'check for numeric value using convertion
'when it's not numeric go to error handler
aTemp(iArr   1) = CSng(sTemp)
'get new array size
iArr = UBound(aTemp)

'add temperature on the end of list
Me.LstTemp.AddItem sTemp

'exit sub
Exit_CmdAddTemp_Click:
    Exit Sub

'error handler
Err_CmdAddTemp_Click:
    'catch all errors including convertion errors
    Select Case Err.Number
        Case 13 'can't convert value to single
            MsgBox "Enter corect value of temperature!", vbExclamation, "Error no. " & Err.Number
        Case Else
            MsgBox Err.Description, vbExclamation, "Error no. " & Err.Number
    End Select
    Resume Exit_CmdAddTemp_Click
    
End Sub


Private Sub CmdDisplay_Click()
Dim i As Integer, avgTemp As Single

'sum temperature
For i = LBound(aTemp) To UBound(aTemp)
    avgTemp = avgTemp   aTemp(i)
Next i

'divide by array size to get average temperature
avgTemp = avgTemp / UBound(aTemp)

'display returned value
Me.TxtAvgTemp = avgTemp

End Sub

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiafg
系列文章
更多 icon
同类精品
更多 icon
继续加载