Помогите новичку
Добавлено: 23 дек 2009, 12:56
Мне нужно построить график функции в Microsoft Studio Visual Basic. Помогите, пожалуйста
--------------------------------------------------------------------------------
Добавлено сообщение
--------------------------------------------------------------------------------
Public Class Grafik01
Dim St As String
Dim i, i1 As Integer
Dim x, y As Double
Dim x1, y1 As Double
Dim dx, dy, n, m, a, b, b1, min, max As Integer
Dim y0, y2, k, kk As Integer
Dim f As Boolean
'Public Function f(ByVal x As Object) As Object
' f = Math.Exp(x) * Math.Sin(x)
'End Function
'создается процедура загрузки формы
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
k = 25
f = False
VScrollBar1.Value = k
Button4.Text = k
dx = Graf1.Width / 2
dy = Graf1.Height / 2
End Sub
'Эта кнопка очистки
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
f = False
Graf1.Refresh()
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
k = VScrollBar1.Value
Button4.Text = k
If f = True Then
Setka()
End If
End Sub
'эта кнопка построит график
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Setka()
End Sub
' процедура построения
Private Sub Setka()
f = True
Graf1.Refresh()
Dim g As Graphics = Graf1.CreateGraphics
Dim MyPen As New Pen(Color.Black)
MyPen.DashStyle = Drawing2D.DashStyle.Solid
MyPen.Color = System.Drawing.Color.Black
MyPen.Width = 1
For i = dy To 0 Step -k
g.DrawLine(MyPen, 0, i, Graf1.Width, i)
Next i
For i = dy To Graf1.Height Step k
g.DrawLine(MyPen, 0, i, Graf1.Width, i)
Next i
MyPen.Width = 2
g.DrawLine(MyPen, 0, dy, Graf1.Width, dy)
g.DrawLine(MyPen, Graf1.Width - 10, dy - 5, Graf1.Width, dy)
g.DrawLine(MyPen, Graf1.Width - 10, dy + 5, Graf1.Width, dy)
MyPen.Width = 1
For i = dx To 0 Step -k
g.DrawLine(MyPen, i, 0, i, Graf1.Height)
Next i
For i = dx To Graf1.Width Step k
g.DrawLine(MyPen, i, 0, i, Graf1.Height)
Next i
MyPen.Width = 2
g.DrawLine(MyPen, dx, 0, dx, Graf1.Height)
g.DrawLine(MyPen, dx, 0, dx - 5, 10)
g.DrawLine(MyPen, dx, 0, dx + 5, 10)
MyPen.Color = System.Drawing.Color.Red
i1 = 0
x1 = (i1 - dx) / k
y1 = -k * (Math.Sin(x)) + dy
y0 = y1
For i = 0 To Graf1.Width Step 1
x = (i - dx) / k
y = -k * (Math.Sin(x)) + dy
y2 = y
If y2 > dy Then
y2 = dy - (y2 - dy)
End If
g.DrawLine(MyPen, i1, y0, i, y2)
i1 = i
y0 = y2
Next i
End Sub
'кнопка выход
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Close()
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End Sub
End Class
Выводит ошибку здесь: Dim g As Graphics = Graf1.CreateGraphics.
Скажите, пожалуйста, в чем ошибка?
--------------------------------------------------------------------------------
Добавлено сообщение
--------------------------------------------------------------------------------
Public Class Grafik01
Dim St As String
Dim i, i1 As Integer
Dim x, y As Double
Dim x1, y1 As Double
Dim dx, dy, n, m, a, b, b1, min, max As Integer
Dim y0, y2, k, kk As Integer
Dim f As Boolean
'Public Function f(ByVal x As Object) As Object
' f = Math.Exp(x) * Math.Sin(x)
'End Function
'создается процедура загрузки формы
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
k = 25
f = False
VScrollBar1.Value = k
Button4.Text = k
dx = Graf1.Width / 2
dy = Graf1.Height / 2
End Sub
'Эта кнопка очистки
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
f = False
Graf1.Refresh()
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
k = VScrollBar1.Value
Button4.Text = k
If f = True Then
Setka()
End If
End Sub
'эта кнопка построит график
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Setka()
End Sub
' процедура построения
Private Sub Setka()
f = True
Graf1.Refresh()
Dim g As Graphics = Graf1.CreateGraphics
Dim MyPen As New Pen(Color.Black)
MyPen.DashStyle = Drawing2D.DashStyle.Solid
MyPen.Color = System.Drawing.Color.Black
MyPen.Width = 1
For i = dy To 0 Step -k
g.DrawLine(MyPen, 0, i, Graf1.Width, i)
Next i
For i = dy To Graf1.Height Step k
g.DrawLine(MyPen, 0, i, Graf1.Width, i)
Next i
MyPen.Width = 2
g.DrawLine(MyPen, 0, dy, Graf1.Width, dy)
g.DrawLine(MyPen, Graf1.Width - 10, dy - 5, Graf1.Width, dy)
g.DrawLine(MyPen, Graf1.Width - 10, dy + 5, Graf1.Width, dy)
MyPen.Width = 1
For i = dx To 0 Step -k
g.DrawLine(MyPen, i, 0, i, Graf1.Height)
Next i
For i = dx To Graf1.Width Step k
g.DrawLine(MyPen, i, 0, i, Graf1.Height)
Next i
MyPen.Width = 2
g.DrawLine(MyPen, dx, 0, dx, Graf1.Height)
g.DrawLine(MyPen, dx, 0, dx - 5, 10)
g.DrawLine(MyPen, dx, 0, dx + 5, 10)
MyPen.Color = System.Drawing.Color.Red
i1 = 0
x1 = (i1 - dx) / k
y1 = -k * (Math.Sin(x)) + dy
y0 = y1
For i = 0 To Graf1.Width Step 1
x = (i - dx) / k
y = -k * (Math.Sin(x)) + dy
y2 = y
If y2 > dy Then
y2 = dy - (y2 - dy)
End If
g.DrawLine(MyPen, i1, y0, i, y2)
i1 = i
y0 = y2
Next i
End Sub
'кнопка выход
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Close()
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End Sub
End Class
Выводит ошибку здесь: Dim g As Graphics = Graf1.CreateGraphics.
Скажите, пожалуйста, в чем ошибка?