Click to See Complete Forum and Search --> : [RESOLVED] drawing a line?
greythej
Dec 27th, 2005, 05:55 PM
it sounds silly, but i want to have a line on a form, in vb6 i could choose a line from the toolbox and place it in the form. I can't seem to do this in c#. any help please??
GTJ
RobDog888
Dec 27th, 2005, 06:01 PM
You can use GDI+ to draw graphics. I mostly know VB.NET but its all similar in .NET. Or you can also use a label with its height set to the thickness you desire. Then color the background and you have a makeshift line control.
greythej
Dec 27th, 2005, 06:04 PM
cheers for the quick reply..
good idea with the label control, but i can't get it thin enough.
How do i use the GDI+ thing??
RobDog888
Dec 27th, 2005, 06:08 PM
You need to import the System.Drawing namespace. Then declare a Graphics object. From there you get a method called DrawLine that takes your points and color to draw in.
RobDog888
Dec 27th, 2005, 06:11 PM
Here is one of my VB.NET examples of drawing text using GDI+. Shouldnt be too much to convert it to C#.
Option Explicit On
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents rtbCanvas As System.Windows.Forms.RichTextBox
Friend WithEvents btnDrawMe As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.rtbCanvas = New System.Windows.Forms.RichTextBox
Me.btnDrawMe = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'rtbCanvas
'
Me.rtbCanvas.Location = New System.Drawing.Point(24, 20)
Me.rtbCanvas.Name = "rtbCanvas"
Me.rtbCanvas.Size = New System.Drawing.Size(244, 196)
Me.rtbCanvas.TabIndex = 0
Me.rtbCanvas.Text = ""
'
'btnDrawMe
'
Me.btnDrawMe.Location = New System.Drawing.Point(188, 228)
Me.btnDrawMe.Name = "btnDrawMe"
Me.btnDrawMe.TabIndex = 1
Me.btnDrawMe.Text = "Draw Me"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnDrawMe)
Me.Controls.Add(Me.rtbCanvas)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnDrawMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawMe.Click
Dim br As New SolidBrush(Color.Red)
Dim ft As New Font("Arial", 12, FontStyle.Italic Or FontStyle.Bold)
Me.rtbCanvas.CreateGraphics.DrawString("RobDog888 w00t! :D", ft, br, Me.rtbCanvas.Left + 10, Me.rtbCanvas.Top + 10)
br.Dispose()
ft.Dispose()
End Sub
End Class
RobDog888
Dec 27th, 2005, 06:21 PM
You can set the height of the label in its properties window to 1 pixel or 2. Easier then resizing as the snap to grid may cause it to increase or decrease by 4-8 pixels.
greythej
Dec 27th, 2005, 06:25 PM
o yer got it cheers...
GTJ
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.