How to Draw Lines Vb.net

Thread: How can I draw a straight line in vb.net?

  1. July 26th, 2003,06:20 AM #1

    Question How can I draw a straight line in vb.net?

    Morning all,

    Is there anyway to add a straight line or curved line to my application in vb.net?

    In vb6 you could use the line component from the toolbox to do this but that component seems to have been removed from the vb.net toolbox.

    Thanks in advance,

    B.


  2. July 26th, 2003,08:16 AM #2

    There isn't a line control in VB.NET... but u could code for it.

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim blackPen As New Pen(Color.Black, 3)
    ' Create coordinates of points that define line.
    Dim x1 As Single = 100.0F
    Dim y1 As Single = 100.0F
    Dim x2 As Single = 500.0F
    Dim y2 As Single = 100.0F
    ' Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
    End Sub

    Back after a long hibernation.


  3. July 26th, 2003,11:21 AM #3

    How would you call that sub?

  4. July 26th, 2003,10:27 PM #4

    U don't need to call that sub. It fires automatically since it's a paint event.

    There are some other line controls (user drawn) available on the web as well that uses the pen technique.

    Back after a long hibernation.


  5. July 27th, 2003,12:29 AM #5

    Originally posted by newvisva
    U don't need to call that sub. It fires automatically since it's a paint event.

    There are some other line controls (user drawn) available on the web as well that uses the pen technique.

    I figured that out but it paints on the form, I need something to paint on a textbox like a background, so you can read text that goes over it

  6. July 27th, 2003,07:19 AM #6

    kasracer,

    I assume that u want to put a background pic/ paint the background of a textbox. It is not possible directly coz the textbox doesn't have a paint event.

    However, if u inherits a textbox, u could get to override the paint event which means u could draw. If u need a sample i could post it.

    Back after a long hibernation.


  7. July 27th, 2003,10:22 AM #7

    Thanks newvisva that works great but I need to add several lines on different forms. Is it possible to put this code into a class so that I can reuse it on any form in my application.

    The idea would be to have a line control which I could drag and drop onto my forms from the toolbox, much like in vb6.

    Is this possible?

    B.


  8. July 27th, 2003,12:42 PM #8

    you could do this ....
    in a module :

    Code:

    Module Module1      Public Function DrawLine(ByVal frm As Form) As Boolean         Dim grph As Graphics = frm.CreateGraphics         With grph             Dim p As New Pen(Color.Red, 3)             .DrawLine(p, 0, 50, frm.Width, 50)         End With         Return True     End Function '/// this function will handle paint events for all forms End Module
    in your chosen forms :

    Code:

                                    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint         DrawLine(Me)     End Sub

    string Signature = Censored;


  9. July 27th, 2003,02:05 PM #9

    Originally posted by newvisva
    kasracer,

    I assume that u want to put a background pic/ paint the background of a textbox. It is not possible directly coz the textbox doesn't have a paint event.

    However, if u inherits a textbox, u could get to override the paint event which means u could draw. If u need a sample i could post it.

    Could you post a sample please? If I could figure out how to paint onto the background of the textbox, then I could figureout where to put it.

  10. July 27th, 2003,03:28 PM #10

    kasracer , are you trying to draw lines in a textbox? or do something else?

    string Signature = Censored;


  11. July 27th, 2003,04:13 PM #11

    Originally posted by dynamic_sysop
    kasracer , are you trying to draw lines in a textbox? or do something else?

    I want to draw a line in the background of the text box at a margin

  12. July 27th, 2003,04:46 PM #12

    ok well here's a quick example of drawing a line across the textbox , you would have to use trial and error to get the position ( where it has 4's at present in the code example )

    Code:

                                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim g As Graphics = TextBox1.CreateGraphics         Dim pn As New Pen(Color.Red, 2)         Dim rct As New Rectangle(TextBox1.Location.X, TextBox1.Location.Y, TextBox1.Width, TextBox1.Height)         With g             .DrawLine(pn, 0, 4, rct.Width, 4)             .Save()         End With     End Sub

    string Signature = Censored;


  13. July 27th, 2003,05:46 PM #13

    I tried to use that code and I put it on when the textbox has focus (Since this will always be on the textbox)

    The line appears for like .01 of a second then the textbox covers it.


  14. July 27th, 2003,06:03 PM #14

    Question. The RichTextBox does not support a transparent background. Is there ANYWAY to make it so it does? If there was a way to allow it's backgorund to be transparent, what I'm trying to do would be very simple

  15. July 27th, 2003,06:16 PM #15

    you can make a richtextbox's back transparent

    Code:

                                    Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer     Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer     Private Const GWL_EXSTYLE As Integer = -20     Private Const WS_EX_TRANSPARENT As Integer = CInt(&H20&)        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim x As Integer = GetWindowLong(RichTextBox1.Handle, GWL_EXSTYLE)         x = x Or WS_EX_TRANSPARENT         SetWindowLong(RichTextBox1.Handle, GWL_EXSTYLE, x)         RichTextBox1.Visible = False         RichTextBox1.Refresh()         RichTextBox1.Visible = True     End Sub
    it'll work

    string Signature = Censored;


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width



How to Draw Lines Vb.net

Source: https://forums.codeguru.com/showthread.php?256806-How-can-I-draw-a-straight-line-in-vb-net

0 Response to "How to Draw Lines Vb.net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel