Пример 2 (ListBox)
Private Sub ListBox1_Change()
Select Case ListBox1.ListIndex
Case 0
Me.BackColor = vbYellow
Case 1
Me.BackColor = vbGreen
Case 2
Me.BackColor = vbRed
End Select
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "Желтый"
ListBox1.AddItem "Зеленый"
ListBox1.AddItem "Красный"
'ListBox1.ListIndex = 0
End Sub
В завершение разберем следующий пример:
Дана функция F(x)=2x-sin(x)-0,5.
Ø Найти корень уравнения на интервале [-1,1],
Ø вычислить значение на интервале [-1,1] с шагом =0,1 и
Ø построить график функции
Текст программного кода приведен ниже
Function f(x As Double) As Double
f = 2 * x - Sin(2 * x) - 0.25
End Function
Private Sub CommandButton1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Dim h As Double
Dim x As Double
Dim y As Double
a = CDbl(TextBox1.Text)
b = CDbl(TextBox2.Text)
h = CDbl(TextBox4.Text)
eps = 0.0001
If OptionButton1.Value = True Then
Do While Abs(b - a) > eps
c = (a + b) / 2
If f(a) * f(c) > 0 Then
a = c
Else
b = c
End If
Loop
c = (a + b) / 2
Label3.Caption = "Результат" & Chr(13) & _
"Значение корня = " & c
'TextBox3.Text = c
Label3.Visible = True
End If
If OptionButton2.Value = True Then
x = a: i = 0
Do While x <= b
y = f(x)
ListBox1.ColumnCount = 2
ListBox1.ColumnWidths = "30;40"
ListBox1.AddItem x
ListBox1.List(i, 1) = y
i = i + 1
x = x + h
Loop
ListBox1.Visible = True
Label4.Visible = True
End If
If OptionButton3.Value = True Then
x = a: i = 1
Do While x <= b
y = f(x)
Worksheets(2).Cells(i, 1) = x
Worksheets(2).Cells(i, 2) = y
i = i + 1
x = x + h
Loop
График
End If
End Sub
Private Sub CommandButton2_Click()
Hide
End Sub
Private Sub UserForm_Initialize()
TextBox1.Text = " "
TextBox2.Text = ""
TextBox4.Text = ""
TextBox1.SetFocus
End Sub
Sub График()
'
' График Макрос
' Макрос записан 28.05.2009 (Татьяна Донец)
'
'
i = 1
While Worksheets(2).Cells(i, 1) <> ""
i = i + 1
Wend
i = i - 2
Range("A1:B" & i).Select
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Sheets("Лист2").Range("A1:B" & i), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Лист2"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "График функции"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
End Sub
Дата добавления: 2015-08-08; просмотров: 838;