Author: Maliha Atteeq
Download Source Code : 1097_Kode.zip
In this simple article you will learn that how to draw a sine curve on the form in vb and C #.net.
Graphics:
While playing with the graphics you must keep the following point in your mind:
You can’t draw graphics on simple load event of the form. To create graphics on the form, you must override the onpaint() function of the form.
The syntax of the onpaint() function is:
C#:
protected override void OnPaint (PaintEventArgs e)
{
}
VB:
Dim e As PaintEventArgs
Me.OnPaint(e)
Make an object of garaphics and an array of pointF class. There are two constructors for making the object, depending on your requirements you can select anyone among them.
C#:
PointF[] aptf = new PointF[cx];
VB:
Dim aptf As PointF() = New PointF(cx - 1) {}
The next function is the drawLine(). This function has 3 overloaded methods,depending on your requirements you can use any of them. The return type of this function is void.
The syntax of the DrawLine() function is:
C#:
g.DrawLines(new Pen(Color.BlueViolet), aptf);
VB:
g.DrawLines(New Pen(Color.BlueViolet), aptf)
To demonstrate make a new window application and write the following code on form’s onpaint event:
C#:
protected override void OnPaint(PaintEventArgs e)
{
int cx = ClientSize.Width;
int cy = ClientSize.Height;
Graphics g = e.Graphics;
PointF[] aptf = new PointF[cx];
for (int i = 0; i < cx; i++)
{
aptf[i].X = i;
aptf[i].Y = cy / 2 * (1 - (float)Math.Sin(i * 2 * Math.PI / (cx - 1)));
}
g.DrawLines(new Pen(Color.BlueViolet), aptf);
}
VB:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim cx As Integer = ClientSize.Width
Dim cy As Integer = ClientSize.Height
Dim g As Graphics = e.Graphics
Dim aptf As PointF() = New PointF(cx - 1) {}
For i As Integer = 0 To cx - 1
aptf(i).X = i
aptf(i).Y = cy / 2 * (1 - CSng(Math.Sin(i * 2 * Math.PI \ (cx - 1))))
Next
g.DrawLines(New Pen(Color.BlueViolet), aptf)
End Sub
Now write the following code on FORM LOAD event:
C#:
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "DEVASP APPLICATION";
}
VB:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Text = "DEVASP APPLICATION"
End Sub
This simple article tells that how to draw a sine curve on the form in vb and C #.net.