我有一个分配,用户将在列表框中输入数字。当用户输入数字时,它将计算平均值和标准偏差。(我们的教授不希望我们使用内置的标准差函数)程序将有一个上下文菜单,这些是创建菜单的指南:您必须创建一个名为SetMenu的子例程。它用于根据列表的内容启用和禁用上下文菜单。当列表为空时,禁用清除。当列表中至少包含一个数字时,将启用“清除”。如果未选择任何内容,则禁用“删除项目”。选择某些内容后,将启用“删除项目”。 这就是我需要帮助的地方,创建了这个子程序。
这是我的代码:
Option Strict On
Public Class Form1
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click
ListBox.Items.Add(InputBox.Text)
InputBox.Text = ""
End Sub
Private Sub ComputeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComputeBtn.Click
Dim sum, x, number, numberSum, subtractedNum As Integer
Dim average, insideRoot, squaredRoot As Double
For x = 0 To ListBox.Items.Count - 1
ListBox.SelectedIndex = x
number = Integer.Parse(ListBox.SelectedItem.ToString)
sum = sum + number
average = sum / ListBox.Items.Count
Next
AverageLbl.Text = average.ToString
For x = 0 To ListBox.Items.Count - 1
ListBox.SelectedIndex = x
number = Integer.Parse(ListBox.SelectedItem.ToString)
subtractedNum = CInt((number - average) ^ 2)
numberSum = numberSum + subtractedNum
insideRoot = (numberSum / (ListBox.Items.Count - 1))
squaredRoot = insideRoot ^ (1 / 2)
Next
DeviationLbl.Text = squaredRoot.ToString("N2")
End Sub
Private Sub ClearMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearMenu.Click
ClearMenu.Enabled = False
SetMenu()
ListBox.Items.Clear()
End Sub
Private Sub DeleteMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteMenu.Click
ListBox.Items.Remove(ListBox.SelectedItem)
End Sub
Private Sub InputBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputBox.TextChanged
If IsNumeric(InputBox.Text) And InputBox.Text <> "" Then
AddBtn.Enabled = True
Else
AddBtn.Enabled = False
End If
End Sub
Private Sub SetMenu()
'Need help here
End Sub
结束班
答案 0 :(得分:1)
怎么样:
Private Sub toggleContextMenuItems()
ClearToolStripMenuItem.Visible = (ListBox1.Items.Count > 0)
DeleteToolStripMenuItem.Visible = (ListBox1.SelectedIndex > -1)
End Sub
然后在需要时调用此方法。